WurstScript icon indicating copy to clipboard operation
WurstScript copied to clipboard

Idea: extension properties

Open peq opened this issue 10 years ago • 4 comments

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).

peq avatar Dec 23 '14 17:12 peq

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.

Cokemonkey11 avatar Aug 21 '16 22:08 Cokemonkey11

It has nothing to do with subclassing. It's more like open classes in C#.

peq avatar Aug 21 '16 23:08 peq

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?

Cokemonkey11 avatar Aug 22 '16 22:08 Cokemonkey11

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] = ....

peq avatar Aug 23 '16 17:08 peq