WurstScript
WurstScript copied to clipboard
Idea: extension properties
Like extension functions one could also have extension properties. Extension properties would allow to add new fields to an existing class (but not to other types).
Example (inspired by HotN/wurst/HeroStuff/CaptureAnimation.wurst):
// add a new timer field to the Hero class:
timer Hero.captureTimer
// usage
public function Hero.stopCaptureAnimation()
if this.captureTimer != null
this.captureTimer.release()
if this.currentEffect != null
this.currentEffect.destr()
The translation could be the same as for normal fields (just using an array).
Seems like sort of a dirty way to avoid the cost of subclassing. I'm not sure if such special-case code does more good than harm.
It has nothing to do with subclassing. It's more like open classes in C#.
I think the reason I'm skeptical is because extension-functions are considered kind of dirty in other languages (ruby for example), but in wurst they're cool because you can extend handle types.
Another issue is that I don't think I fully understand what this is for. Are you changing the base class, or only changing the class in some scoped context? In other words, once you add the timer Hero.captureTimer
, is that now a global property of all Hero objects, or only the ones in this package?
Scoping would be the same as with extension methods. So it would not be monkey patching as in Ruby.
However, the feature has not much benefits over defining an array, so I don't think I will add it:
// the following code with extesion properties ...
timer Hero.captureTimer
Hero hero = new Hero
hero.captureTimer = ....
// could be implemented with arrays:
timer array captureTimer
Hero hero = new Hero
captureTimer[hero castTo int] = ....