bcc icon indicating copy to clipboard operation
bcc copied to clipboard

Interfaces: Missed implementations only caught when creating an instance

Open GWRon opened this issue 8 months ago • 1 comments

When using an interface and not implementing the required method it compiles as long as no instance is created:

SuperStrict
Framework Brl.Blitz


Interface IDrawable
  Method Draw:Int()
End Interface



Type TBase implements IDrawable
  Field y:Int
End Type

'Local b:TBase = New TBase

Remove the comment sign above and try to compile again ...

Compile Error: Can't create instance of type TBase due to abstract Method IDrawable.Draw:Int().
[/BlitzMaxNG/tmp/untitled2.bmx;11;0]
Build Error: failed to compile (65280) /BlitzMaxNG/tmp/untitled2.bmx
Process complete

Is this "normal" behaviour? I would have expected the compiler blames it in both cases.

Now I tried to check if I can circumvent the limitation of the "Can't create instance of type TBase":

SuperStrict
Framework Brl.Blitz
Import Brl.Reflection

Interface IDrawable
  Method Draw:Int()
End Interface



Type TBase implements IDrawable
  Field y:Int
End Type


Local b:TBase = TBase(TTypeID.ForName("TBase").NewObject())

This compiles - which won't if the check was done regardless of how a TBase-instance is created.

Edit: But as @HurryStarfish pointed out at discord, Reflection.mod was always enabling the creation of "abstract" type instances (leading to exceptions when calling the errorneous methods then)

GWRon avatar May 29 '24 20:05 GWRon