You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In a trait, I have a method that is using one of the trait arguments. In case the argument is not given, it falls back to a default value.
The unexpected: as soon as the default value is generated, it is stored at trait level and gets returned on each invocation. In other words, the default value is only evaluated once. In many cases, this will be no problem. However, the issue arises with a dynamic default value. See (and try) this Ruby script, where the default value is derived from the class name:
As you can see, the return value of any subclass's demo method is the name of the first-defined class. (It would be the same with name ||= Time.now: the defaulted timestamp is instantiated once and returned on each invocation of demo.)
The distilled issue seems to be that assigning variables named like trait arguments will change state at trait level, i.e. modify the trait argument value.
Expected behavior
demo would always return the name of the current class (or the current time). Assigning a variable named like a trait argument should not change the value of the trait argument.
Workaround
Avoiding to assign variables named like any of the trait arguments.
I am not sure whether this behavior is a real bug, or if it might be a feature of define_method. Anyway, a solution that enables to ||=-assign variables named like trait arguments would be appreciated.
The text was updated successfully, but these errors were encountered:
In a trait, I have a method that is using one of the trait arguments. In case the argument is not given, it falls back to a default value.
The unexpected: as soon as the default value is generated, it is stored at trait level and gets returned on each invocation. In other words, the default value is only evaluated once. In many cases, this will be no problem. However, the issue arises with a dynamic default value. See (and try) this Ruby script, where the default value is derived from the class name:
As you can see, the return value of any subclass's
demo
method is the name of the first-defined class. (It would be the same withname ||= Time.now
: the defaulted timestamp is instantiated once and returned on each invocation ofdemo
.)The distilled issue seems to be that assigning variables named like trait arguments will change state at trait level, i.e. modify the trait argument value.
Expected behavior
demo
would always return the name of the current class (or the current time). Assigning a variable named like a trait argument should not change the value of the trait argument.Workaround
Avoiding to assign variables named like any of the trait arguments.
I am not sure whether this behavior is a real bug, or if it might be a feature of
define_method
. Anyway, a solution that enables to||=
-assign variables named like trait arguments would be appreciated.The text was updated successfully, but these errors were encountered: