Skip to content
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

Closed
alrz opened this issue Sep 3, 2015 · 12 comments
Closed

Proposal: User-defined literals #4971

alrz opened this issue Sep 3, 2015 · 12 comments

Comments

@alrz
Copy link
Member

alrz commented Sep 3, 2015

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:

class Distance {
    ...
    public static literal operator Distance km(double p) { ... }
}

As an extension operator (#4945):

public static literal operator TimeSpan hour(this double h) { ... }

Using improved type aliases (#4884):

// helps with further extension methods
using Radian = double;

// maybe in another file
public static literal operator Radian deg(double degree) { ... }

Usage:

Math.Sin(30deg);
@dsaf
Copy link

dsaf commented Sep 3, 2015

Good luck 😄 #263.

@gafter
Copy link
Member

gafter commented Sep 3, 2015

This proposal requires a lexical grammar. Please provide one.

@alrz
Copy link
Member Author

alrz commented Sep 3, 2015

@gafter

Affected literals including:

literal:
: integer-literal
: real-literal
: character-literal
: string-literal

Since integer-literal and real-literal has already some suffix defined, they would be modified as follow:

integer-type-suffix:
: one of standard suffixes
: suffix_identifier

However, no changes to hexadecimal-integer-literal to avoid conflict with hexadecimal characters.

real-type-suffix:
: one of standard suffixes
: suffix_identifier

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:
: ' character ' suffix_identifier opt

string-literal:
: regular-string-literal suffix_identifier opt
: verbatim-string-literal suffix_identifier opt

@dsaf
Copy link

dsaf commented Sep 3, 2015

There is a similar feature in F# (MeasureAttribute)

This was motivated by compile-time verification as far as I understand. Does your suggestion imply that feature as well #144?

@alrz
Copy link
Member Author

alrz commented Sep 3, 2015

@dsaf the feature I proposed here is just an alternative syntax for extension methods like 12.Hours() and since it returns another type you would naturally get compile-time verification but in F# you have more specific "units of measure" like float<g kg^-1> and float<ml/cm^3>. this is not the case here.

@gafter
Copy link
Member

gafter commented Sep 3, 2015

@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"?

@alrz
Copy link
Member Author

alrz commented Sep 5, 2015

@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.

@gafter
Copy link
Member

gafter commented Sep 6, 2015

@alrz what is the advantage of this construct over extension methods?

@alrz
Copy link
Member Author

alrz commented Sep 7, 2015

@gafter it provides a concentrated syntax that implies this value is of a specific unit.

@gafter
Copy link
Member

gafter commented Sep 7, 2015

@alrz don't extension methods do the same?

@alrz
Copy link
Member Author

alrz commented Sep 7, 2015

@gafter you are convincing me that this is not a good idea. I do agree, it's not worth the effort.

@Unknown6656
Copy link

It could be perfect for complex number representation: #12699

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants