Question about version ( ) keyword
Vincent R via Digitalmars-d
digitalmars-d at puremagic.com
Mon Mar 21 07:51:48 PDT 2016
Hi,
When looking at core definitions like core.sys.posix.pthread I
can see some "duplicated" code because it seems version doesn't
support Or as we would do in C/C++ with #ifdefined.
For instance if can read this:
version( CRuntime_Glibc )
{
enum PTHREAD_BARRIER_SERIAL_THREAD = -1;
int pthread_barrier_destroy(pthread_barrier_t*);
int pthread_barrier_init(pthread_barrier_t*, in
pthread_barrierattr_t*, uint);
int pthread_barrier_wait(pthread_barrier_t*);
int pthread_barrierattr_destroy(pthread_barrierattr_t*);
int pthread_barrierattr_getpshared(in pthread_barrierattr_t*,
int*);
int pthread_barrierattr_init(pthread_barrierattr_t*);
int pthread_barrierattr_setpshared(pthread_barrierattr_t*,
int);
}
else version( FreeBSD )
{
enum PTHREAD_BARRIER_SERIAL_THREAD = -1;
int pthread_barrier_destroy(pthread_barrier_t*);
int pthread_barrier_init(pthread_barrier_t*, in
pthread_barrierattr_t*, uint);
int pthread_barrier_wait(pthread_barrier_t*);
int pthread_barrierattr_destroy(pthread_barrierattr_t*);
int pthread_barrierattr_getpshared(in pthread_barrierattr_t*,
int*);
int pthread_barrierattr_init(pthread_barrierattr_t*);
int pthread_barrierattr_setpshared(pthread_barrierattr_t*,
int);
}
else version (OSX)
{
}
else version (Solaris)
{
enum PTHREAD_BARRIER_SERIAL_THREAD = -2;
int pthread_barrier_destroy(pthread_barrier_t*);
int pthread_barrier_init(pthread_barrier_t*, in
pthread_barrierattr_t*, uint);
int pthread_barrier_wait(pthread_barrier_t*);
int pthread_barrierattr_destroy(pthread_barrierattr_t*);
int pthread_barrierattr_getpshared(in pthread_barrierattr_t*,
int*);
int pthread_barrierattr_init(pthread_barrierattr_t*);
int pthread_barrierattr_setpshared(pthread_barrierattr_t*,
int);
}
else version (CRuntime_Bionic)
{
}
else
{
static assert(false, "Unsupported platform");
}
When I see this code I cannot help thinking of something like:
version( CRuntime_Glibc ) || version( FreeBSD ) || version
(Solaris)
{
if(version(Solaris))
enum PTHREAD_BARRIER_SERIAL_THREAD = -2;
else
enum PTHREAD_BARRIER_SERIAL_THREAD = -1;
int pthread_barrier_destroy(pthread_barrier_t*);
int pthread_barrier_init(pthread_barrier_t*, in
pthread_barrierattr_t*, uint);
int pthread_barrier_wait(pthread_barrier_t*);
int pthread_barrierattr_destroy(pthread_barrierattr_t*);
int pthread_barrierattr_getpshared(in pthread_barrierattr_t*,
int*);
int pthread_barrierattr_init(pthread_barrierattr_t*);
int pthread_barrierattr_setpshared(pthread_barrierattr_t*,
int);
}
else version (OSX) ||version (CRuntime_Bionic)
{
}
else
{
static assert(false, "Unsupported platform");
}
I suppose language creators had good reasons to not allow it but
I am a bit disapointed by the aspect of this language because
some files are really verbose.
However I understand that this limitation allow a clear
separation between different platforms...
More information about the Digitalmars-d
mailing list