Re: opCast cannot implicitly convert a.opCast of type X to Y

2018-02-14 Thread aliak via Digitalmars-d-learn
On Thursday, 15 February 2018 at 00:34:33 UTC, Meta wrote: On Thursday, 15 February 2018 at 00:27:40 UTC, Meta wrote: On Wednesday, 14 February 2018 at 23:46:30 UTC, aliak wrote: On Wednesday, 14 February 2018 at 15:14:24 UTC, Meta wrote: Ooh yes, of course! Thank you :) Even better: import

Re: opCast cannot implicitly convert a.opCast of type X to Y

2018-02-14 Thread Meta via Digitalmars-d-learn
On Thursday, 15 February 2018 at 00:27:40 UTC, Meta wrote: On Wednesday, 14 February 2018 at 23:46:30 UTC, aliak wrote: On Wednesday, 14 February 2018 at 15:14:24 UTC, Meta wrote: Ooh yes, of course! Thank you :) Even better: import std.conv; auto b = a.map!(to!float); Actually, that won'

Re: opCast cannot implicitly convert a.opCast of type X to Y

2018-02-14 Thread Meta via Digitalmars-d-learn
On Wednesday, 14 February 2018 at 23:46:30 UTC, aliak wrote: On Wednesday, 14 February 2018 at 15:14:24 UTC, Meta wrote: I think the best way to do this is to implement `map` for your optional type. Optional!U map(U, alias f)() { return empty? no!U : some!U(f(t)); } Optional!int a = 3;

Re: opCast cannot implicitly convert a.opCast of type X to Y

2018-02-14 Thread aliak via Digitalmars-d-learn
On Wednesday, 14 February 2018 at 15:14:24 UTC, Meta wrote: I think the best way to do this is to implement `map` for your optional type. Optional!U map(U, alias f)() { return empty? no!U : some!U(f(t)); } Optional!int a = 3; auto b = a.map!(v => cast(float)v); assert(is(typeof(b) == Opt

Re: opCast cannot implicitly convert a.opCast of type X to Y

2018-02-14 Thread Meta via Digitalmars-d-learn
On Monday, 12 February 2018 at 02:05:16 UTC, aliak wrote: From spec: Cast expression: "cast ( Type ) UnaryExpression" converts UnaryExpresssion to Type. And https://dlang.org/spec/operatoroverloading.html#cast makes no mention of the return type of opCast. One could think that the return type

Re: opCast cannot implicitly convert a.opCast of type X to Y

2018-02-13 Thread aliak via Digitalmars-d-learn
On Tuesday, 13 February 2018 at 12:12:30 UTC, Nathan S. wrote: On Monday, 12 February 2018 at 02:05:16 UTC, aliak wrote: struct B(T) { T t; } struct A(T) { T t; auto opCast(U)() { return B!U(cast(U)t); } } void main() { auto a = A!int(3); auto b = cast(float)a;

Re: opCast cannot implicitly convert a.opCast of type X to Y

2018-02-13 Thread Nathan S. via Digitalmars-d-learn
On Monday, 12 February 2018 at 02:05:16 UTC, aliak wrote: struct B(T) { T t; } struct A(T) { T t; auto opCast(U)() { return B!U(cast(U)t); } } void main() { auto a = A!int(3); auto b = cast(float)a; // error } Having the result of "cast(float) a" not be a float

Re: opCast cannot implicitly convert a.opCast of type X to Y

2018-02-12 Thread rumbu via Digitalmars-d-learn
On Monday, 12 February 2018 at 02:05:16 UTC, aliak wrote: From spec: Cast expression: "cast ( Type ) UnaryExpression" converts UnaryExpresssion to Type. And https://dlang.org/spec/operatoroverloading.html#cast makes no mention of the return type of opCast. One could think that the return type