Any way to automatically convert structs on return?

2024-08-01 Thread Emma via Digitalmars-d-learn
This code works: ```d struct None {} struct Option(T) { bool hasSome; T value; this(None) {} this(T v) { hasSome = true; value = v; } } Option!int a = 123;// automatically constructs an Option!int from a bare int Option!int b = None(); // same as abov

Re: Any way to automatically convert structs on return?

2024-08-01 Thread Emma via Digitalmars-d-learn
Thanks everyone for the replies! I just thought it was weird that implicit construction is seemingly supported when directly initialising a struct but not in any other case. I guess it's because it's clearly less ambiguous. On Thursday, 1 August 2024 at 13:07:09 UTC, Lance Bachmeier wrote: F

Member function passed through template alias only requiring `this` in certain conditions?

2018-07-19 Thread Emma via Digitalmars-d-learn
Hello, I’ve got this piece of code: --- import std.stdio; void test(alias fn1, alias fn2)() { fn1(); fn2(); } struct Foo { void foo() { test!(bar, baz); } void bar() {} void baz() {} } --- If I try to compile it, dmd complains, which I guess makes

Can't use is expression on nested type

2025-03-13 Thread Emma via Digitalmars-d-learn
Hello, I was doing some template nonsense and came across this weird issue. This compiles fine: ```d struct Foo(string S) {} bool foo(T)(T it) { static if (is(T == Foo!S, string S)) { return true; } else { return false; } } void main() { foo(Foo!"x"()); } ``` b