Bringing TypeConverter to C# editor #8881
Replies: 1 comment 1 reply
-
The entire concept of "conversion" is about conversion at runtime, not compile time. The language should be more interested in conversions at compile time and runtime conversions are discouraged. XAML needs TypeConverter because it needs to deserialize objects from stream with strings.
This is actually the major down side. Strings are untyped, requires parsing overhead, and can be misspelled. Building a complete parser of string format is heavy and hard work. Moreover, strings are naturally linear and unstructured, makes nesting very complex. Instead, the C# language should be more interested in providing building syntax for different sort of types. Taking the each examples:
This is about simplified or customized literal declaration. Specific to enums, there's already proposal #8641 and #4479, allowing syntax
This needs to run more customized syntax parser. When integrated with language, the bar is much higher than integration with application framework. The code needs to be directly executed by compiler, to have graceful error reporting and handling, etc. If there's no such compile-time integration and everything is converted at runtime, it will have no difference than a shorthand to static factory method, like
This is the complex, nested case. The outer level is solved collection expression and the proposal is reduced into With composability, different levels can evolve separately, for example you can do
This is the worst example with no interest. In the example, the different types are totally unrelated and happen to accept three numbers. The "unbuilt" source of The language feature benefiting this would be extension conversion operators. Imagine you can define this:
This one is even worse example. Parsable logic expression is immediately a new language. The examples are LINQ expressions and interpolated strings, which are integrated very deeply in the language, in order to be composable with other features.
This one is in fact unclear to me, whether constructing a new node with label or finding the corresponding node. A general-purpose language is more interested about composable blocks, instead of opaque boxes of DSL. XAML is more a DSL, with special interest of given types. Type converters are just deserializers. I do write XAML a lot, and are always struggling with the poor composability of the object or binding notations. |
Beta Was this translation helpful? Give feedback.
-
In XAML, TypeConverter refers to a converter provided by the XAML framework, which is used to convert string-form property values into specific types of objects within the .NET framework. These converters are automatically applied.
In C#, this automatic conversion feature is lacking, but there is a similar feature, User-defined explicit and implicit conversion operators.
These two features have their own advantages and disadvantages, but TypeConverter has more significance for future language service features. Below, we will refer to TypeConverter as TC and explicit and implicit conversion operators as EIC for convenience in later comparisons.
EIC (explicit and implicit conversion operators)
TC (TypeConverter)
1024
|auto
, which corresponds to specifying a definite width or not specifying a valueDouble.NaN
;What would bringing TC functionality into C# bring?
OneOf
situation. The example above (1024
|auto
) demonstrates that if you define a parameter in C# to accept a value, you may add more mental burden before or after entering the method. If this can be solved by language services, that would be great.How to introduce this feature into C#?
~
),auto
to indicateDouble.NaN
,#FF101010
to indicateColor
orBrush
.More Demonstrations
The simplest demonstration
Clearly used to reduce code volume
The following may be the thing WPF players want most
Perhaps more advanced syntax may appear, which may not only have code hints but also more complex source code generators
For other industries, such as gaming, this may lead to a more comfortable experience.
Beta Was this translation helpful? Give feedback.
All reactions