You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
C# does not currently allow the definition of generic indexer properties. In addition, the CLR metadata tables do not provide a way to express a generic indexer property.
Motivating example
Say you have a strongly-typed OptionKey<T> for declaring options. The generic type T represents the type of value associated with a particular key.
The code above is reminiscent of Java, where "properties" do not have any special syntax. If C# supported generic indexers, the above code could instead be written as follows:
publicinterfaceIOptions{
T this<T>[OptionKey<T>key]{get;set;}}voidFoo(){IOptionso= ...;
o[DefaultOptions.SomeBooleanOption]=true;intintegerValue= o[DefaultOptions.SomeIntegerOption];}
Language vs. Runtime
Like most requests for new features in a language, it is possible for the compiler to offer support by simply "lowering" the semantics to a form that the underlying runtime supports. However, in this particular case I believe the motivating example provides similar benefits to other languages which target the same runtime, so updating the metadata format to directly express generic indexers could be advantageous especially for scenarios involving assemblies written in multiple languages.
Note that the metadata format already allows the accessor methods to include generic type parameters; it is only the property metadata that needs to be updated.
Remaining issues
The primary remaining issue is the explicit specification of generic type arguments at the call site. In the examples above, type inference was used for both getting and setting property values. At first glance, syntax like the following might work, but I have not performed a rigorous evaluation of this in the context of the complete language specification.
void Foo()
{
IOptions o = ...;
o<bool>[DefaultOptions.SomeBooleanOption] = true;
int integerValue = o<int>[DefaultOptions.SomeIntegerOption];
}
The text was updated successfully, but these errors were encountered:
C# does not currently allow the definition of generic indexer properties. In addition, the CLR metadata tables do not provide a way to express a generic indexer property.
Motivating example
Say you have a strongly-typed
OptionKey<T>
for declaring options. The generic typeT
represents the type of value associated with a particular key.Where options are exposed through the
IOptions
interface:Code could then use the generic indexer as a nice strongly-typed options store:
The code above is reminiscent of Java, where "properties" do not have any special syntax. If C# supported generic indexers, the above code could instead be written as follows:
Language vs. Runtime
Like most requests for new features in a language, it is possible for the compiler to offer support by simply "lowering" the semantics to a form that the underlying runtime supports. However, in this particular case I believe the motivating example provides similar benefits to other languages which target the same runtime, so updating the metadata format to directly express generic indexers could be advantageous especially for scenarios involving assemblies written in multiple languages.
Note that the metadata format already allows the accessor methods to include generic type parameters; it is only the property metadata that needs to be updated.
Remaining issues
The primary remaining issue is the explicit specification of generic type arguments at the call site. In the examples above, type inference was used for both getting and setting property values. At first glance, syntax like the following might work, but I have not performed a rigorous evaluation of this in the context of the complete language specification.
The text was updated successfully, but these errors were encountered: