Proposal: Introducing User-Defined Literals (UDL) to C# #9605
Replies: 5 comments 12 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.
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""_km
can be defined so that a numeric literal is converted into aDistance
type.This proposal aims to bring a similar capability to C#, allowing domain-specific types to be constructed directly from literals.
2. Motivation
1.5_km
is more intuitive thanDistance.FromKilometers(1.5)
.using static
allows enabling UDLs only where needed.3. Proposed Syntax
_
+ valid identifierstring
,FormattableString
, and tuplesoperator
keywordusing static
Numeric 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
,_h
toTimeSpan
would 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""_km
for 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 static
7. Alternative syntax:
suffix
contextual keywordAs an alternative to extending
operator
to declare identifier-based UDLs, a contextual keywordsuffix
could 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
operator
to minimize impact, but if the Language Design Meeting (LDM) dislikes extendingoperator
,suffix
remains a viable syntactic sugar.Beta Was this translation helpful? Give feedback.
All reactions