Question about version ( ) keyword

Chris Wright via Digitalmars-d digitalmars-d at puremagic.com
Tue Mar 22 12:24:50 PDT 2016


On Mon, 21 Mar 2016 15:01:27 +0000, Adam D. Ruppe wrote:

> On Monday, 21 March 2016 at 14:51:48 UTC, Vincent R wrote:
>> However I understand that this limitation allow a clear separation
>> between different platforms...
> 
> That's exactly why it is done this way, so the platforms are clearly
> separated from each other and always grouped together for themselves.
> 
> When writing these, we try to copy/paste it from the specific platform's
> documentation as a whole block

That works where you are importing platform APIs. version(Posix) makes 
that a bit easier in some cases. And the general tactic is to wrap those 
APIs to be not platform-specific so you can omit version statements 
almost everywhere.

But if you still find that you have things that need to be versioned out 
but much  of the code is identical, another option is to use mixins:

template DeclarationsForiOSAndAndroid()
{
    extern(C) void foo();
    extern(C) int bar(int i);
}

version (iOS)
{
    mixin DeclarationsForiOSAndAndroid;
    extern(C) int iOSVersion();
}
version (Android)
{
    mixin DeclarationsForiOSAndAndroid;
    extern(C) int androidVersion();
}

There will rarely be a reason to do this, but you might find it handy 
some day.


More information about the Digitalmars-d mailing list