Re: Concepts like c++20 with specialized overload resolution.

2023-05-27 Thread H. S. Teoh via Digitalmars-d-learn
On Sat, May 27, 2023 at 05:49:27PM +, vushu via Digitalmars-d-learn wrote:
> On Saturday, 27 May 2023 at 16:38:43 UTC, Steven Schveighoffer wrote:
[...]
> > void make_lava(T)(ref T lava) if (hasMagma!T) {
> > lava.magma();
> > }
> > 
> > void make_lava(T)(ref T lava_thing) if (!hasMagma!T){
> > lava_thing.try_making_lava();
> > }
[...]
> I see thanks for the example :), I think this probably the closest
> equivalent i dlang.

You can also use static if inside the function, which will give you an
if-then-else structure:

void make_lava(T)(ref T lava) {
static if (hasMagma!T) {
lava.magma();
} else {
lava_thing.try_making_lava();
}
}


T

-- 
Written on the window of a clothing store: No shirt, no shoes, no service.


Re: Concepts like c++20 with specialized overload resolution.

2023-05-27 Thread vushu via Digitalmars-d-learn

On Saturday, 27 May 2023 at 18:41:47 UTC, ryuukk_ wrote:

On Saturday, 27 May 2023 at 17:49:27 UTC, vushu wrote:
On Saturday, 27 May 2023 at 16:38:43 UTC, Steven Schveighoffer 
wrote:

On 5/27/23 9:50 AM, vushu wrote:

On Saturday, 27 May 2023 at 13:42:29 UTC, Basile B. wrote:

[...]


Yes I know there is template constraint, but not with 
specialized overloading right?


so you need to use static if for checking if it hasmagma.


What is missing is an "else" thing.

So you have to repeat the constraint (as a negation) 
unfortunately.


e.g.:

```d
struct LavaMan {
  void magma() { writeln(" LavaMan is throwing LAVA"); }
}

struct FakeVulcano {
  void try_making_lava() { writeln(" Making fake lava"); }
};

void make_lava(T)(ref T lava) if (hasMagma!T) {
lava.magma();
}

void make_lava(T)(ref T lava_thing) if (!hasMagma!T){
lava_thing.try_making_lava();
}
```

-Steve



I see thanks for the example :), I think this probably the 
closest equivalent i dlang.


I feel like overload in that case make things harder to read

My example has less context switch, and hte logic is correctly 
understandable at first sight


Only one make_lava function


It depends this example is quite small and a `static if` is 
sufficient, if you have a lot of cases it would make sense to 
split thing up into overloaded functions.
imagine you are writing a library for handling vector or matrices 
that has a common
`add` function. I just want to know the equivalent thing in dlang 
vs c++.




Re: Concepts like c++20 with specialized overload resolution.

2023-05-27 Thread ryuukk_ via Digitalmars-d-learn

On Saturday, 27 May 2023 at 17:49:27 UTC, vushu wrote:
On Saturday, 27 May 2023 at 16:38:43 UTC, Steven Schveighoffer 
wrote:

On 5/27/23 9:50 AM, vushu wrote:

On Saturday, 27 May 2023 at 13:42:29 UTC, Basile B. wrote:

[...]


Yes I know there is template constraint, but not with 
specialized overloading right?


so you need to use static if for checking if it hasmagma.


What is missing is an "else" thing.

So you have to repeat the constraint (as a negation) 
unfortunately.


e.g.:

```d
struct LavaMan {
  void magma() { writeln(" LavaMan is throwing LAVA"); }
}

struct FakeVulcano {
  void try_making_lava() { writeln(" Making fake lava"); }
};

void make_lava(T)(ref T lava) if (hasMagma!T) {
lava.magma();
}

void make_lava(T)(ref T lava_thing) if (!hasMagma!T){
lava_thing.try_making_lava();
}
```

-Steve



I see thanks for the example :), I think this probably the 
closest equivalent i dlang.


I feel like overload in that case make things harder to read

My example has less context switch, and hte logic is correctly 
understandable at first sight


Only one make_lava function


Re: Concepts like c++20 with specialized overload resolution.

2023-05-27 Thread vushu via Digitalmars-d-learn
On Saturday, 27 May 2023 at 16:38:43 UTC, Steven Schveighoffer 
wrote:

On 5/27/23 9:50 AM, vushu wrote:

On Saturday, 27 May 2023 at 13:42:29 UTC, Basile B. wrote:

[...]


Yes I know there is template constraint, but not with 
specialized overloading right?


so you need to use static if for checking if it hasmagma.


What is missing is an "else" thing.

So you have to repeat the constraint (as a negation) 
unfortunately.


e.g.:

```d
struct LavaMan {
  void magma() { writeln(" LavaMan is throwing LAVA"); }
}

struct FakeVulcano {
  void try_making_lava() { writeln(" Making fake lava"); }
};

void make_lava(T)(ref T lava) if (hasMagma!T) {
lava.magma();
}

void make_lava(T)(ref T lava_thing) if (!hasMagma!T){
lava_thing.try_making_lava();
}
```

-Steve



I see thanks for the example :), I think this probably the 
closest equivalent i dlang.


Re: Concepts like c++20 with specialized overload resolution.

2023-05-27 Thread Steven Schveighoffer via Digitalmars-d-learn

On 5/27/23 9:50 AM, vushu wrote:

On Saturday, 27 May 2023 at 13:42:29 UTC, Basile B. wrote:

