Remove complex and imaginary types?
Rioshin an'Harthen
rharth75 at hotmail.com
Sat Jan 12 01:00:52 PST 2008
"Reiner Pope" <none at here.com> kirjoitti viestissä
news:fm9kta$2f2e$1 at digitalmars.com...
> Daniel919 Wrote:
>
>> >>> The remaining advantage is that of imaginary literals, i.e. the i
>> >>> postfix:
>> >>>
>> >>> 3 + 5i
>> >>
>> >> I'd really like to reserve the above phrase to be reserved to mean an
>> >> imaginary number. If one has the library delivered right with the
>> >> standard compiler or if one has to walk around the Globe in search of
>> >> the One library that actually implements it, I'd still want to have
>> >> this particular notation reserved (in the Language Grammar itself) for
>> >> this particular purpose.
>> >
>> > I do too. And been thinking along the lines of simply putting a hack in
>> > that the postfix 'i' means that it's a literal of type 'imaginary', and
>> > the compiler looks to see if "std.complex" was imported.
>> >
>> > This isn't as outlandish as it sounds, as there's precedent for it both
>> > in C++ <typeinfo> and java.lang.String, as well as D's Object.
>>
>> What about a more general solution like
>> -----------------------------
>> import std.stdio, std.conv;
>>
>> struct complex {
>> real re;
>> real im;
>> complex opAdd(real r) { return complex(re+r, im); }
>> complex opAdd_r(real r) { return complex(re+r, im); }
>> complex opAdd(complex c) { return complex(re+c.re, im+c.im); }
>> string toString() { return std.conv.toString(re) ~ "+" ~
>> std.conv.toString(im) ~ "i"; }
>> }
>>
>> //complex opPostfix("i")(T)(T im) { return complex(0,im); }
>>
>> void main() {
>> // complex c = 1+5i + 2+3i + 6i;
>> complex c = 1+complex(0,5) + 2+complex(0,3) + complex(0,6);
>> writefln( c );
>> }
>> -----------------------------
>>
>> This would also allow
>>
>> real opPostfix("L")(T)(T x) { return x; }
>>
>> T opPostfix("k")(T)(T x) { return x * 1000; }
>> meter opPostfix("m")(T)(T x) { return meter(x); }
>
> Here's my version from a while back:
>
> http://www.digitalmars.com/d/archives/digitalmars/D/Suffix-based_literal_syntax_53992.html
I agree - this suffix support should make it into the language. It would be
a generic way of handling imaginary values, SI-units, etc.
However, in your post from May, you say alphabetic. I'd rather have the
suffix alphanumeric, with the first character locket to alphabetic - as we
may need to express square meters, cubic feet etc., eg. in a general-purpose
unit library.
More information about the Digitalmars-d
mailing list