Open deepnight opened 4 years ago
Another discussion about this: #6841
The compiler handles a few cases when toString
is kept even if not used directly.
What's your scenario of using toString
without referencing it?
This is an example:
class Test {
public function new() {
}
public function toString() {
return "I'm test toString";
}
}
var foo = new Test();
trace(foo);
This works on HL target target, but doesn't on JS (dce removes the toString() ).
Now if I have the following example, the toString is discarded on both targets:
function debugLog(v:Dynamic) {
writeValueSomewhere( Std.string(v) );
}
debugLog(foo);
I think the first sample is supposed to work on all targets already. If it doesn't, then it's a bug.
The second one is "by design". The compiler can't figure out fields usages through dynamic typing. I think, changing v:Dynamic
to v:{function toString():String}
should fix the issue.
If there's no direct reference to a class
toString()
method, it's discarded by DCE, forcing you to add@:keep
in front of all of them.