WurstScript icon indicating copy to clipboard operation
WurstScript copied to clipboard

Add constructor sharing

Open Frotty opened this issue 10 years ago • 4 comments

Like in java, I would like to be able to call other constructors from within a constructor like this:

construct(real x, real y, angle facing)
        this(x,y)
        dummy.setFacing(..)
construct(real x, real y)
        dummy = createUnit(..)

When trying to provide a nice constructor API with wurst, you end up with many boilerplate constructors, e.g. in Fx and there are bigger examples in EBR, where I had to copy the same, long constructor several times, wasting a whole page with almost same code.

It would be fine to enforce the limit, that it has to be the first statement inside the constructor.

Frotty avatar Sep 30 '15 16:09 Frotty

You can still extract common stuff into functions until this is implemented.

peq avatar Sep 30 '15 18:09 peq

yes, i know that you can workround this with init funcstions and such and i was about to add it to the ticket - but it shouldn't really matter to the validity of this enhancement.

Frotty avatar Sep 30 '15 20:09 Frotty

Well, you said that this feature would allow you to save a lot of lines, but I think it is only 2-3 lines per constructor if you call a function instead. So the ticket is still valid, but maybe not so high in priority.

peq avatar Sep 30 '15 20:09 peq

The biggest problem with this is inheritance. You have to call super first so you cannot call your custom init function. Either do this or https://github.com/peq/WurstScript/issues/158 to fix it please.

Frotty avatar Apr 08 '16 18:04 Frotty