[v2] Unset breaks property setters

Report problems with documented functionality
Michaohneel
Posts: 7
Joined: 07 Feb 2021, 16:37
Contact:

[v2] Unset breaks property setters

06 Apr 2023, 10:41

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)
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: [v2] Unset breaks property setters

07 May 2023, 08:08

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.

Return to “Bug Reports”

Who is online

Users browsing this forum: No registered users and 11 guests