restrict alias this?
Namespace
rswhite4 at googlemail.com
Sun Apr 29 05:28:05 PDT 2012
On Sunday, 29 April 2012 at 11:24:00 UTC, Simen Kjaeraas wrote:
> On Sun, 29 Apr 2012 10:14:09 +0200, Namespace
> <rswhite4 at googlemail.com> wrote:
>
>> Based on my previous thread
>> (http://forum.dlang.org/thread/rpcqefmyqigftxsgsqui@forum.dlang.org),
>> I got the question whether it is possible to restrict "alias
>> this"?
>> Similar to templates function, e.g. "void foo(T)(T value) if
>> (isNumeric!(T) ...", but it should be limited here so that it
>> does not interact with the cast or something other.
>> As far as I know, "alias this" is an implicit cast. And just
>> like an explicit cast (opCast) it should be possible to
>> restrict it.
>>
>> Otherwise, I have unfortunately run out of ideas how I can
>> allow an implicit conversion.
>
> What do you mean restrict it?
I mean that you can limit it similar as you can with templates.
In my code i'm using "alias this" to enable an implicit
conversion from any class, e.g. Foo, to Ref!Foo. Ref is an struct
which checks if the value of Foo is null. If it's null a error
message will let you know, that you have a problem with one of
your objects (i hate the access violation message, that's the
original reason for that).
But if i cast with a class, that implements the implicit
conversion, as in the following:
[code]
class A {
mixin TRef!(A);
}
class B : A { }
A a1 = new B();
B b1 = cast(B) a1.access; // <- problem
[/code]
the cast will implicit convert with "alias this" to Ref!B. And
that isn't what i want and neither what should happen. In this
case a limit to "alias this" would be great. A limitation, that
"alias this" must not convert the cast to Ref!B.
More information about the Digitalmars-d-learn
mailing list