New built-in type/entity — Symbol #9850
-
Reasoning:Alternative for ExplanationSymbol SomeSymbol;Symbols are created in compile time. Symbols are reference type. Declaration is initialization. Symbol SomeSymbol;
void func(Symbol symb)
{
if (symb is SomeSymbol)
{
...
}
}There is the System.Symbol abstract base type (and possibly keyword "symbol)" with allowed boxing and unboxing for any declared symbol. Declared symbols work as static objects. Symbol SymbolA;
var someSymbol = SymbolA;Symbol identifiers can work both as variables or types depending on context. Symbol None;
public Union Optional<T> : Union<T, None>
{
...
}Possible Symbol usage with Unions for creating custom Union, where one of possible values doesn't express anything besides itself, i.e. declared Symbol represents only itself and nothing else, it doesn't contain any excessive information. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 12 replies
-
|
IMO if C# were to adopt something like this it would be better off with companion objects, types with singleton instances. Although it's such an easy pattern to adopt manually that honestly I'm not sure that there'd be a lot of utility to it. With value types it also feels a lot less necessary. |
Beta Was this translation helpful? Give feedback.
-
|
The desire to declare something that can be used as both a type and a value has come up in discriminated unions. If a member of the union is a type and the type contains no state, it would be nice to be able to use the type as a value rather than creating new instances of the type which are all functionally interchangeable. This |
Beta Was this translation helpful? Give feedback.
I see. I'm not sure i see enough value there. Just writing
sealed class MySpecificError : Symbol;seems fine to me. But we'll consider this, and if we see enough value, move forward on it.