Objects as Function Parameter Defaults
Objects as Function Parameter Defaults
Can AutoHotkey support objects as default values for optional function arguments?
Re: Objects as Function Parameter Defaults
Code: Select all
myFunc("myString")
myFunc(string,obj:="")
{
if !obj
obj := {a: "cat", b: "dog"}
for k, v in obj
MsgBox,, % string, % "key: " . k . "`nvalue: " . v
}
HTH
Re: Objects as Function Parameter Defaults
- I've wanted something like this, thanks for sharing.
- I had thought that something that would tell you if the nth parameter had been passed, would be good, if possible, as a simple way to achieve this. E.g. 'IsPassed' or similar.
- I had thought that something that would tell you if the nth parameter had been passed, would be good, if possible, as a simple way to achieve this. E.g. 'IsPassed' or similar.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: Objects as Function Parameter Defaults
@jeeswg I prefer a special constant variable that indicates nothing and that is evaluated as false. For example, NULL. In addition to indicating a no value in the parameters, it could be interpreted as zero in DllCall. It could be useful to differentiate an empty string from nothing. We could differentiateClipboard := NULLfromClipboard := ""(v2).
Re: Objects as Function Parameter Defaults
Thanks Flipeador. I also think that that could be a good idea.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: Objects as Function Parameter Defaults
Having an object as a default value implies that you will receive that object whenever the parameter is omitted. Think about it: you will receive that object, not a new object each time. If you want a new object each time, that cannot be implemented as a default value.
A case like this would more intuitively be a default initializer, evaluated before the function begins if the parameter was omitted.
I have no specific plan to implement this.
A case like this would more intuitively be a default initializer, evaluated before the function begins if the parameter was omitted.
Code: Select all
myFunc(string, obj := {a: "cat", b: "dog"}) {
Re: Objects as Function Parameter Defaults
I think about default values like an expression that gets executed whenever the parameter is omitted.Having an object as a default value implies that you will receive that object whenever the parameter is omitted.
Which means we can put funcfion calls and variables etc. there.
So I don't understand how that would be implied.
Recommends AHK Studio
Re: Objects as Function Parameter Defaults
To understand my post, you need to interpret my words according to their actual English meaning.
A value is not an expression. A value is a value.
"an object" literally means one object. {} is not an object. It is an expression which produces a different object each time it is evaluated.
A parameter's default is obviously not an expression that gets executed whenever the parameter is omitted, since we can't - as you said - put function calls and variables etc. there. What you want implemented is what you imagined a "default value" to be, not what it is. "Default value" is the wrong name for it.
A value is not an expression. A value is a value.
"an object" literally means one object. {} is not an object. It is an expression which produces a different object each time it is evaluated.
A parameter's default is obviously not an expression that gets executed whenever the parameter is omitted, since we can't - as you said - put function calls and variables etc. there. What you want implemented is what you imagined a "default value" to be, not what it is. "Default value" is the wrong name for it.
Re: Objects as Function Parameter Defaults
Hmm yeah what we want is not a default value but a default expression I guess.
Recommends AHK Studio
Re: Objects as Function Parameter Defaults
Then why is there no topic requesting it be added? I've started a topic about nothing.Flipeador wrote:I prefer a special constant variable that indicates nothing and that is evaluated as false.
What would be the point of that?We could differentiate Clipboard := NULL from Clipboard := "" (v2).
Re: Objects as Function Parameter Defaults
Nothing, I forgot that it always empties before. I got confused. Clearly it does not make sense.What would be the point of that?
I've started a topic
Who is online
Users browsing this forum: No registered users and 13 guests