On 2018-07-23 20:39, aliak wrote:
Hi,

I'm playing around with an Optional wrapper type. It stores a type T and a bool that defines whether a value is defined or not:

struct Optional(T) {
   T value;
   bool defined = false;
   this(U : T)(auto ref inout(U) value) inout {
     this.value = value;
     this.defined = true;
   }
}

To facilitate it's use I have two type constructors:

inout(Optional!T) some(T)(auto ref inout(T) value) {
     return inout(Optional!T)(value);
}

Optional!T no(T)() {
     return Optional!T();
}

The above produces a problem when working with strings. Basically the type information gets slightly altered so you can't do this:

auto a = [no!string, some("hello")];

You get a type mismatch:

* no!string = Optional!string
* some("hello") = immutable(Optional!(char[]))

I've created a short code gist, so basically I'm wondering how to get it to compile without changing what's in main()

https://run.dlang.io/is/BreNdZ

I guess I can specialize on string type T, but this is a more general problem that can be shown with:

struct S {}
alias Thing = immutable S;
Thing thing = S();

auto x = some(thing);
auto y = no!Thing;
auto arr = [x, y]; // no can do buddy

This [1] compiles the first example but not the second.

[1] https://run.dlang.io/is/SJ02kP

--
/Jacob Carlborg

Reply via email to