Last active
May 12, 2020 19:15
-
-
Save AbePralle/7fcd5671141bb6c1822f20be806e1bde to your computer and use it in GitHub Desktop.
Rogue Metacode to fix specialized ThisType
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #------------------------------------------------------------------------------ | |
| # Class Cloner | |
| #------------------------------------------------------------------------------ | |
| class Cloner [singleton] | |
| METHODS | |
| method clone<<$Type>>( obj:$Type )->$Type | |
| return $Type() | |
| endClass | |
| #------------------------------------------------------------------------------ | |
| # THIS PROGRAM DOESN'T WORK RIGHT | |
| #------------------------------------------------------------------------------ | |
| class Base | |
| METHODS | |
| method cloned->ThisType [propagated] | |
| return Cloner.clone<<ThisType>>( this ) | |
| # 'ThisType' works properly as a return type on a propagated | |
| # method. However, as a <<type specializer>> it is one-way-mapped | |
| # in the base version and there is no easy way to fix that [edit: | |
| # see update below]. | |
| # | |
| # So for now the propagated method would always call | |
| # Cloner.clone<<Base>>(), never updating to Cloner.clone<<Extended>>. | |
| # UPDATE: this code now works after all with commit | |
| # 4b88ec605a4459706a5bce398e87a1aba77c06f6 to the Rogue develop branch. | |
| endClass | |
| class Extended : Base; | |
| println Base().cloned | |
| println Extended().cloned | |
| #------------------------------------------------------------------------------ | |
| # METACODE TO THE RESCUE! | |
| #------------------------------------------------------------------------------ | |
| class Base; | |
| class Extended : Base; | |
| println Base().cloned | |
| println Extended().cloned | |
| $metacode<Type.organize> | |
| if (this.instance_of(Program.find_type("Base"))) | |
| sourceln "method cloned->" + this.name | |
| sourceln " return Cloner.clone<<$>>( this )" (this.name) | |
| injectMethod | |
| endIf | |
| $endMetacode |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment