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

v7.x Target-typed "default" literal #67

Closed
RexJaeschke opened this issue Dec 11, 2020 · 2 comments
Closed

v7.x Target-typed "default" literal #67

RexJaeschke opened this issue Dec 11, 2020 · 2 comments
Labels
type: feature This issue describes a new feature
Milestone

Comments

@RexJaeschke
Copy link
Contributor

RexJaeschke commented Dec 11, 2020

@gafter, I'm assigning this to you in the hope you can give me early feedback on my initial thoughts on how to write a proposal for this feature, so I can get started. Thanks in advance! Of course, input from other members is also welcome.

Proposal https://github.com/dotnet/csharplang/blob/standard-proposals/proposals/csharp-7.1/target-typed-default.md allows the use of default on its own (rather than default(T)) in contexts where the type can be inferred.

The "Detailed design" part of the proposal suggests introducing a new kind of expression, the default literal. At a first/second read, that suggests a separate section in expressions.md. However, the "Summary" part of the proposal states

The target-typed default feature is a shorter form variation of the default(T) operator, which allows the type to be omitted. Its type is inferred by target-typing instead. Aside from that, it behaves like default(T).

Here's my thinking:

Rather than add a new section, what we already have in 12.7.15 Default value expressions can be extended to handle this new spelling of a default value. For example, the existing grammar could be extended thus:

default_value_expression
    : 'default' '(' type ')'
    | default_literal
    ;

default_literal
    : 'default'
    ;

rather than the more succinct

default_value_expression
    : 'default' ('(' type ')')?
    ;

[By adding the grammatically unnecessary rule default_literal, we give a name to that alternative that we can refer to in the narrative.]

That said, I see an advantage to also having a name for the existing alternative; perhaps we could do something like the following instead:

default_value_expression
    : explictly_typed_default
    | default_literal
    ;

explictly_typed_default
    : 'default' '(' type ')'
    ;

default_literal
    : 'default'
    ;

Using this approach, I think the changes to this section are minimal.

The only other edits I see are

  1. To conversion.md, add a new section default literal conversion
  2. To expressions.md deal with the possibility of default == default (if that is not already covered)

I'll write some tests to figure out what support is given by the is and as operators.

@RexJaeschke RexJaeschke added this to the C# 7.x milestone Dec 11, 2020
@jskeet jskeet added the type: feature This issue describes a new feature label Jan 7, 2021
@jskeet
Copy link
Contributor

jskeet commented Feb 3, 2021

I think I'm most in favour of the last idea, giving them different names. But it's a weakly-held position, from someone who's really not a grammar expert.

@gafter
Copy link
Member

gafter commented Feb 3, 2021

I think the idea of using different names (your last grammar in the original post) makes sense to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature This issue describes a new feature
Projects
None yet
Development

No branches or pull requests

3 participants