Type wrapping blockers
Andrei Alexandrescu
SeeWebsiteForEmail at erdani.org
Tue Oct 5 09:17:00 PDT 2010
On 10/5/10 9:15 CDT, Steven Schveighoffer wrote:
> On Tue, 05 Oct 2010 10:06:58 -0400, Mafi <mafi at example.org> wrote:
>
>> Am 05.10.2010 14:50, schrieb Steven Schveighoffer:
>>> One of the good goals of D I think is to be able to automatically
>>> encapsulate a type, and all its attributes/functions, in order to
>>> slightly alter the functionality. I think this is probably a pattern,
>>> but I don't know what it is (Interceptor?).
>>>
>>> One of the best methods to wrap a type is to use opDispatch. But there
>>> are some problems that block this. It might be good to get a bug report
>>> that gathers these together. I have one that I just ran into -- IFTI and
>>> literals. Basically, if you have a function:
>>>
>>> void foo(short x);
>>>
>>> you can call foo(1) no problem.
>>>
>>> But if you *wrap* the type that contains foo, you cannot use opDispatch
>>> to implement foo(1), because IFTI treats 1 as an int. So what you get is
>>> an instantiation of opDispatch like this:
>>>
>>> opDispatch!("foo", int)(1) Which then cannot call foo, because you
>>> cannot cast int to short.
>>>
>>> Does anyone have any other blockers that prevent type wrapping? Does
>>> anyone have any ideas on how to fix the above issue?
>>>
>>> -Steve
>> Iff you are only wrapping one other thing (ie struct, class...), you
>> should be able to use alias this. It should in my opinion correctly
>> resolve to foo(short). If dmd doesn't find foo in your wrapper it
>> should look for foo in the alias before saying that 1 has to be int.
>> But I didn't test.
>
> Alias this does not allow interception of calls.
>
> I want to do something inside opDispatch besides just calling foo(1).
>
> -Steve
If you only want to intercept a few specific calls, you may want to use
alias this and simply define the appropriate methods. They will be found
by name lookup before alias this kicks in.
For intercepting all calls even to method names you don't know, indeed
opDispatch is the way to go.
Andrei
More information about the Digitalmars-d
mailing list