July 23, 2007 Re: DMD 1.019 and 2.003 releases | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | "Walter Bright" <newshound1@digitalmars.com> wrote in message news:f82v81$cvb$2@digitalmars.com... > > f: pointer to function returning void > foo: function returning void Ahh.. there's the distinction. |
July 23, 2007 Re: DMD 1.019 and 2.003 releases | ||||
---|---|---|---|---|
| ||||
Posted in reply to Derek Parnell | Reply to Derek,
> Are you serious??? Why are we allowed to do mathematics with
> characters?
>
I have done this a few times
char c;
int v = (c - '0')
|
July 23, 2007 Re: DMD 1.019 and 2.003 releases | ||||
---|---|---|---|---|
| ||||
Posted in reply to Derek Parnell | Derek Parnell wrote:
> On Mon, 23 Jul 2007 12:22:32 -0700, Walter Bright wrote:
>
>> Don Clugston wrote:
>>> Walter Bright wrote:
>>>> Don Clugston wrote:
>>>>> I'm having trouble understanding the difference between "isScalar", "isArithmetic", and "isIntegral". Is this table correct?
>>>>>
>>>>> int uint real wchar
>>>>> Y Y Y N isArithmetic
>>>>> N N Y N isFloating
>>>>> Y Y N Y isIntegral
>>>>> Y Y Y Y isScalar
>>>>> N Y N N isUnsigned
>>>>>
>>>>> ----------------
>>>> wchar is arithmetic and unsigned.
>>> OK. Then is there any difference between scalar and arithmetic ?
>> scalar includes pointers.
>
> And yet one can do (some) arithmetic on pointers ... !?
Yes. These definitions of integral, arithmetic, and scalar are common in C derived languages (and is defined in the C standard, too).
|
July 23, 2007 Re: DMD 1.019 and 2.003 releases | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | Walter Bright Wrote:
> Sean Kelly wrote:
> > Walter Bright wrote:
> >> Robert Fraser wrote:
> >>> May I ask why you put the road there before traits? I know there's a std.traits, but perhaps a different keyword entirely or just a single underscore might be more appropriate...? It looks a bit like a compiler-specific extension to me...
> >>
> >> I was thinking they should be buried inside of templates.
> >
> > But does that affect what the name should be?
>
> Don't need a prominent keyword for it.
Hmmmm.... it just seems to disturb the zen of D (having one keyword with two underscores), and I think it'll be used just as much as some other keywords, whether it's buried in templates or not.
|
July 23, 2007 Re: DMD 1.019 and 2.003 releases | ||||
---|---|---|---|---|
| ||||
Posted in reply to Christian Kamm | Christian Kamm Wrote:
> The extended compile time reflection opens the door for all kinds of cool things! Here's an RTTI-Visitor, for instance.
>
> It uses compile-time foreach to build a sequence of if-statements that check the classinfo and call the matching method.
>
> Regards,
> Christian
> // released into the public domain
>
> import std.stdio;
>
> class A
> {}
>
> class B : A
> {}
>
> class C : A
> {}
>
> class Dispatcher
> {
> void foo(A a)
> {
> writefln("A");
> }
>
> void foo(B b)
> {
> writefln("B");
> }
>
> mixin Dispatch!("foo");
> }
>
> class ExDispatcher : Dispatcher
> {
> alias Dispatcher.foo foo;
>
> override void foo(B b)
> {
> writefln("B override");
> }
>
> void foo(C c)
> {
> writefln("C");
> }
>
> mixin Dispatch!("foo");
> }
>
> void main()
> {
> auto disp = new Dispatcher;
> disp.dispatch(new A); // calls disp.foo(A)
> disp.dispatch(new B); // calls disp.foo(B)
>
> Dispatcher exdisp = new ExDispatcher;
> exdisp.dispatch(new A); // calls disp.foo(A)
> exdisp.dispatch(new B); // calls exdisp.foo(B)
> exdisp.dispatch(new C); // calls exdisp.foo(C)
> }
>
>
> class MethodNotFoundException : Exception
> {
> this(string msg) { super(msg); }
> }
>
> template Dispatch(string fname)
> {
> mixin("alias typeof(" ~ fname ~ ") fsym;");
> static if(is(fsym arg_types == function) && is(fsym return_type == return))
> {
> return_type dispatch(Object o, arg_types[1..$] params)
> {
> alias typeof(__traits(getVirtualFunctions, typeof(this), fname)) funcs;
> foreach(i, func; funcs)
> {
> static if(is(func it_arg_types == function) && is(func it_return_type == return))
> {
> static assert(is(arg_types[1..$] == it_arg_types[1..$]) && is(return_type == it_return_type),
> "Except for the first argument, the signature of all functions must be identical.");
> static assert(is(it_arg_types[0] == class), "First argument must be a class type.");
>
> if(it_arg_types[0].classinfo is o.classinfo)
> return __traits(getVirtualFunctions, this, fname)[i](cast(it_arg_types[0]) o, params);
> }
> else
> static assert(false, fname ~ " is not a function");
> }
>
> throw new MethodNotFoundException("No matching method '" ~ fname ~ "' found in " ~ this.classinfo.name ~ " for class " ~ o.classinfo.name);
> }
> }
> else
> static assert(false, fname ~ " is not a function");
> }
>
>
Awesome! My first thought was generating hash functions for arbitrary structures, which would make using classes in AAs much easier.
But there's still a niche for runtime reflection!
|
July 23, 2007 Re: DMD 1.019 and 2.003 releases | ||||
---|---|---|---|---|
| ||||
Posted in reply to Carlos Santander | Carlos Santander wrote: > Walter Bright escribió: >> >> http://www.digitalmars.com/d/1.0/changelog.html >> http://ftp.digitalmars.com/dmd.1.019.zip >> >> http://www.digitalmars.com/d/changelog.html >> http://ftp.digitalmars.com/dmd.2.003.zip > > How can we check specific overloads with __traits? For example, > > class A > { > abstract void foo(); > int foo(int i) { return i; } > } > > void main () > { > auto isvirtual = __traits(isAbstractFunction, A.foo); // what is it? > } You'll need to split them apart with getVirtualFunctions, or cast the A.foo. > BTW, the documentation for __traits has isVirtualFunction in the example for isAbstractFunction. Fixed. |
July 24, 2007 Re: DMD 1.019 and 2.003 releases | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | Walter Bright Wrote:
>
> http://www.digitalmars.com/d/1.0/changelog.html http://ftp.digitalmars.com/dmd.1.019.zip
>
> http://www.digitalmars.com/d/changelog.html http://ftp.digitalmars.com/dmd.2.003.zip
BTW, is extern(System) mentioned in the docs (it's there under the list of available externs but it doesn't explain exactly what it does). I understand from the discussions before that it means extern(Windows) on Windows and extern(C) on Linux, but is there a generalized definition? What would it mean on Mac or Solaris? (extern(C), I'm guessing....)
|
July 24, 2007 Re: DMD 1.019 and 2.003 releases | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | BCS wrote:
> Reply to Derek,
>
>> Are you serious??? Why are we allowed to do mathematics with characters?
>>
>
> I have done this a few times
>
> char c;
> int v = (c - '0')
Perfectly reasonable, as is character +/- integer, but character + character is nonsense, just as it makes sense to subtract two points yielding a vector, or add a vector to a point (yielding a point) but no sense to "add" two points in a mere affine space.
-- James
|
July 24, 2007 Re: DMD 1.019 and 2.003 releases | ||||
---|---|---|---|---|
| ||||
Posted in reply to James Dennett | On Mon, 23 Jul 2007 21:25:31 -0700, James Dennett wrote: > BCS wrote: >> Reply to Derek, >> >>> Are you serious??? Why are we allowed to do mathematics with characters? >>> >> >> I have done this a few times >> >> char c; >> int v = (c - '0') > > Perfectly reasonable, as is character +/- integer, but character + character is nonsense, just as it makes sense to subtract two points yielding a vector, or add a vector to a point (yielding a point) but no sense to "add" two points in a mere affine space. I'm sorry. I am a bit of a pedantic bastard at times. I use ... char c; int v = (cast(int)c - '0') To me it the same as assuming that boolean values are integers. -- Derek (skype: derek.j.parnell) Melbourne, Australia 24/07/2007 3:13:04 PM |
July 24, 2007 Re: DMD 1.019 and 2.003 releases | ||||
---|---|---|---|---|
| ||||
Posted in reply to James Dennett | James Dennett wrote:
> BCS wrote:
>> char c;
>> int v = (c - '0')
> Perfectly reasonable, as is character +/- integer, but
> character + character is nonsense, just as it makes sense
> to subtract two points yielding a vector, or add a vector
> to a point (yielding a point) but no sense to "add" two
> points in a mere affine space.
So we get:
char +/- int : okay
int +/- char : okay
char - char : okay
char + char : baddie!
I Look at this this way: Nobody in his right mind is gonna
try to put into the compiler, which kinds of calculation
make "sense" and which don't; that would be just insane.
Also, in C at least a char is just another kind of number
that just happens to be about the right size for an ASCII
value, at least that's the way I look at it... ;-)
Regards, Frank
|
Copyright © 1999-2021 by the D Language Foundation