www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Type wrapping blockers

reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
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
Oct 05 2010
next sibling parent reply Mafi <mafi example.org> writes:
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. Mafi
Oct 05 2010
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Tue, 05 Oct 2010 10:06:58 -0400, Mafi <mafi 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
Oct 05 2010
parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 10/5/10 9:15 CDT, Steven Schveighoffer wrote:
 On Tue, 05 Oct 2010 10:06:58 -0400, Mafi <mafi 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
Oct 05 2010
prev sibling next sibling parent reply Don <nospam nospam.com> writes:
Steven Schveighoffer wrote:
 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.
That is bug 4953.
Oct 05 2010
parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Tue, 05 Oct 2010 10:25:31 -0400, Don <nospam nospam.com> wrote:

 Steven Schveighoffer wrote:
 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.
That is bug 4953.
I don't think so, because I'm using IFTI to imply the argument types. A generic opDispatch to wrap a function: auto opDispatch(string op, Args...)(Args args) { /* perform interception work here */ return mixin!("wrappedVal." ~ op ~ "(args)"); } If you don't know what the arguments are *supposed* to be, I'm unsure how to derive them based on what the type being wrapped supports. The specific situation that's failing, in dcollections, I have a function: add(V[] elems...) where V is the type being collected. When V == short, then calling add directly like this: add(1,2,3,4,5); works fine. But calling the wrapper with add(1,2,3,4,5) fails, because Args... is interpreted as int, int, int, int, int. What I really need is to say "hey IFTI, determine the argument types by looking up as if it were called on this type, because that's what I'm going to call" rather than use the arguments themselves. Bug 4953 is when IFTI should play no part in determing the argument types. Or am I missing something? -Steve
Oct 05 2010
prev sibling next sibling parent "Simen kjaeraas" <simen.kjaras gmail.com> writes:
Steven Schveighoffer <schveiguy yahoo.com> wrote:

 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?
Not directly. However, you could probably coerce std.typecons' AutoImplement to do your wrapping for you. -- Simen
Oct 05 2010
prev sibling next sibling parent "Denis Koroskin" <2korden gmail.com> writes:
On Tue, 05 Oct 2010 16:50:04 +0400, Steven Schveighoffer  
<schveiguy yahoo.com> wrote:

 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
You can try iterating over all class methods and construct proxy ones using string mixins at compile time. Might be next to impossible to implement properly though :)
Oct 05 2010
prev sibling next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Tue, 05 Oct 2010 08:50:04 -0400, Steven Schveighoffer  
<schveiguy yahoo.com> wrote:

 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?
Found another one, really old bug: 1748 Basically, if you have class C(T) { } and you try to get (C!int).stringof you get just "C". This makes specifying wrapped arguments that are class templates impossible for mixins. I'm giving up my workaround to try and implement the "Nifty chaining", it appears it's just not possible with the compiler bugs today :( -Steve
Oct 05 2010
prev sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Tue, 05 Oct 2010 08:50:04 -0400, Steven Schveighoffer  
<schveiguy yahoo.com> wrote:

 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 ideas on how to fix the above issue?
I found a solution: http://d.puremagic.com/issues/show_bug.cgi?id=4998 Anyone see any holes in it? BTW, bug 5000 coming up! Big milestone :) -Steve
Oct 06 2010
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Wednesday 06 October 2010 04:26:43 Steven Schveighoffer wrote:
 BTW, bug 5000 coming up!  Big milestone :)
Though I'm not sure that 5000 bugs is exactly something to celebrate... - Jonathan M Davis
Oct 06 2010
next sibling parent reply "Lars T. Kyllingstad" <public kyllingen.NOSPAMnet> writes:
On Wed, 06 Oct 2010 05:24:18 -0700, Jonathan M Davis wrote:

 On Wednesday 06 October 2010 04:26:43 Steven Schveighoffer wrote:
 BTW, bug 5000 coming up!  Big milestone :)
Though I'm not sure that 5000 bugs is exactly something to celebrate... - Jonathan M Davis
That depends on how many are using D, and to which degree they are putting DMD+Phobos through its paces. I have a project on dsource that has zero bug reports, but I don't think it's because it's bug-free. ;) -Lars
Oct 06 2010
parent Lutger <lutger.blijdestijn gmail.com> writes:
Lars T. Kyllingstad wrote:

 On Wed, 06 Oct 2010 05:24:18 -0700, Jonathan M Davis wrote:
 
 On Wednesday 06 October 2010 04:26:43 Steven Schveighoffer wrote:
 BTW, bug 5000 coming up!  Big milestone :)
Though I'm not sure that 5000 bugs is exactly something to celebrate... - Jonathan M Davis
That depends on how many are using D, and to which degree they are putting DMD+Phobos through its paces. I have a project on dsource that has zero bug reports, but I don't think it's because it's bug-free. ;) -Lars
I agree. I always welcome the increase of list of bugs at work, my co-workers sometimes find that strange. But I figure they are in the codebase anyway, better to get them in plain sight.
Oct 09 2010
prev sibling parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 10/6/10 7:24 CDT, Jonathan M Davis wrote:
 On Wednesday 06 October 2010 04:26:43 Steven Schveighoffer wrote:
 BTW, bug 5000 coming up!  Big milestone :)
Though I'm not sure that 5000 bugs is exactly something to celebrate...
There are 1516 open issues at the time being, and the derivative is still positive. But there are 2086 closed bugs, and the derivative is also positive (see http://tinyurl.com/2ewh8eq). There is some celebration to be had. If there were no bugs activity then the product wouldn't be used. Andrei
Oct 06 2010
parent reply Juanjo Alvarez <fake fakeemail.com> writes:
On Wed, 06 Oct 2010 10:21:58 -0500, Andrei Alexandrescu 
<SeeWebsiteForEmail erdani.org> wrote:
 also positive (see http://tinyurl.com/2ewh8eq).
404 Not Found. Should I open a bug report? ;)
Oct 06 2010
parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 10/6/10 17:53 CDT, Juanjo Alvarez wrote:
 On Wed, 06 Oct 2010 10:21:58 -0500, Andrei Alexandrescu
 <SeeWebsiteForEmail erdani.org> wrote:
 also positive (see http://tinyurl.com/2ewh8eq).
404 Not Found. Should I open a bug report? ;)
Hm, it works for me but it asks me to log in first. Probably you need an account. Andrei
Oct 06 2010