WurstScript icon indicating copy to clipboard operation
WurstScript copied to clipboard

Add named and optional parameters

Open peq opened this issue 12 years ago • 2 comments

// consider this function:
function add(x = 0, y = 0, z = 0)
    return x + y + z

// can be called like this:
add(1, 2, 3)
add(1, 2)
add(1)
add(x = 1, y = 2, z = 3)
add(z = 3, x = 1, y = 2)
add(z = 3, y = 2)
...

peq avatar Sep 07 '12 14:09 peq

with type inference of the parameters? kewl...:+1:

Frotty avatar Jun 10 '14 22:06 Frotty

:+1: :+1: :+1: for the optional ones.

This would make function overloading obsolete for many cases (like having a increasing amount of parameters) and therefore cause smaller files/less lines per file. E.g. :

construct(string file, int duration, boolean looping, boolean is3D, boolean stopOnLeaveRange, int fadeIn, int fadeOut, string eaxSetting )

construct(string file, int duration, boolean looping )

construct(string file, int duration, boolean looping, boolean is3D )

This allows for leaving out some values, but more constructors would bloat the class/file even more. With optional:

construct(string file, int duration, boolean looping=false, boolean is3D=false, boolean stopOnLeaveRange=false, int fadeIn=DEFAULT_SOUND_FADE_IN_RATE, int fadeOut=DEFAULT_SOUND_FADE_OUT_RATE, string eaxSetting=DEFAULT_SOUND_EAX_SETTINGS )

Only the needed functions would be in the Jass code later of course. Since linebreaks are allowed between brackets there shouldn't be a visual problem either.

Frotty avatar Jul 24 '14 14:07 Frotty