dotnet/csharplang

Open questions for `field` and `value` as contextual keywords

cston opened this issue · 2 comments

Open questions for field and value as contextual keywords

Open questions for field and value as contextual keywords. Some of these issues were raised at the previous LDM discussion.

1. Allow nameof(field) and nameof(value)?

nameof(value) is useful for error reporting, and has an obvious value of "value".

Should nameof(field) also be supported? If so, what is the expected value of nameof(field)?

if (value is null)
    throw new ArgumentNullException(nameof(value));

Console.WriteLine(nameof(field)); // ok?

2. Should value be a keyword in a property or indexer get? Should field be a keyword in an indexer?

value is the implicit parameter for set and init accessors. Should that keyword be allowed in get accessors where there is no implicit parameter?

field is the backing field for non-indexer properties. Should that keyword be allowed in indexers where there is no obvious backing field?

In both cases, the only use of the keywords may be nameof().

object P { get => nameof(value); }

object this[int i]
{
    get { return _list[i]; }
    set { _list[i] = value ?? nameof(field); }
}

3. Should field and value be considered keywords in lambdas and local functions within property accessors?

It seems obvious that field and value should be allowed in nested functions, to allow closures over those values, but confirming that field and value are keywords in nested functions.

4. Should field and value be keywords in property or accessor signatures?

field and value will be considered keywords within accessor bodies. Should they also be considered keywords outside the accessor bodies in the property and accessor signatures?

class await { }
async Task<await> F() => return default; // ok

class field { }
field P { get; set; } // ok?

5. Should nameof(field) and nameof(value) be allowed in property and accessor signatures?

Related to the previous question, but specifically as values using nameof().

[MyAttribute(nameof(field))] object P { get; set; }

object this[string x, string y = nameof(value)] { get; set; }

My opinion: 1, yes, 2, no and no, 3, yes, 4, no, 5, no.