Enum Solidity Understanding - solidity

I am trying to understand solidity bit better, however the underscores drive me crazy.
Let’s say I declare an enum
enum wine {red, white}
And now I declare a variable of type enum and I assign a value
wine _wine = wine.red
Why do I need there an underscore in front of the second wine?
Happy for comments!

You cannot have a variable with the same name as an already existing datatype.
Example from many other typed languages:
// ok
string _string = "hello";
// error - cannot have a `string` type variable called `string`
string string = "hello";
In your case, wine specifies the datatype, and _wine is the variable name.

Related

Naming recommendation about "set"

I have trouble with making proper variable name
I made some variable like "node_set_name"
but this name looks not good for me because my program uses a lot of get set functions
maybe this feeling just comes from my first language is not English...
anyway, can anyone recommend me some good variable name for replacing mathematics mean of "set"?
I think I can use "group" rather than "set"
node_group_name but I am not sure this "group" has replace meaning of "set" naturally?
typedef struct {
uint8* node_set_name;
uint8 node_set_type;
uint8 related_node_information;
uint8 element_set_count;
uint8 _pad;
uint8 node_element_list[MAX_MISC_NODE_SET_SIZE];
} fsw_misc_node_set_information;

How to modify type of a local variable in java byte code

I am rewriting bytecode using Javassist. I need to modify type of some local variables to object. How can I access local variables and change their types using Javassist?
Thanks.
This question is quite old but since I did something similar I'll post my solution:
Since I did not found any solution to change the type of a field in a class in Javassist, I just deleted the old method and added a new one with the same name but my desired type:
CtClass point = ClassPool.getDefault().get("Point");
CtField toBeDeleted = point .getField("fieldName");
point .removeField(toBeDeleted);
CtField newField = CtField.make("public int fieldName = 0;", point);
point.addField(newField );
So with this example I took the field fieldName in class point that was of type let's say Object and know has been changed in a field of type 'int' and initialized to 0

Why is empty string transformed to "(null)"?

I have following code sample:
double t_values[FFT_SIZE];
AnsiString prefix;
double multiplier;
AutoScaleData(t_values, FFT_SIZE, prefix, multiplier);
AnsiString str;
str.printf("Voltage [%sV]", prefix);
Pretty simple isn't it?
String str is used as description for numeric values displayed in graph. For value say 0.05 V it is much more comprehensive and intuitive to say instead that "voltage is 50 mV". This is ensured through using proper prefix in this case "m". For values range <1; 1000) there is no need to add any prefix. We can for example say "voltage over this element is 50 volts" and this is perfectly understandable. I have problem that library function printf keeps adding string "(null)" when prefix is empty string for example:
"Voltage [(null)V]"
I haven't seen this type of behaviour anytime before when using Microsoft Visual Studio. Can this behaviour be somehow avoided?
Short Answer:
str.printf("Voltage [%sV]", prefix.c_str());
Detailed Answer:
AnsiString contains a single class member, a char* pointer named Data. When the string is empty, the Data pointer is NULL. The Data pointer resides at the starting memory address of the AnsiString instance.
When calling printf(), its %s specifier is expecting you to pass in a char* pointer, but you are passing in an AnsiString instance instead. That is effectively the same as passing printf() the internal AnsiString::Data pointer directly. In other words, this statement:
str.printf("Voltage [%sV]", prefix);
Is effectively the same as if you had done this instead:
str.printf("Voltage [%sV]", prefix.data());
That is why printf() outputs "(null)". You are passing it a NULL pointer to begin with.
The AnsiString::c_str() method never returns a NULL pointer. If the string is not empty, c_str() returns the AnsiString::Data pointer as-is. Otherwise, it returns a pointer to a static '\0' character instead. Either way, printf() does not receive a NULL pointer in that situation. If the string is empty, it receives a pointer to a 0-length null-terminated string, so it outputs a blank string instead of "(null)".
If is very likely that your variable prefix is NULL. Try this:
str.printf("Voltage [%sV]", prefix == NULL ? "" : prefix);
I managed to do this with following construction:
str.printf("Voltage [%sV]", prefix.c_str());
But i don't understand what is the difference.
Using the Joachim's answer, I've created the following macro (using C):
#define ISNULLSTR(x) (x == NULL ? "" : x)
And I can use it like:
printf("%s", ISNULLSTR(possible_null_text));
Hopefully it will help someone reaching here for the same reason.

Evaluate if int matches a defined enum type

I have an enum typedef containing several type definitions, eg:
ActionTypeSomething = 1,
ActionTypeSomethingElse = 2
And so on.
So a method I've written evaluates a passed int and then returns a value (for example, a string) accordingly.
(NSString *)evaluatAndReturnProperResult:(int)typeID
NSString *repsonseString;
switch (typeID)
case actionTypeSomething: {
responseString = #"an appropriate string for typeID"
}
...
return responseString;
So my switch evaluates each supported type and returns the correct string.
Now for my question:
I only want to return strings for supported types (i.e., in theory any integer could be passed). If there's no match, I return nil.
Obviously I can do this using exactly the method I already have. But could I (in theory) improve performance by evaluating the passed int to see if it matches any of my defined enum types BEFORE I send it through switch (the switch isn't massive, but I'd still rather just return nil at the beginning of the method if I know there's not going to be a match).
I'm sure this is easy, could someone suggest how to evaluate if my passed integer matches any define enum ActionType before I enter the switch? In this case I'm probably prematurely optimizing, but it's more of a general question about how to do achieve it (not if I should).
You can define 2 more enum values:
typedef enum {
ActionTypeMin = 1,
ActionTypeSomething = 1,
ActionTypeSomethingElse = 2,
ActionTypeMax = 2
} ActionType;
Then check:
typeID >= ActionTypeMin && typeID <= ActionTypeMax
The argument for your method shouldn't be an int, it should ideally be the enum you have defined. This gives you some compile time checking.
If this is not possible then your default case in the switch will handle it just fine - that's what they are designed for.
You are using a TypeDef that's to limit ActionEnum to a set of values, so you shouldn't be using int in your program except for up or downstream communication. Some would say not even then and that you should recive a string and map it to the enum and vice versa.
In terms of getting your strings the usual optimsatiin is
Have an array of strings from ActionType1 to ActionTypeN.
Use the enum as the index to look it up from the array.
The array will also give you the doings to map the string to the enum.
A simple if statement of the enum cast as an integer against teh bound of the array will let you deal gracefully with a bad value, though to me that should throw a big blaring exception.

What are the exact names for the components of an argument?

Example: I have this Objective-C code:
+(NSString*)stringWithString:(NSString*)string;
String: is the name of the argument?
NSString* is the data type of the argument?
for the last part, string I'm not sure. It's somewhat a name too. But what's the exact term?
Here's an example from the apple docs:
+ (id)dateWithTimeInterval:(NSTimeInterval)seconds sinceDate:(NSDate *)date
seconds
The number of seconds to add to date. Use a negative argument to specify a date and time before date.
date
The date.
Obviously they're looking at the variable when referencing the arguments, not the name part in front of the datatype-brackets. I was always wondering how to name this thing correctly.
Here + shows that it is class function you can access it by the class not the object.
first (NSString*) shows it returns an string then stringWithString this is the name
and after that (NSString*)is the argument type.
and finally string is the argument which use as local parameter for the function.
Name of the function , variables and classes follow a naming convention for easy understanding about the code.