Thread overview
class grammar
May 07, 2021
Nick
May 07, 2021
Adam D. Ruppe
May 07, 2021
Basile B.
May 07, 2021
Nick
May 07, 2021

The class grammar, as defined in the D language specification (Classes), seems to imply that a class can inherit from a fundamental type. Explicitly, the specification says that a 'SuperClass' is a 'BasicType'. And a 'FundamentalType' is a 'BasicType'.

However, when I attempt to compile the following declaration, I get an error.

class A : int {}
Error: class `test.A` base type must be `class` or `interface`, not `int`

After reading the grammar for a 'BasicType' (BasicType), it is not clear to me how a user-defined type (such as a class or interface) is also a 'BasicType' (as defined by the grammar). However, the compiler only accepts classes or interfaces as a base type.

How am I misunderstanding the specification?

May 07, 2021

On Friday, 7 May 2021 at 18:07:45 UTC, Nick wrote:

>

The class grammar, as defined in the D language specification (Classes), seems to imply that a class can inherit from a fundamental type. Explicitly, the specification says that a 'SuperClass' is a 'BasicType'. And a 'FundamentalType' is a 'BasicType'.

I think the grammar allows it to parse, but then defers the error till later.

There's a lot of things that are too generic for the parser to reject, but don't match the semantics later.

Like it the grammar only allowed a ClassOrInterface... it'd have to know what X is before it is parsed which is problematic.

>

Error: class test.A base type must be class or interface, not int

Notice that this is not a parse error, but rather a semantic check later on, which is consistent with that grammar.

The parser accepts it, then later on, a future pass of the compiler resolves what the word actually means and then says "wait a bit that is a sentence sure, but it doesn't actually make sense".

May 07, 2021

On Friday, 7 May 2021 at 18:07:45 UTC, Nick wrote:

>

The class grammar, as defined in the D language specification ...
is not clear to me how a user-defined type (such as a class or interface) is also a 'BasicType' (as defined by the grammar). However, the compiler only accepts classes or interfaces as a base type.

Just like a natural language what is grammatically correct is not necessarily semantically correct.

In the case of the inherithence list it's easier to choose 'BasicType' because 'TypeOf' and 'QualifiedIdentifier' can both be solved to a class or an interface.

With this example:

alias B = int;
class A : B {}

you can see that the semantic check to reject int is necessary anyway.

May 07, 2021

On Friday, 7 May 2021 at 19:33:30 UTC, Basile B. wrote:

>

On Friday, 7 May 2021 at 18:07:45 UTC, Nick wrote:

>

The class grammar, as defined in the D language specification ...
is not clear to me how a user-defined type (such as a class or interface) is also a 'BasicType' (as defined by the grammar). However, the compiler only accepts classes or interfaces as a base type.

Just like a natural language what is grammatically correct is not necessarily semantically correct.

In the case of the inherithence list it's easier to choose 'BasicType' because 'TypeOf' and 'QualifiedIdentifier' can both be solved to a class or an interface.

With this example:

alias B = int;
class A : B {}

you can see that the semantic check to reject int is necessary anyway.

Okay. So, although the base class list grammar is permissive, the restricted semantics of this list is indicated by its optional elements being explicitly named 'SuperClass' (class) and 'Interface' (interface). I now see how the 'QualifiedIdentifier' and 'Typeof' grammar could apply if these semantics are taken into account.