Proposal: Introducing User-Defined Literals (UDL) to C# #9605
Replies: 6 comments 14 replies
-
|
Why not just do something like Wrt to your motivation:
I find that debatable. But even if so, 1.5.km seems totally fine.
I don't understand this. How is that any different from calling apis? Regardless, of this is true for
Same with
Same with
I don't know that this means. But it seems the same with |
Beta Was this translation helpful? Give feedback.
-
I don't know if I agree. You already mentioned adding _km as an extension on numbers to give you distance. So I would expect _m to be the same ("meters"). But here it also means "minutes". Using actual types with actual well named methods to create and consume them avoids this sort of inherent ambiguity entirely. |
Beta Was this translation helpful? Give feedback.
-
|
The idea for this proposal started from a simple thought: |
Beta Was this translation helpful? Give feedback.
-
|
There are 2 reasons why
The above defense of |
Beta Was this translation helpful? Give feedback.
-
what a reason when we can use or or or better use extensions (https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods) |
Beta Was this translation helpful? Give feedback.
-
|
People seem to misunderstand what kind of problem this would actually solve. I, on the other hand, have encountered problems which cannot be solved without user defined literals or rather in a very ugly way.
Either way, this exact same problem comes up with software implemented quadruple precision floating point types and literals. For both these cases I simply end up logging the result of the string conversion to the console in hex in some dev environment and use constructors that take in 64bit halves instead of simply writing "1E+4000q". Really annoying, unreadable and tedious. I was able to live with that, until I encountered another issue with not having this feature in a SIMD math library I maintain. There I have half precision SIMD vectors and in order to not have to write The point is, every type that could reasonably be expressed as a literal should have a user defined literal, especially - and this is the big one guys - if other literal types like |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Proposal: Introducing User-Defined Literals (UDL) to C#
TL;DR
Allow user-defined suffixes in the form of
_<suffix>for numeric, string, interpolated string, and tuple literals.This enables concise and type-safe representation of domain-specific values, with scope control via
using static.1. What is UDL?
UDL (User-Defined Literal) is a mechanism that allows developers to attach custom meaning to literals, directly constructing instances of specific types.
In C++, for example,
operator""_kmcan be defined so that a numeric literal is converted into aDistancetype.This proposal aims to bring a similar capability to C#, allowing domain-specific types to be constructed directly from literals.
2. Motivation
1.5_kmis more intuitive thanDistance.FromKilometers(1.5).using staticallows enabling UDLs only where needed.3. Proposed Syntax
_+ valid identifierstring,FormattableString, and tuplesoperatorkeywordusing staticNumeric literal
String literal
Interpolated string (FormattableString)
Tuple literal
4. Example: Extending BCL Types (Compatibility with Future Extension Operators)
If extension operators are implemented in the future, UDLs would integrate seamlessly with existing BCL types.
For example, adding
_ms,_s,_m,_htoTimeSpanwould enable highly readable time specifications.Moreover, the .NET BCL could define and ship these suffixes by default, allowing developers to use them out of the box.
5. Prior Art
operator""_kmfor custom suffixes on numeric or string literals.These examples show that UDLs are a proven method for improving readability and type safety.
6. Benefits
using static7. Alternative syntax:
suffixcontextual keywordAs an alternative to extending
operatorto declare identifier-based UDLs, a contextual keywordsuffixcould be introduced specifically for declaring literal suffix handlers.For example:
This is arguably more readable and keeps the grammar change isolated, but it adds a new declaration form (and a new keyword) and would need to be considered in the design of extension operators as “extension suffixes.”
For v1, we propose to keep
operatorto minimize impact, but if the Language Design Meeting (LDM) dislikes extendingoperator,suffixremains a viable syntactic sugar.Beta Was this translation helpful? Give feedback.
All reactions