# HaxeFoundation/haxe-evolution

Open
wants to merge 5 commits into
from

## Conversation

Projects
None yet
7 participants

### mak added some commits Nov 14, 2017

``` add proposal "polymorphic 'this' types" ```
``` d9216d8 ```
``` add header ```
``` cc59505 ```
``` copy/paste fix ```
``` 381889c ```
``` add syntax highlighting for haxe ```
``` 43a39b7 ```
Member

### nadako commented Nov 14, 2017

 I added a link to the rendered version so one can easily view the proposal markdown document.
Member

### EricBishton commented Nov 14, 2017

 As a first thought, isn't this what static extensions are supposed to solve? Why would that mechanism be insufficient? (https://try.haxe.org/#01b82) ```using Test.Calculator; class BasicCalculator { public var result = 0.0; public function new () {} public function add(a: Int): BasicCalculator { result += a; return this; } } class Calculator { public static function sin(c:BasicCalculator): BasicCalculator { c.result = Math.sin(c.result); return c; } } class Test { static function main() { var calculator = new BasicCalculator(); var result = calculator.add(2).add(3).sin().result; // <-- Totally possible in current Haxe } }```

### matulkum commented Nov 14, 2017 • edited

 @EricBishton I agree that you solved this specific example with a static extensions. In general it is not the same though: you cannot override a method in BasicCalculator. You can also just add methods, not properties. You could not extend BasicCalculator with a static extension like in this example: ```enum Mode { RAD; DEG; } class Calculator extends BasicCalculator { public var mode: Mode; public function sin() { if( this.mode == RAD) this.result = Math.sin(this.result); else this.result = Math.sin(this.result/180*Math.PI); return this; } } class Test { static function main() { var calculator = new Calculator(); calculator.mode = DEG; var result = calculator.add(2).add(3).sin().result; } }``` Also static Extensions do not help in creating Extern definitions.
``` bugfix in code example ```
``` ae07631 ```

### matulkum left a comment

 fixed 2 little bugs in the code examples.
Member

### markknol commented Nov 14, 2017

 I wonder if this should be a type inference feature, if you leave out the return type definition, it works? Then `this` stays the same in its context, just as we have now.

### matulkum commented Nov 14, 2017

 @markknol Actually we do NOT have that now. Currently the return type `BasicCalculator.add()` will infer to `BasicCalculator` always, even if it is called from `Calculator`.. and I think rightly so. By providing `this` as an option to implicitly define the return type you can have it both ways and you have actual control. See also @nadako comment at the community forum: http://community.haxe.org/t/feature-proposal-polymorphic-this-types/174/5
Member

### markknol commented Nov 15, 2017 • edited

 Just want to mention that @andyli once wrote this very nice article: https://blog.onthewings.net/2010/12/19/method-chaining-and-fluent-interface-in-haxe/ But I'd love to have polymorphic this type too, I use a lot of chaining too.

### matulkum commented Nov 15, 2017

 @markknol thanks for the link. Did not even see that before. So this is actually a pretty "ok" workaround for this case.
Member

### EricBishton commented Nov 15, 2017

 @markknol @andyli Perhaps, if Andy gives permission, a version of his article could make it into the Haxe cookbook on the web site (with appropriate attribution).
Member

### andyli commented Nov 16, 2017

 Feel free to take it to the cookbook. I'm glad that little post is still useful. :)

Closed

Member

### RealyUniqueName commented Nov 23, 2017 • edited

 I'd like to also be able to do this: ```class Some { static function doSomething():this { var inst = new this(); return inst; } static function doAnotherThing():Class { return this; } }``` Which will allow to: Reduce amount of work on renaming types Simplify macros which need to operate with current type Simplify copy-pasting (bad side effect :) ) Maybe make it a reserved class name `This` instead of `this`.

Closed

Member

### RealyUniqueName commented Sep 21, 2018

 We discussed this proposal and came to a conclusion it could be useful. However, @Simn has no clear vision how to implement it and needs to do a research first. As for my previous comment about using `This` as the type expression, it should be a separate proposal.

Closed

### fullofcaffeine commented Dec 5, 2018 • edited

 Just want to mention that @andyli once wrote this very nice article: https://blog.onthewings.net/2010/12/19/method-chaining-and-fluent-interface-in-haxe/ But I'd love to have polymorphic this type too, I use a lot of chaining too. The link is dead. Did anyone ever add this to the cookbook? I couldn't find anything related.
Member

### markknol commented Dec 6, 2018 • edited

 @fullofcaffeine the site is up again, please PR it for the cookbook 🥇