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
IMO we should not allow the modification of preceding arguments within the default argument expression. Or please let me know if you have any reason to support such behaviour.
If not allow the modification of a, is there compatibility problem?
The answer is no, practically, since the feature has only been introduced lately.
It might work if the output JavaScript code like this.
(snip)
Can decreased number of function calls. But many changes you will probably need.
Such design was considered when we introduced the default arguments, but had been dismissed for two reasons: that the model is complicated, and that it would be slow. Current design will run fast together with the inline optimizer, since the default argument expressions can be inline-expanded in the caller side by the optimizer.
What I mean is that modification of preceding arguments should disallowed only within the default argument expressions, and it should continue to be allowed at other locations. i.e.
function fn(a : number, b : number = 10) : void {
a = 11;
}
should be considered valid, OTOH
function fn(a : number, b : number = ++a) : void {
}
a is changed outside the default argument expressions.
Should b() always returns 1 even after the change?
IMO b() should return 1 even after a is being changed, since it is more consistent to consider the default argument expressions as something that is expanded at the caller-side rather than the opposite.
Below is a valid example using default argument expression that prints Derived:123. Such behaviour would seem natural if the expression is considered to be expanded at the caller side (since the intended call signature would be Base#f(:number)), but would seem unnatural if it were to be considered as expanded at the callee-side (i.e. method with no arguments is only defined in the base class, and thus the output should be Base:123 in such case).
class Base {
function f(n : number = 123) : void {
log "Base:" + n;
}
}
class Derived extends Base {
override function f(n : number) : void {
log "Derived:" + n;
}
}
class _Main {
static function main(args : string[]) : void {
(new Derived).f(); // output: Derived:123
}
}
Having said that, it might not be necessary to disallow the modification of preceding arguments within default argument expressions. Just documenting the behavior might be sufficient.
The override example illustrates the feature of the compiler that is already being used. That is why I believe it is better to consider the default argument expressions as something that is expanded at the caller-side which in turn means that the return value of b() should not be affected by the modification of a, unless there is a strong reason not to do so.
Activity
tuchida commentedon May 16, 2014
I was trying to fix tuchida@8770306 . I found two problems.
First. This code fails to compile when add
--optimize release --minifyoptions. I reported #328 .Second, It is not expected in the case of closure has side effects. I do not know how to deal with this case.
Do you have a good idea?
kazuho commentedon May 19, 2014
Thank you for looking into the issue.
IMO we should not allow the modification of preceding arguments within the default argument expression. Or please let me know if you have any reason to support such behaviour.
tuchida commentedon May 19, 2014
For example.
If not allow the modification of
a, is there compatibility problem?tuchida commentedon May 19, 2014
It might work if the output JavaScript code like this.
Can decreased number of function calls. But many changes you will probably need.
kazuho commentedon May 19, 2014
The answer is no, practically, since the feature has only been introduced lately.
Such design was considered when we introduced the default arguments, but had been dismissed for two reasons: that the model is complicated, and that it would be slow. Current design will run fast together with the inline optimizer, since the default argument expressions can be inline-expanded in the caller side by the optimizer.
Argument is read-only when used default parameters.
tuchida commentedon May 19, 2014
Thanks!
How do you like tuchida@bbaabac ?
kazuho commentedon May 19, 2014
Thank you for looking into the issue.
What I mean is that modification of preceding arguments should disallowed only within the default argument expressions, and it should continue to be allowed at other locations. i.e.
should be considered valid, OTOH
should be invalid.
tuchida commentedon May 19, 2014
This case.
ais changed outside the default argument expressions.Should
b()always returns 1 even after the change?Or should disallowed modification of 'a', because it is referenced in the default argument expressions?
kazuho commentedon May 20, 2014
Thank you for the clarification.
IMO
b()should return 1 even afterais being changed, since it is more consistent to consider the default argument expressions as something that is expanded at the caller-side rather than the opposite.Below is a valid example using default argument expression that prints
Derived:123. Such behaviour would seem natural if the expression is considered to be expanded at the caller side (since the intended call signature would beBase#f(:number)), but would seem unnatural if it were to be considered as expanded at the callee-side (i.e. method with no arguments is only defined in the base class, and thus the output should beBase:123in such case).kazuho commentedon May 20, 2014
Having said that, it might not be necessary to disallow the modification of preceding arguments within default argument expressions. Just documenting the behavior might be sufficient.
tuchida commentedon May 20, 2014
The very interesting case of override method.
I do not know which is the best.
kazuho commentedon May 20, 2014
The override example illustrates the feature of the compiler that is already being used. That is why I believe it is better to consider the default argument expressions as something that is expanded at the caller-side which in turn means that the return value of
b()should not be affected by the modification ofa, unless there is a strong reason not to do so.Argument is read-only with in default argument expressions.