wrapping and delegating to members of a type

145 views
Skip to first unread message

Kevin Squire

unread,
Mar 22, 2013, 1:32:22 PM3/22/13
to juli...@googlegroups.com
Hi, this has come up before, but I can't seem to find it.

I'm looking to create a subtype of AbstractArray which contains as members a vector and one other element.  

type MyArray <: AbstractArray
   x
::Vector
   d
end

I'm looking for lazy way to delegate almost all array functions to MyArray.x, without typing out each one.  I believe Doug Bates or maybe Toivo suggested a macro to do this previously--something like

@delegate MyArray x getindex setindex! delete! ...

but I can't seem to find it (or maybe it was just a suggestion, with no code?).  

I'm sure I could think it through, but if anyone has any code that does this or suggestions on a better way to deal with it, that would be great.

Cheers!

   Kevin

John Myles White

unread,
Mar 22, 2013, 1:40:24 PM3/22/13
to juli...@googlegroups.com
This is a suggestion I made long ago that Patrick, Toivo and Stefan suggested generalizing substantially. Unfortunately I didn't write very much code for it when I first demo'ed it out. Because it keeps coming up, I'll work on something today. It would be good to at least start an RFC that we can all work on. Toivo had a lot of ideas, but they seemed substantially more complex than what I was looking for.

 -- John

Kevin Squire

unread,
Mar 22, 2013, 1:48:54 PM3/22/13
to juli...@googlegroups.com
Perfect, John!.  Is any of the previous conversation on the mailing list or github?

Kevin

John Myles White

unread,
Mar 22, 2013, 1:49:38 PM3/22/13
to juli...@googlegroups.com

Kevin Squire

unread,
Mar 22, 2013, 1:53:18 PM3/22/13
to juli...@googlegroups.com
Thanks!

Toivo Henningsson

unread,
Mar 22, 2013, 3:33:03 PM3/22/13
to juli...@googlegroups.com
On Friday, 22 March 2013 18:40:24 UTC+1, John Myles White wrote:
This is a suggestion I made long ago that Patrick, Toivo and Stefan suggested generalizing substantially. Unfortunately I didn't write very much code for it when I first demo'ed it out. Because it keeps coming up, I'll work on something today. It would be good to at least start an RFC that we can all work on. Toivo had a lot of ideas, but they seemed substantially more complex than what I was looking for.

Yeah, I can't take credit for the idea.

Anyway, what I'm most interested in is a syntax for delegation that is transparent. So far, I don't think there's any getting around specifying each function that you want to delegate by name. I guess that the simplest way would be by @eval and for loops. Toy example:

    julia> type T
               x
           
end

    julia
> import Base.sin, Base.cos

    julia
> for f in (:+, :- )    # delegate binary + and - to T.x
               
@eval $f(a::T, b::T) = $f(a.x, b.x)
           
end

    julia
> for f in (:sin, :cos) # delegate sin and cos
               
@eval $f(a::T) = $f(a.x)
           
end

    julia
> T(2)
    T
(2)

    julia
> T(2) + T(4)
   
6

    julia
> sin(T(3.14))
   
0.0015926529164868282

Most things that we could come up with would be fancier ways to write this kind of thing.
One thing to consider is that it seems hard to do this with a macro without eval, at least if the list of functions to delegate to isn't listed literally in the macro arguments. Not sure if that's a problem, maybe there's a good reason to break the no-eval-in-macro rule in this case. (Strictly speaking, the macro could emit a call to eval instead of calling it at expansion time, but then I guess it should be pretty clear from the macro name that it does that.)

John Myles White

unread,
Mar 22, 2013, 6:55:56 PM3/22/13
to juli...@googlegroups.com
For the simple case of delegating functions with only a single argument, I wrote up something that it would be good to get feedback on before I make a full-out RFC:


For functions with more than one arguments I don't currently see anything better than Toivo's approach.

 -- John

Kevin Squire

unread,
Mar 31, 2013, 11:27:17 PM3/31/13
to juli...@googlegroups.com
It looks like I'm the only one who responded.  Want to turn this into a pull request?  It might move forward then.

Cheers,

   Kevin
Reply all
Reply to author
Forward
0 new messages