Recommended coding convention for combining unix and windows code ?

wobbles via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jun 7 11:17:04 PDT 2016


On Tuesday, 7 June 2016 at 15:33:57 UTC, chmike wrote:
> Hello
>
> I'm writing some code that I want to be portable across Posix 
> and Windows.
>
> What is the recommended code convention for such type of code ?
>
> 80% of the class implementation is the same for both OS.
>
> Should I write the following and copy past the 80%
>
> version( Windows ) {
>     import core.sys.windows;
>     class MyInfo {...}
> } else version( Posix ) {
>     import core.sys.posix;
>     class MyInfo {...}
> } else {
>     static assert(false, "Unsupported platform");
> }
>
> or should I do it the C way with multiple embedded static if... 
> ?
>
> version( Windows ) {
>     import core.sys.windows;
> } else {  //Posix
>     import core.sys.posix;
> }
>
> class MyInfo {
>     ...
>     static if(windows) {
>     enum Value {...}
>     } static else {  //Posix
>     enum Value {...}
>     }
>     ...
> }

I did something similar, and went with the c-style version (x) 
inside each code block.

Have a look here for how it looks.
https://github.com/grogancolin/dexpect/blob/master/source/dexpect.d



More information about the Digitalmars-d-learn mailing list