On Saturday, 21 May 2016 at 23:06:10 UTC, Ali Çehreli wrote:
On 05/21/2016 12:56 PM, captain_fid wrote:
>>> [...]
fail me
>>>       [...]
value); }
>>> [...]
have a
>> [...]
something.
> [...]
missing/forgetting/misunderstanding here.

opCast is for explicit type conversions. However, you seem to want implicit type conversions.

> [...]
solution.

vit's 'alias ... this' solution is it. Here is some for info:

  http://ddili.org/ders/d.en/alias_this.html

And here is another example if you want the 'bool' value to be calculated as opposed to being a member:

struct S {
    int x;
    int y;

    /* Implicit type conversion to 'bool'
     * (Because myBoolValue() returns 'bool'.) */
    alias myBoolValue this;

    bool myBoolValue() {
        return (x + y) == 7;    // Some special condition
    }
}

void main() {
    auto a = S(1, 2);
    auto b = S(3, 4);

    assert(!a);    // Calls a.myBoolValue()
    assert( b);    // Calls b.myBoolValue()
}

Ali

Thanks Ali (and again for making your work so available - I'm almost always re-reading). For some reason, I've missed 'alias this' and it is fascinating.

I'm off to retry these suggestions now...

Reply via email to