Skip to content

Create separate grammars - #1880

Open
BillWagner wants to merge 1 commit into
dotnet:v9-recordsfrom
BillWagner:separate-grammar-productions
Open

BillWagner wants to merge 1 commit into
dotnet:v9-recordsfrom
BillWagner:separate-grammar-productions

Conversation

@BillWagner

Copy link
Copy Markdown
Member

This PR introduces two separate grammar productions for record types in C# 9.

It's one of the two alternatives discussed in our November meeting.

This PR introduces two separate grammar productions for `record` types in C# 9.
Comment thread standard/classes.md

```ANTLR
record_class_base
: ':' class_type base_argument_list?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this uses class_type as the grammar for the base, with a semantic restriction that the base is a record. If we made it a grammar rule that the base was a record, the following would be prohibited:

public record R(int Number, string Message) : object;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

object is the base class of all types (§8.2.3); sometimes explicitly, a base for a class is a class_type (§8.2.1) which includes object; and sometimes simplicity, as for value types (§8.3.2).

We can follow this grammar approach by defining object to effectively be a class and a record, something like:

record_base
    : ':' record_type base_argument_list?
    ;

record_type        
    : type_name   // in the same way classes & value types are defined this is where
                  // the semantic requirement is applied – type_name must be a record
    | 'object'
    ;

Or we could keep object as a class with something like:

record_base
    : ':' record_base_type base_argument_list?
    ;

record_base_type
    : type_name   // a record type
    | 'object'
    ;

Yes, they are the same grammar with a non-terminal renamed to remove the implication that object is a class type.

I would recommend this approach as it both meshes with the current approach and avoid using a class production in record_base.

Comment thread standard/classes.md
### §rec-class-general General

A record class is a specialized reference type that is optimized for storing data rather than behavior. It provides built-in functionality that would normally require significant “boilerplate” code in a non-record class, such as value-based equality and easy immutability.
A ***record class*** is a class declared using `record`. The rules for classes apply, subject to the provisions of this subclause. Record classes provide built-in functionality for storing data, such as value-based equality and support for immutability.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One comment, which you already know:

  • A record is a reference type that is optimised for starting data rather than behaviour – of something like that, i.e. follow the original
  • In future: A value record is a value type that is…
  • record_declatation
  • In future: value_record_declaration

Etc. Enough on this here.

To me “A record class is a specialised reference type“ just reads wrong, if we are going to take the “record is a variant of class” then would it not be “A record class is a specialised class type”?

@Nigel-Ecma Nigel-Ecma left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a quick review following @BillWagner comment, a small change suggestion to better mesh with the current grammar approach.

(I added a non-review comment before I decided to review, it is a caller for the standard suggested change – either a record is a variant of class or it is a reference type (sibling?))

Comment thread standard/classes.md

```ANTLR
record_class_base
: ':' class_type base_argument_list?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

object is the base class of all types (§8.2.3); sometimes explicitly, a base for a class is a class_type (§8.2.1) which includes object; and sometimes simplicity, as for value types (§8.3.2).

We can follow this grammar approach by defining object to effectively be a class and a record, something like:

record_base
    : ':' record_type base_argument_list?
    ;

record_type        
    : type_name   // in the same way classes & value types are defined this is where
                  // the semantic requirement is applied – type_name must be a record
    | 'object'
    ;

Or we could keep object as a class with something like:

record_base
    : ':' record_base_type base_argument_list?
    ;

record_base_type
    : type_name   // a record type
    | 'object'
    ;

Yes, they are the same grammar with a non-terminal renamed to remove the implication that object is a class type.

I would recommend this approach as it both meshes with the current approach and avoid using a class production in record_base.

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

Successfully merging this pull request may close these issues.

2 participants