monkey 2 code weirdness
Community Forums/Monkey Talk/monkey 2 code weirdness
| ||
ok, why is this :=Local color := canvas.Color and this = Local clip:Recti clip.min.x = -Frame.min.x I would have thought that one version of assignment would be the best way forward. Is there a logical reason for this? |
| ||
Personal preference, I suppose. I think many (including Mark) like the former, but I'd prefer not to have to slow down and figure out what a variable's type is:Local speed1:=100 Local speed2:=100.0 Local speed3:=400 Local speed4:=200 Local speed5:=100.0 Local speed6:=60 Local speed7:=100 Local speed8:=90.0 Local speed9:=100 Local speed10:=0.5 Only way to tell here is to check for the presence of a decimal point! Then there's assignment from a function, for which you'd have to refer to the docs to check a variable's type. I much prefer the latter form of definition. |
| ||
so the := is only used when defining. E.G. using local? whilst just defining an existing variable is = |
| ||
Yes. := is short for = New <typename> |
| ||
Yes. := is short for = New <typename> No it's not :-p It's called type inference ( https://en.wikipedia.org/wiki/Type_inference ). The compiler determines the type based on what appears on the right-hand side of = So, in effect you are writing... :typename = but you can leave out typename as the compiler will work it out. So in James' example above, speed1 would be an Int, speed2 would be a Float - which is where things get a big vague... because looking at the code it wouldn't be immediately clear what types your variables might have. Whether that is a concern to you or not is your personal preference :o) |
| ||
You're right. As I hit 'post' I was thinking something was off :) |