digitalmars.D.learn - class grammar
- Nick (21/21) May 07 2021 The class grammar, as defined in the D language specification
- Adam D. Ruppe (13/20) May 07 2021 I think the grammar allows it to parse, but then defers the error
- Basile B. (13/19) May 07 2021 Just like a natural language what is grammatically correct is not
- Nick (7/27) May 07 2021 Okay. So, although the base class list grammar is permissive, the
The class grammar, as defined in the D language specification ([Classes](https://dlang.org/spec/grammar.html#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. ```d class A : int {} ``` ```d Error: class `test.A` base type must be `class` or `interface`, not `int` ``` After reading the grammar for a 'BasicType' ([BasicType](https://dlang.org/spec/type.html#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](https://dlang.org/spec/grammar.html#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: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.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