Enum: Please explain
Community Forums/Monkey2 Talk/Enum: Please explain
| ||
Hi I just downloaded the alpha-MX2 and looked at the various test sources. I found the Enum example: at the first read it is quite logic and readable. But, re-reading it I had some doubts. What is the difference between a const BLENDMODE_ALPHA=0 and BlendMode.Alpha? How I can understand that BlendMode.Alpha is not my own class&propriety? In this case Enum definition is quite close to the function... in a more structured file it will be quite difficult. When I read SetBlendMode:Void(blend:BlendMode) what I really read? A function that accepts a specific parameter (blend of Class BlendMode)? When I read name:something I just think at variable/parameter_name:class/type. I just feel a little confused. |
| ||
For me Enums are more about making you code more readable.and allows you to encapsulate a group of items together. There is no real differences otherwise but once you use them its hard to go back!!! Thanks for finally adding them Mark!! |
| ||
> What is the difference between a const BLENDMODE_ALPHA=0 and BlendMode.Alpha? The main difference is that enums are 'strongly typed'. BLENDMODE_ALPHA is 'just an int' - if you have a function like... Function SetBlendMode:Void( mode:int ) End ...you can pass pretty much anything to it. You can pass '5' or BLENDMODE_ALPHA or GL_ALPHA or SDL_BLENDMODE_ALPHA or any other int you happen to have lying around, and the compiler wont complain. Of course, the program may or may not work as expected! However, with this... Function SetBlendMode:Void( mode:BlendMode ) End ...you can ONLY pass BlendMode.Something. The compiler wont let you pass the wrong 'kind' of blendmode, which means more coding errors are caught at compile time. |
| ||
@marksibly: ...which means more coding errors are caught at compile time. And this is always a good thing. ;) |
| ||
However, with this... Function SetBlendMode:Void( mode:BlendMode ) End ...you can ONLY pass BlendMode.Something That's pretty cool, actually. |
| ||
I love Enums, looks like Monkey is growing up. :) |
| ||
@Mark: Have you considered a default to-string conversion of Enums to provide the Enum name instead of the string conversion of its integer value? This is very handy when debugging code, and we could always make a Int(myenum) to get the integer value if we ever need it |
| ||
> to-string conversion of Enums I think this is a good idea! One slight wrinkle with enums is representing 'bitmasks', things like ImageFlags.Filter|ImageFlags.Mipmap. I was planning to allow '&' and '|' with enums of the same type, but this does make ToString a bit trickier. |
| ||
...you can ONLY pass BlendMode.Something. The compiler wont let you pass the wrong 'kind' of blendmode, which means more coding errors are caught at compile time. Ok, this makes sense, even if this makes everyrhing more ... 'strict'. Thanks |
| ||
oh yeah, I think you got to have & and | with enums... so whatever it takes :) |
| ||
I was planning to allow '&' and '|' with enums of the same type, but this does make ToString a bit trickier. Yes, this would add some complexity when there are several bits set on a single enum variable, but maybe we can sort it out if the string conversion outputs all "bitmask" included values. It would be a maximum of 32, so it may not be too much, would it? |
| ||
It's quick a dinky little coding challenge! Here's my first attempt (haven't actually tried it)... Function EnumToString:String( enumValue:Int,enumType:IntMap<String> ) Local str:="" Repeat Local id:=enumType.Get( enumValue ) If id Return str+id For Local it:=Eachin enumType If enumValue & it.Key <> it.Key Continue enumValue&=~it.Key id=it.Value Exit Next If Not id Return str+"<ERROR>" str+=id If Not enumValue Return str str+="|" Forever End |
| ||
Looks good. The pipe can work with string splitting for (de)serializing enums maybe... |