[Ironruby-core] A nicer syntax for generic extension methods

Tomas Matousek Tomas.Matousek at microsoft.com
Wed Feb 3 12:12:54 EST 2010


The LINQ experience should get better as soon as we improve our interaction with the type inference engine in DLR overload resolver. IronRuby currently doesn’t use its full power like IronPython does, so you need to explicitly specify more generic arguments that should be necessary.

Tomas

From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Ivan Porto Carrero
Sent: Wednesday, February 03, 2010 8:49 AM
To: ironruby-core at rubyforge.org
Subject: Re: [Ironruby-core] A nicer syntax for generic extension methods

For lightspeed I wrote an internal dsl that allows you to query similarly to the regular api but no special module for linq stuff. I skipped linq altogether didn't need it there.

so what I created there is (It's in the asp.net<http://asp.net> mvc chapter btw)

find_all User do
   where a(:name).like("ivan%") & a(:age) > 23
   order_by :name.desc
end

that may be wrong but it's close to something like that..
---
Met vriendelijke groeten - Best regards - Salutations
Ivan Porto Carrero
Blog: http://flanders.co.nz
Twitter: http://twitter.com/casualjim
Author of IronRuby in Action (http://manning.com/carrero)


On Wed, Feb 3, 2010 at 4:01 PM, Ryan Riley <ryan.riley at panesofglass.org<mailto:ryan.riley at panesofglass.org>> wrote:
Thanks, Ivan. That's awesome ... that's just like F#. I should have realized it would be that simple. I'll post this to the Delegates section of the .NET interop page on the wiki, since it currently doesn't exist.

Also, I noticed you alluded to something similar in IronRuby in Action where you talk about LightSpeed, but I couldn't find anything in the MEAP copy I have. If I am able to spin up a few LINQ samples (probably Rx, Pfx, and/or XLinq), I'll shoot them your way, if you are interested.

Cheers!

Ryan Riley

Email: ryan.riley at panesofglass.org<mailto:ryan.riley at panesofglass.org>
LinkedIn: http://www.linkedin.com/in/ryanriley
Blog: http://wizardsofsmart.net/
Twitter: @panesofglass
Website: http://panesofglass.org/

On Tue, Feb 2, 2010 at 11:40 PM, Ivan Porto Carrero <ivan at whiterabbitconsulting.eu<mailto:ivan at whiterabbitconsulting.eu>> wrote:
just pass your block to the constructor of a delegate and you should be good to go

Action.new { more_work_here }
---
Met vriendelijke groeten - Best regards - Salutations
Ivan Porto Carrero
Blog: http://flanders.co.nz
Twitter: http://twitter.com/casualjim
Author of IronRuby in Action (http://manning.com/carrero)



On Tue, Feb 2, 2010 at 11:38 PM, Ryan Riley <ryan.riley at panesofglass.org<mailto:ryan.riley at panesofglass.org>> wrote:
That's fantastic, Tomas, thanks! Is there any way to pass a block, lambda, or Proc into the slot for the delegate, or perhaps a way to create a .NET delegate (or Expression) from a block, lambda, or Proc?

Thanks,

Ryan Riley

Email: ryan.riley at panesofglass.org<mailto:ryan.riley at panesofglass.org>
LinkedIn: http://www.linkedin.com/in/ryanriley
Blog: http://wizardsofsmart.net/
Twitter: @panesofglass
Website: http://panesofglass.org/

On Tue, Feb 2, 2010 at 1:36 AM, Tomas Matousek <Tomas.Matousek at microsoft.com<mailto:Tomas.Matousek at microsoft.com>> wrote:
Actually, you can add Ruby methods to List<T> … IronRuby type system does some magic for you ☺:

>>> include System::Collections::Generic
=> Object
>>> List[Fixnum].included_modules
=> [System::Collections::Generic::List[T], System::Collections::Generic::IList[Fixnum], System::Collections::Generic::IList[T], System::Collections::Generic::ICollection[Fixnum], System::Collections::Generic::ICollection[T], System::Collections::Generic::IEnumerable[Fixnum], System::Collections::Generic::IEnumerable[T], System::Collections::IEnumerable, Enumerable, System::Collections::IList, System::Collections::ICollection, System::Collections::Generic, Kernel]

As you can see the List<> generic type definition is treated as a module that is mixed in each of its instantiations. Although there are no predefined methods on it you can open it and add some. First we need to get Ruby class for List<T>. If you index System.Collections.Generic.List by a fixnum instead of a class/module you’ll get the generic definition of arity 1. Let’s name it ListOfT:

>>> ListOfT = List[1]

And then we can open it up:

>>> module ListOfT
...   def size
...     count
...   end
... end
=> nil
>>> l = List[Fixnum].new
=> []
>>> l.add(1)
=> nil
>>> l.add(2)
=> nil
>>> l.size
=> 2

Tomas


From: ironruby-core-bounces at rubyforge.org<mailto:ironruby-core-bounces at rubyforge.org> [mailto:ironruby-core-bounces at rubyforge.org<mailto:ironruby-core-bounces at rubyforge.org>] On Behalf Of Orion Edwards
Sent: Monday, February 01, 2010 6:31 PM
To: ironruby-core at rubyforge.org<mailto:ironruby-core at rubyforge.org>
Subject: Re: [Ironruby-core] A nicer syntax for generic extension methods

IIRC you can open "concrete" generics, but not "open" ones: In plain english this means you can add methods to List<string> but not List<T>.

This is essentially because List<T> isn't a real type in the CLR, it's basically some metadata that can be used to build a real type when the T is supplied.

You could as an alternative add methods to the underlying non-generic IEnumerable interface, but then you'd have to do some run-time reflection to figure out that your List is actually a List<string>... This is probably not nice.

In theory when CLR4 lands and has support for co/contra variant generics, List<object> should match List<string> and everything else, but I don't know if IronRuby would also work for this?

Good luck
On Tue, Feb 2, 2010 at 7:52 AM, Ryan Riley <ryan.riley at panesofglass.org<mailto:ryan.riley at panesofglass.org>> wrote:
I have been trying to figure out how to Rubify generic extension methods for use with the likes of Rx, Open XML SDK, etc. Ivan went over it a bit with me this weekend, but I'm still having difficulty including a module within a .NET type. Is that even possible?

...

The questions I'm not able to answer are:

  1.  Can I somehow open up a .NET class, say System::Collections::Generic::List[T] and include the EnumerableExtensions? So far, I'm finding that's a no.
  2.  How do I hook in the included(base) method above? I'm assuming that's a one-time call, but I don't see anywhere that it's called when a module is included. Do I need to use a before_filter or perform that action at the beginning of the linq_select method?

Thanks!

Ryan Riley

Email: ryan.riley at panesofglass.org<mailto:ryan.riley at panesofglass.org>
LinkedIn: http://www.linkedin.com/in/ryanriley
Blog: http://wizardsofsmart.net/
Twitter: @panesofglass
Website: http://panesofglass.org/

_______________________________________________
Ironruby-core mailing list
Ironruby-core at rubyforge.org<mailto:Ironruby-core at rubyforge.org>
http://rubyforge.org/mailman/listinfo/ironruby-core


_______________________________________________
Ironruby-core mailing list
Ironruby-core at rubyforge.org<mailto:Ironruby-core at rubyforge.org>
http://rubyforge.org/mailman/listinfo/ironruby-core


_______________________________________________
Ironruby-core mailing list
Ironruby-core at rubyforge.org<mailto:Ironruby-core at rubyforge.org>
http://rubyforge.org/mailman/listinfo/ironruby-core


_______________________________________________
Ironruby-core mailing list
Ironruby-core at rubyforge.org<mailto:Ironruby-core at rubyforge.org>
http://rubyforge.org/mailman/listinfo/ironruby-core


_______________________________________________
Ironruby-core mailing list
Ironruby-core at rubyforge.org<mailto:Ironruby-core at rubyforge.org>
http://rubyforge.org/mailman/listinfo/ironruby-core

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/ironruby-core/attachments/20100203/1bb47afb/attachment-0001.html>


More information about the Ironruby-core mailing list