How to construct a struct that does not explicitly define a constructor
user1234
user1234 at 12.de
Sat Mar 1 03:13:55 UTC 2025
On Saturday, 1 March 2025 at 02:19:55 UTC, Meta wrote:
>
> Thanks, I forgot about that syntax. Another question I have is
> if there's a way to do this inline:
>
> ```d
> struct Test
> {
> int n;
> float f;
>
> static Test opCall(int n, float f)
> {
> //return {n, f}; Error
> //return Test {n, f}; Error
> //return {n: n, f: f}; Error
> //return Test {n: n, f: f}; Error
> }
> }
>
> void main()
> {
> auto test = Test(1, 2.0);
> assert(test.n == 1);
> assert(test.f == 2.0);
> }
> ```
As you've been said, that does not work ATM but that is something
that I expect to work from day one when D will have tuples
```d
struct Test
{
int n;
double f;
static Test opCall(int n, float f)
{
return (0,0.1);
}
}
```
because the tuple-expression `(0,0.1)` is explicitly convertible
to `Test`, based on the concept of "structural typing".
More information about the Digitalmars-d-learn
mailing list