bmx-ng / bcc

A next-generation bcc parser for BlitzMax
zlib License
33 stars 12 forks source link

"New(params) override" fails #591

Closed GWRon closed 1 year ago

GWRon commented 2 years ago

I am trying to override an overloaded "New()". Prior I was using

TBase: Method Create:TBase(params)
TExt: Method Create:TExt(params)

which I now thought could be replaced with overloaded "New"-chains ... while normal "new()" works, it seems to not work with overloaded "new(params)".

SuperStrict
Framework Brl.StandardIO

Type TBase
    Method New()
        Print "TBase.new()"
    End Method

    Method New(i:Int, j:Int)
        Print "TBase.new("+i+", "+j+")"
    End Method
End Type

Type TExt Extends TBase
    Method New(i:Int, j:Int) Override
        Print "TExt.new("+i+", "+j+")"
    End Method
End Type

Local e:TExt = New TExt(10,20)

results in:

Building untitled1
[ 10%] Processing:untitled1.bmx
Compile Error: Method does not override method from its super type.
[/home/ronny/Arbeit/Tools/BlitzMaxNG/tmp/untitled1.bmx;15;0]
Build Error: failed to compile (65280) /home/ronny/Arbeit/Tools/BlitzMaxNG/tmp/untitled1.bmx
Process complete

this one:

SuperStrict
Framework Brl.StandardIO

Type TBase
    Method New(i:Int, j:Int)
        Print "TBase.new("+i+", "+j+")"
    End Method
End Type

Type TExt Extends TBase
    Method New(i:Int, j:Int)
        Print "TExt.new("+i+", "+j+")"
    End Method
End Type

Local e:TExt = New TExt(10,20)

Compiles but only outputs:

TExt.new(10, 20)

and finally:

SuperStrict
Framework Brl.StandardIO

Type TBase
    Method New()
        Print "TBase.new()"
    End Method

    Method New(i:Int, j:Int)
        Print "TBase.new("+i+", "+j+")"
    End Method
End Type

Type TExt Extends TBase
    Method New(i:Int, j:Int)
        Print "TExt.new("+i+", "+j+")"
    End Method
End Type

Local e:TExt = New TExt(10,20)

outputs:

TBase.new()
TExt.new(10, 20)
HurryStarfish commented 2 years ago

Constructors work differently and can't be overridden like regular methods. Override isn't allowed on them, although the (error message you're getting is misleading). Base types are initialized with New() by default. If you want to delegate to a different constructor, you can do that with an explicit call at the top, as in

Type TExt Extends TBase
    Method New(i:Int, j:Int)
        Super.New i, j
        Print "TExt.new("+i+", "+j+")"
    End Method
End Type
GWRon commented 2 years ago

I had this "super.new()" in mind too. And I am okay with not being able to "override".

Yet i would have thought that TExt.new(10, 20)

Would call a TBase.new(i, j) If it existed, but it only automatically calls the new() version without params.