bmx-ng / bcc

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

Interfaces: Missed implementations only caught when creating an instance #665

Open GWRon opened 2 months ago

GWRon commented 2 months ago

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)

HurryStarfish commented 1 month ago

Is this "normal" behaviour?

Yes, because any type that contains an abstract method implicitly becomes an abstract type, and adding Abstract to the type definition is optional in that case. TBase is abstract because it inherits Draw, which is why it complains if you try to create an instance of it.

What should however be an error is adding Final to the class, because Abstract Final types are not allowed.