www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - version as an expression?

reply ryuukk_ <ryuukk.dev gmail.com> writes:
Currently, i am doing this,( wich sucks

```D
version(Windows)
     alias socket_t = size_t;
else
     alias socket_t = int;
```

I'd prefer to be able to do:

```D
alias socket_t = version(Windows) size_t else int;
```

And better, if we had pattern matching and some builtin 
information available at compile time:


```D
alias socket_t = switch (builtin.target.os) {
     .windows => size_t,
     else     => int
};
```


Why i hate the current way of doing it, but i feel it'll be 
available in a distant future unfortunately:


- nothing will tell me if i forgot to implement an alias for a 
target until i compile for that target
- i repeat the name of the alias N times
Sep 08 2022
parent reply TheGag96 <thegag96 gmail.com> writes:
On Thursday, 8 September 2022 at 21:15:18 UTC, ryuukk_ wrote:
 (snip)
Well, for now, there's at least [this hack](https://forum.dlang.org/post/mailman.2048.1462983185.26339.digitalm rs-d puremagic.com) using a `struct` with a `static` `opDispatch`. I [use it](https://github.com/TheGag96/sm64-port/blob/dlang/src/dsrc/util.d#L16-L20) in [some places](https://github.com/TheGag96/sm64-port/blob/dlang/src/ds c/config.d#L16-L27) in a project of mine.
Sep 10 2022
parent reply ryuukk_ <ryuukk.dev gmail.com> writes:
On Sunday, 11 September 2022 at 05:38:44 UTC, TheGag96 wrote:
 On Thursday, 8 September 2022 at 21:15:18 UTC, ryuukk_ wrote:
 (snip)
Well, for now, there's at least [this hack](https://forum.dlang.org/post/mailman.2048.1462983185.26339.digitalm rs-d puremagic.com) using a `struct` with a `static` `opDispatch`. I [use it](https://github.com/TheGag96/sm64-port/blob/dlang/src/dsrc/util.d#L16-L20) in [some places](https://github.com/TheGag96/sm64-port/blob/dlang/src/ds c/config.d#L16-L27) in a project of mine.
It doesn't work with alias
Sep 11 2022
next sibling parent ryuukk_ <ryuukk.dev gmail.com> writes:
Your trick is very clever btw, thanks for sharing!
Sep 11 2022
prev sibling parent reply Nick Treleaven <nick geany.org> writes:
On Sunday, 11 September 2022 at 21:38:51 UTC, ryuukk_ wrote:
 On Sunday, 11 September 2022 at 05:38:44 UTC, TheGag96 wrote:
 On Thursday, 8 September 2022 at 21:15:18 UTC, ryuukk_ wrote:
 (snip)
Well, for now, there's at least [this hack](https://forum.dlang.org/post/mailman.2048.1462983185.26339.digitalm rs-d puremagic.com) using a `struct` with a `static` `opDispatch`. I [use it](https://github.com/TheGag96/sm64-port/blob/dlang/src/dsrc/util.d#L16-L20) in [some places](https://github.com/TheGag96/sm64-port/blob/dlang/src/ds c/config.d#L16-L27) in a project of mine.
It doesn't work with alias
```d import std.traits; alias A = Select!(isVersion("Windows"), WinType, PosixType); ```
Sep 12 2022
parent reply ryuukk_ <ryuukk.dev gmail.com> writes:
On Monday, 12 September 2022 at 10:02:48 UTC, Nick Treleaven 
wrote:
 On Sunday, 11 September 2022 at 21:38:51 UTC, ryuukk_ wrote:
 On Sunday, 11 September 2022 at 05:38:44 UTC, TheGag96 wrote:
 On Thursday, 8 September 2022 at 21:15:18 UTC, ryuukk_ wrote:
 (snip)
Well, for now, there's at least [this hack](https://forum.dlang.org/post/mailman.2048.1462983185.26339.digitalm rs-d puremagic.com) using a `struct` with a `static` `opDispatch`. I [use it](https://github.com/TheGag96/sm64-port/blob/dlang/src/dsrc/util.d#L16-L20) in [some places](https://github.com/TheGag96/sm64-port/blob/dlang/src/ds c/config.d#L16-L27) in a project of mine.
It doesn't work with alias
```d import std.traits; alias A = Select!(isVersion("Windows"), WinType, PosixType); ```
Sorry but i refuse to touch phobos, specially std.traits, i want my compile to remain fast
Sep 12 2022
parent reply Tejas <notrealemail gmail.com> writes:
On Monday, 12 September 2022 at 10:26:40 UTC, ryuukk_ wrote:
 On Monday, 12 September 2022 at 10:02:48 UTC, Nick Treleaven 
 wrote:
 On Sunday, 11 September 2022 at 21:38:51 UTC, ryuukk_ wrote:
 On Sunday, 11 September 2022 at 05:38:44 UTC, TheGag96 wrote:
 [...]
It doesn't work with alias
```d import std.traits; alias A = Select!(isVersion("Windows"), WinType, PosixType); ```
Sorry but i refuse to touch phobos, specially std.traits, i want my compile to remain fast
Weren't you copy pasting stuff that you needed? Why can't you do that with this as well?
Sep 12 2022
parent reply ryuukk_ <ryuukk.dev gmail.com> writes:
On Monday, 12 September 2022 at 10:39:20 UTC, Tejas wrote:
 On Monday, 12 September 2022 at 10:26:40 UTC, ryuukk_ wrote:
 On Monday, 12 September 2022 at 10:02:48 UTC, Nick Treleaven 
 wrote:
 On Sunday, 11 September 2022 at 21:38:51 UTC, ryuukk_ wrote:
 On Sunday, 11 September 2022 at 05:38:44 UTC, TheGag96 wrote:
 [...]
It doesn't work with alias
```d import std.traits; alias A = Select!(isVersion("Windows"), WinType, PosixType); ```
Sorry but i refuse to touch phobos, specially std.traits, i want my compile to remain fast
Weren't you copy pasting stuff that you needed? Why can't you do that with this as well?
My initial request was: "use version as an expression" We are bike-shedding to a point where "just use a template from std bro" is what people are telling me to do instead Where will i put all the files i copy, do i have to do that for every projects i will create You can understand that that's not the path i wanted to take when i created this post, i expect language improvement Same story with the .Enum thing i proposed few months ago, if i made it it's not to accept having to use mixing everywhere bellow my enums bloating the global scope I loose hope every posts
Sep 12 2022
parent ryuukk_ <ryuukk.dev gmail.com> writes:
I remember what one person told one not long ago: "stop using 
other languages as an example, just use that if that's what you 
want"

I'm forced again, let's look at other languages, zig for example:


```
const socket_t = switch (builtin.os.tag) {
     .windows => usize,
     else     => i32
};
```

caveat: it require importing "builtin", but it's very small, and 
doesn't have any other inter module dependency, it's just data, 
evaluated at compile time:


Will we stick to what C did 50 years ago, or we are ready to move 
on and improve the language beyond what C can offer? (no, 'std' 
doesn't count as improving the language)

I don't particularly like zig, but if D is stuck in the past and 
we can't expect improvements, and all the solutions will be on 
phobos instead, then i'll find it as dreadful to use as zig, and 
at this point using zig would feel better

"Why do you hate phobos this much?"

It's not that i hate it, it serve a purpose, offer a set of API 
ready to use

The problem is i target esoteric platforms that the language 
doesn't want to acknowledge (WASM), so i can't use it, that's as 
simple as that

And the performance profile is not aligned with what i expect, so 
i can't use it because of that too

I am also against Exception Handling, so i can't use it for that 
too

I want to control the memory usage, so i can't use it for that too

This is why i expect D to be usable, and to receive improvements 
without having to subscribe to Phobos, wich i don't want to do, 
hence my suggestions
Sep 12 2022