On Saturday, 27 May 2023 at 13:23:38 UTC, vushu wrote:

[...]
Is there something equivalent in dlang, doesn't seem like d support 
specialized overload?


D solution is called [template 
constraints](https://dlang.org/spec/template.html#template_constraints).


Yes I know there is template constraint, but not with specialized 
overloading right?


so you need to use static if for checking if it hasmagma.


What is missing is an "else" thing.

So you have to repeat the constraint (as a negation) unfortunately.

e.g.:

```d
struct LavaMan {
  void magma() { writeln(" LavaMan is throwing LAVA"); }
}

struct FakeVulcano {
  void try_making_lava() { writeln(" Making fake lava"); }
};

void make_lava(T)(ref T lava) if (hasMagma!T) {
lava.magma();
}

void make_lava(T)(ref T lava_thing) if (!hasMagma!T){
lava_thing.try_making_lava();
}
```

-Steve


Re: Concepts like c++20 with specialized overload resolution.

2023-05-27 Thread ryuukk_ via Digitalmars-d-learn

On Saturday, 27 May 2023 at 13:23:38 UTC, vushu wrote:

you can use: ``static if (__traits(hasMember, T, "magma"))``


```D
import std;

struct LavaMan {
  void magma() { writeln(" LavaMan is throwing LAVA"); }
}

struct FakeVulcano {
  void try_making_lava() { writeln( " Making fake lava"); }
}


// all the magic
void make_lava(T)(ref T lava_thing){
static if (__traits(hasMember, T, "magma"))
lava_thing.magma();
else
lava_thing.try_making_lava();
}

int main() {
  LavaMan v;
  FakeVulcano l;
  make_lava(l);
  make_lava(v);
  return 0;
}
```

```
 Making fake lava
 LavaMan is throwing LAVA
```


Re: Concepts like c++20 with specialized overload resolution.

2023-05-27 Thread vushu via Digitalmars-d-learn

On Saturday, 27 May 2023 at 13:42:29 UTC, Basile B. wrote:

On Saturday, 27 May 2023 at 13:23:38 UTC, vushu wrote:

[...]
Is there something equivalent in dlang, doesn't seem like d 
support specialized overload?


D solution is called [template 
constraints](https://dlang.org/spec/template.html#template_constraints).


Yes I know there is template constraint, but not with specialized 
overloading right?


so you need to use static if for checking if it hasmagma.


Re: Concepts like c++20 with specialized overload resolution.

2023-05-27 Thread Basile B. via Digitalmars-d-learn

On Saturday, 27 May 2023 at 13:23:38 UTC, vushu wrote:

[...]
Is there something equivalent in dlang, doesn't seem like d 
support specialized overload?


D solution is called [template 
constraints](https://dlang.org/spec/template.html#template_constraints).


Re: string to char[4] FourCC conversion

2023-05-27 Thread kdevel via Digitalmars-d-learn
On Friday, 26 May 2023 at 13:18:15 UTC, Steven Schveighoffer 
wrote:

[...]
This worked for me:

```d
char[4] fourC(string s)
{
if(s.length >= 4)
return s[0 .. 4];


Silent truncation? Non-ASCII chars?


char[4] res = 0;


According to [1], [2] or [3] that should read

```
char[4] res = ' ';
```


res[0 .. s.length] = s;
return res;
}
```


[1] Multimedia Programming Interface and Data Specifications 1.0
IBM Corporation and Microsoft Corporation
August 1991, p. 11

https://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/Docs/riffmci.pdf


[2] http://ffmpeg.org/doxygen/trunk/raw_8c_source.html#l00050

[3] https://en.wikipedia.org/wiki/FourCC
"The byte sequence is usually restricted to ASCII printable 
characters, with space characters reserved for padding shorter 
sequences. [...] Some FourCCs however, do contain non-printable 
characters, and are not human-readable without special formatting 
for display; for example, 10bit Y'CbCr 4:2:2 video can have a 
FourCC of ('Y', '3', 10, 10) which ffmpeg displays as rawvideo 
(Y3[10] [10] / 0x0A0A3359), yuv422p10le."




Concepts like c++20 with specialized overload resolution.

2023-05-27 Thread vushu via Digitalmars-d-learn

```
template 
concept HasMagma = requires(T l) { l.magma(); };

struct LavaMan {
  void magma() { std::cout << " LavaMan is throwing LAVA" << 
std::endl; }

};

struct FakeVulcano {
  void try_making_lava() { std::cout << " Making fake lava" << 
std::endl; }

};

void make_lava(HasMagma auto& lava) {
lava.magma();
}

void make_lava(auto& lava_thing){
lava_thing.try_making_lava();
}

int main() {
  LavaMan v;
  FakeVulcano l;
  make_lava(l);
  make_lava(v);
  return 0;
}
```

results in:
```
 Making fake lava
 LavaMan is throwing LAVA
```

Is there something equivalent in dlang, doesn't seem like d 
support specialized overload?




Re: Complicated @property access only works when I write an extra parenthesis "()"

2023-05-27 Thread realhet via Digitalmars-d-learn

It seems like I managed to solve it.

All the chain of properties now capturing a generic value type T.
And finally the most inner associative array will handle the 
implicit cast.
Maybe that extra implicit () got confused when the types are 
same, but the aliases to those types are different. o.O


Now both expressions are working.

   samples[key].mainPicture().width
   samples[key].mainPicture.width

Side effect: The internal chaos behind this also got rediced by 
25% as I had to refactor to find the root of the problem :D