Compiler: raise when allocating an abstract virtual type #12141
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #3835
Fixes #12123
Consider this code:
So far, so good.
The idea is to support the scenario of having an array of types, being able to fill that with some classes, and eventually instantiate them all.
The problem comes when we put a
Foo
in that array:Previously, the compiler would actually create an instance of
Foo
, which didn't make sense becauseFoo
is abstract.So now in this PR when you do that, you get a runtime error when you call
new
onFoo
when the compiler doesn't know which of the types there are there (in the hierarchy.), like in the example above.Note that if you do:
Foo.new
the compiler absolutely knows that
Foo
is the receiver, and will produce a compile-time error instead.Alternatively, we could disallow calling
new
on a type that's "an abstract class or any of its sublclasses" but I think that can limit some use cases or scenarios.