user defined literals
Steven Schveighoffer
schveiguy at yahoo.com
Fri Mar 25 08:46:08 PDT 2011
On Fri, 25 Mar 2011 11:14:26 -0400, Trass3r <un at known.com> wrote:
> Steven Schveighoffer Wrote:
>> What is the need for this? Just to replace complex(0, 5) with 5i? I
>> don't see the huge benefit.
> I didn't claim it was of "huge" benefit. It's just some slight syntactic
> sugar.
> Furthermore I didn't mean to praise its usefulness. I was actually
> asking about practical issues that could arise if it _was_ implemented
> the way I presented.
OK, I understand.
>> I simply find the complication of the language is not worth the
>> benefits.
> This is more towards my question. Please elaborate how this complicates
> the language.
Aside from the parsing issues, which I think would be minor, it's more of
a problem with anyone and his mother is allowed to define literal types.
That is, someone could have 1fud and I'm not sure what the hell that
means, so I have to go find out where that xyz is defined. It must be
global, since literals can be used anywhere, so it can be anywhere.
I just find that I have very little need for custom literals with all the
wealth of features that D has, so I would be hesitant to stray from the
f(value) style which is easily understood and readily parsable.
>> Also, complex is the clear and obvious beneficiary
> Yep, though I think complex literals would still better be handled by
> the compiler specifically so that 5 + 3i is transformed to Complex(5,3)
> rather than 5 + Complex(0, 3)
We have quite powerful mixin abilities such that I think something like:
complex!("5 + 3i")
could be rewritten as Complex!double(5, 3) without compiler help. And
like you said, the feature as proposed does not do the ideal thing.
>> What are other use cases? BigXXX for sure, but Rational
>> probably cannot be covered by this since you need two numbers.
> Units, I think.
> auto dist = 1km;
> sin(90deg);
These are good points, but of course, units are open to interpretation.
Not every unit has a standard abbreviation, and not every abbreviation is
unique.
These things are highly context sensitive. For example, I could say:
auto mytime = 1h + 3m + 4s;
auto mydistance = 15m;
Notice that 3 minutes and 15 meters can't really be distinguished by the
compiler.
I like the idea of having this be more context-sensitive and also allow
for the feature without language changes:
auto mytime = tstamp!("1h + 3m + 4s");
Not only that, but it's compile-time, meaning there is no actual call to
some operator processor to generate the timestamp.
I believe we already have this for octal. If we could define a more
standard way to create these things, then I think it would be a good step.
-Steve
More information about the Digitalmars-d
mailing list