Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
GWRon opened this issue May 29, 2024 · 1 comment
Open

Comments

@GWRon
Copy link
Contributor

GWRon commented May 29, 2024

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
Copy link
Member

HurryStarfish commented Jun 4, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants