Objects as Function Parameter Defaults

Propose new features and changes
1100++
Posts: 78
Joined: 10 Feb 2018, 19:05

Objects as Function Parameter Defaults

31 Aug 2018, 16:44

Can AutoHotkey support objects as default values for optional function arguments?
User avatar
Xtra
Posts: 2744
Joined: 02 Oct 2015, 12:15

Re: Objects as Function Parameter Defaults

31 Aug 2018, 17:09

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
}
You can do the same for the built in A_ variables.

HTH
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Objects as Function Parameter Defaults

31 Aug 2018, 17:43

Thats just not the same :(
Recommends AHK Studio
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Objects as Function Parameter Defaults

31 Aug 2018, 18:46

- 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.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

Re: Objects as Function Parameter Defaults

31 Aug 2018, 19:05

@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).
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Objects as Function Parameter Defaults

31 Aug 2018, 19:15

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
lexikos
Posts: 9553
Joined: 30 Sep 2013, 04:07
Contact:

Re: Objects as Function Parameter Defaults

05 Sep 2018, 17:18

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.

Code: Select all

myFunc(string, obj := {a: "cat", b: "dog"}) {
I have no specific plan to implement this.
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Objects as Function Parameter Defaults

06 Sep 2018, 01:07

Having an object as a default value implies that you will receive that object whenever the parameter is omitted.
I think about default values like an expression that gets executed 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
lexikos
Posts: 9553
Joined: 30 Sep 2013, 04:07
Contact:

Re: Objects as Function Parameter Defaults

06 Sep 2018, 03:18

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.
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Objects as Function Parameter Defaults

06 Sep 2018, 03:52

Hmm yeah what we want is not a default value but a default expression I guess.
Recommends AHK Studio
lexikos
Posts: 9553
Joined: 30 Sep 2013, 04:07
Contact:

Re: Objects as Function Parameter Defaults

06 Sep 2018, 04:17

Flipeador wrote:I prefer a special constant variable that indicates nothing and that is evaluated as false.
Then why is there no topic requesting it be added? I've started a topic about nothing.
We could differentiate Clipboard := NULL from Clipboard := "" (v2).
What would be the point of that?
User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

Re: Objects as Function Parameter Defaults

06 Sep 2018, 07:38

What would be the point of that?
Nothing, I forgot that it always empties before. I got confused. Clearly it does not make sense.
I've started a topic
:thumbup:

:wave:

Return to “Wish List”

Who is online

Users browsing this forum: No registered users and 37 guests