ID:2124784
 
A fun little quirk of DM I just discovered. Did you know that arguments can have expressions in their default values?

That's because DM's compiler actually includes the argument initialization in the BODY of the function.

mob
var
some_value = 2.5
proc
SomeProc(Somearg=some_value||2.5)
//Somearg will be either the value of some_value, or 2.5 if some_value is 0 or null.


mob
var
some_value = 0
proc
SomeProc(Somearg=(some_value ? 1 : 0))
//Somearg will be 1 or 0 no matter what some_value is set to.


And it gets crazier:

proc
getMoveDir(atom/a,atom/b)
if(isturf(a)&&isturf(b))
if(a.z==b.z)
var/dx = abs(a.x-b.x), dy = abs(a.y-b.y)
if(dx&&dy&&dx<=1&&dy<=1)
return get_dir(a,b)
return 0

move_event
var
atom/movable/mover
atom/OldLoc
atom/NewLoc
Dir
time
atom
movable
Move(atom/NewLoc,Dir=0,MoveEvent=new/move_event(src,loc,NewLoc,Dir,world.time,getMoveDir(loc,NewLoc)))



Is this documented anywhere?
I've avoided doing too much initialization in arguments because of this bug:

http://www.byond.com/forum/?post=73469#comment3923208
the error only triggers if you use src.

You should never use src. anyways.
the error only triggers if you use src.

Oddly, that's not entirely true.

Move(atom/NewLoc,Dir=0,Source=src)
var/atom/OldLoc = loc //runtime error


Move(atom/NewLoc,Dir=0,Source=src)
var/atom/OldLoc = src.loc //no runtime error


It's really strange. It only seems to happen on the first line of the function. As long as you don't use a src-referenced variable that you referenced implicitly in the args, or don't use an implicit variable in the first line after you referenced src in the args, you are safe. If you do either, the first line must be explicit if implicit, or implicit if explicit.
There's some really spooky and REALLY annoying bug with setting default arguments for procs that are linked to Byond procs (verbs, move, cross/uncross, etc) that I absolutely hate and I'm 90% sure its what you are describing.

For example I added a third argument to cross of turf/T = src.loc, it causes the first proc call or var read to runtime no matter what. I've also had it so a verb calling a proc on the src with a similar default argument caused the same thing to occur, runtime on first var read/proc call.

I absolutely hate it even more since I can't easily reproduce it in a test project.
In response to Clusterfack
The bug is actually that accessing a src var in proc args as a default appears to cause a reference problem with src itself. The best way around this is to force the default yourself.

proc/buggy(a, b, c=src.hp)
...

proc/notbuggy(a, b, c)
if(isnull(c)) c = hp
...