-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: User-defined literals #4971
Comments
Good luck 😄 #263. |
This proposal requires a lexical grammar. Please provide one. |
Affected literals including: literal: Since integer-literal and real-literal has already some suffix defined, they would be modified as follow: integer-type-suffix: However, no changes to hexadecimal-integer-literal to avoid conflict with hexadecimal characters. real-type-suffix: So far, identifiers shouldn't have conflict with predefined suffixes. To workaround this limitation, we could prepend a mandatory underline to suffix_identifier. For character-literal and string-literal an optional suffix_identifier would be added. character-literal: string-literal: |
This was motivated by compile-time verification as far as I understand. Does your suggestion imply that feature as well #144? |
@dsaf the feature I proposed here is just an alternative syntax for extension methods like |
@alrz OK, thanks. What are the static semantics (i.e. type rules and/or specification by translation into existing constructs)? I presume these new literals do not designate what the language spec currently calls "constants"? |
@gafter It will be more of a syntactic sugar similar to extension methods which provides a concise syntax for data that has units, and may return types other than those that are considered as a constant. |
@alrz what is the advantage of this construct over extension methods? |
@gafter it provides a concentrated syntax that implies this value is of a specific unit. |
@alrz don't extension methods do the same? |
@gafter you are convincing me that this is not a good idea. I do agree, it's not worth the effort. |
It could be perfect for complex number representation: #12699 |
Allows integer, floating-point, character, and string literals to produce objects of user-defined type by defining a user-defined suffix; providing a nice syntax for defining units of measure and domain-specific literals in a DDD context.
There is a similar feature in F# (
MeasureAttribute
) and (surprisingly) C++, but I really feel the need for this in C# as well.As a regular operator:
As an extension operator (#4945):
Using improved type aliases (#4884):
Usage:
The text was updated successfully, but these errors were encountered: