monkey 2 code weirdness

Community Forums/Monkey Talk/monkey 2 code weirdness

AdamStrange(Posted 2016) [#1]
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?


BlitzSupport(Posted 2016) [#2]
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.


AdamStrange(Posted 2016) [#3]
so the := is only used when defining. E.G. using local?

whilst just defining an existing variable is =


Wiebo(Posted 2016) [#4]
Yes. := is short for = New <typename>


Brucey(Posted 2016) [#5]
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)


Wiebo(Posted 2016) [#6]
You're right. As I hit 'post' I was thinking something was off :)