www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Another use for static ifs

reply "Unknown W. Brackets" <unknown simplemachines.org> writes:
We currently have:

static if (some_const == 5)
static if (is(bar T))

As I mentioned before (before static if), I think it would be very 
useful to be able to check, with static if or similar, for the existence 
of functions.

I don't mean linking.  I just mean checking if a function was defined 
yet.  In its basic form, this doesn't seem that great:

version (Windows)
    import std.c.windows.windows;
else version (linux)
    import std.c.linux.linux;

...

static if (GetCurrentThreadId) // or defined(GetCurrentThreadId), etc.
    id = GetCurrentThreadId(...);
else
    id = pthread_self();

However, it becomes more apparent with a longer example:

version (Windows)
    import std.c.windows.windows;
else version (linux)
    import std.c.linux.linux;
else version (macosx)
    import std.c.macosx.macosx;
else version (beos)
    import std.c.beos.beos;

...

static if (GetCurrentThreadId)
    id = GetCurrentThreadId(...);
else
    id = pthread_self();

Of course, this can be handled by an alias.  But, watch this:

...

static if (!stricmp)
{
    int stricmp(char[] s1, char[] s2)
    {
       ...
    }
}

That comes right from gnuc.c in D's source.  Obviously, D wants to avoid 
problems where one operating system or vendor defines a function, and 
another doesn't... but I think it will prove inevitable.

At least with a way to know if things are defined *which doesn't depend 
on an import always having the same functions forever and everywhere* 
it's much more possible to handle both versioning, OS, and vendor 
problems.  This will of course depend on the .lib or .a file containing 
the functions listed in the import, but for my purposes I'm assuming 
*that* will always be true.

And, the last use is as I mentioned in the above paragraph: handling 
different versions of D.  For example, let's say someone ported the DMD 
compiler (or GDC, whatever) to another system... let's say to a gaming 
console, like Sony's.  If I wanted my program to compile on their 
(probably old) version of D, with their probably old phobos, I would be 
able to use the above for things added to phobos I want to use.

Without this, the above situation would be sticky indeed.  There's no 
way to know the current D version in the code, afaik, currently... so I 
would just have to tell the person that they're going to have to wait 
for the person who ported it to update it (or tell them to *try* 
compiling the new phobos with their old version.)

That said, maybe it seems like that situation is a bit far fetched... 
and this doesn't handle !is or anything.  Still, I think it would be 
helpful - even if D somehow convinces everyone who's made autoconf 
necessary to make amends.

-[Unknown]
Jun 19 2005
parent reply "Walter" <newshound digitalmars.com> writes:
"Unknown W. Brackets" <unknown simplemachines.org> wrote in message
news:d93ka0$241e$3 digitaldaemon.com...
 As I mentioned before (before static if), I think it would be very
 useful to be able to check, with static if or similar, for the existence
 of functions.

 I don't mean linking.  I just mean checking if a function was defined
 yet.  In its basic form, this doesn't seem that great:
You can already do it: static if (typeof(foo)) ... will be false if foo is not defined, and true if it is.
Jun 19 2005
parent reply "Unknown W. Brackets" <unknown simplemachines.org> writes:
Typeof, eh?  I noticed you could do &foo, but that of course fails if 
it's not defined with a compiler error.

Is this documented?  As I said, it seems like a very useful thing to use 
in many cases.

By the way: (found while checking again...)
http://www.digitalmars.com/d/declaration.html#typeof

printf("%d\n", typeof('c').size);	// prints 1

Should be:

printf("%d\n", typeof('c').sizeof);	// prints 1

And:

int[typeof[p]] a;	// a is of type int[int*]

Should be (?):

int[typeof(p)] a;	// a is of type int[int*]

Anyway, I can't get it to work.  I just get errors....

static if (typeof(foo))
    found ')' when expecting '.' following 'typeof(foo)'

static if (typeid(typeof(foo)))
    undefined identifier foo

static if (typeof(foo).sizeof != 0)
    undefined identifier foo

But, I like the syntax.  I thought adding "defined" or something was 
bad, and I thought leaving it bare was ugly... typeof really makes it 
work.  If only it'd compile for me :).

May I suppose this is something changed for 0.128, then?

-[Unknown]


 "Unknown W. Brackets" <unknown simplemachines.org> wrote in message
 news:d93ka0$241e$3 digitaldaemon.com...
 
As I mentioned before (before static if), I think it would be very
useful to be able to check, with static if or similar, for the existence
of functions.

I don't mean linking.  I just mean checking if a function was defined
yet.  In its basic form, this doesn't seem that great:
You can already do it: static if (typeof(foo)) ... will be false if foo is not defined, and true if it is.
Jun 20 2005
parent "Walter" <newshound digitalmars.com> writes:
"Unknown W. Brackets" <unknown simplemachines.org> wrote in message
news:d9654v$l5k$1 digitaldaemon.com...
 Typeof, eh?  I noticed you could do &foo, but that of course fails if
 it's not defined with a compiler error.

 Is this documented?  As I said, it seems like a very useful thing to use
 in many cases.

 By the way: (found while checking again...)
 http://www.digitalmars.com/d/declaration.html#typeof

 printf("%d\n", typeof('c').size); // prints 1

 Should be:

 printf("%d\n", typeof('c').sizeof); // prints 1

 And:

 int[typeof[p]] a; // a is of type int[int*]

 Should be (?):

 int[typeof(p)] a; // a is of type int[int*]
Good catch, I'll fix it.
 Anyway, I can't get it to work.  I just get errors....

 static if (typeof(foo))
     found ')' when expecting '.' following 'typeof(foo)'

 static if (typeid(typeof(foo)))
     undefined identifier foo

 static if (typeof(foo).sizeof != 0)
     undefined identifier foo

 But, I like the syntax.  I thought adding "defined" or something was
 bad, and I thought leaving it bare was ugly... typeof really makes it
 work.  If only it'd compile for me :).
Arggh, I goofed. It should be: static if ( is(typeof(foo)) ) ...
Jun 25 2005