Page 1 of 1

[v2] Unset breaks property setters

Posted: 06 Apr 2023, 10:41
by Michaohneel

Code: Select all

class Test {
    prop {
        set => msgbox()
    }
}

Test().prop := unset
This code throws Error: Missing a required parameter on the assignment.

This is because setters are translated to functions with one required parameter.
Manually defining a setter with an optional parameter fixes this behavior:

Code: Select all

x := {}
x.defineProp("prop", {set: (value?) => msgbox()})
x.prop := unset
I think this is unintentional and unintuitive. If it is intentional, the error message should be improved – it may not be obvious to a user that there is a function being called upon the assignment.

(I've tested this on v2.0.2 and v2.0)

Re: [v2] Unset breaks property setters

Posted: 07 May 2023, 08:08
by Helgef
This is because setters are translated to functions with one required parameter.
No, two required parameters,
Objects wrote:A property definition with both get and set actually creates two separate functions, which do not share local or static variables or nested functions. As with methods, each function has a hidden parameter named this, and set has a second hidden parameter named value.
This code doesn't make sense,

Code: Select all

x := {}
x.defineProp("prop", {set: (value?) => msgbox()})
x.prop := unset
the parameter you named value will not hold the value being assigned, unset or otherwise. It references the object being invoked, in this case x. If there is a bug here, it's x.prop := unset, I couldn't find any reference to its meaning in the docs.

Cheers.