[Ironruby-core] Generic method synxtax

Jim Deville jdeville at microsoft.com
Fri Jan 9 17:38:23 EST 2009


Agreed. Once we find what works, we can consider pulling it into the interpreter, but we may not even need to.

JD

From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Jimmy Schementi
Sent: Friday, January 09, 2009 2:03 PM
To: ironruby-core at rubyforge.org
Subject: Re: [Ironruby-core] Generic method synxtax

Cool, implement the basic support and we can play around with the syntax. Thanks Tomas!

From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Tomas Matousek
Sent: Friday, January 09, 2009 1:46 PM
To: ironruby-core at rubyforge.org
Subject: Re: [Ironruby-core] Generic method synxtax

Mine has also an issue that just occurred to me. If you do:

class C
  content.load of(Texture2D), "foo"
end

"of" resolves to Module#of which would return a C<Texture2D> if C was a generic type or throws an exception.
So that's not good either. We would need a different method name for generic method constructor than for generic type. Maybe "method_of"?

As for your idea:
content.of(T).load

is looks backwards. The content is not of T... the load is. I think this would be confusing.

Until we figure out some nice clean solution that works in 100% cases, I would implement the following as the first step for generic method support:

content.method(:load).of(Texture2D).call("mytexture")

(Note, you can do the same in Ruby for regular methods: "foo".method(:center).call(100))

anything else is just a convenience and anyone could define it via standard Ruby means.

Tomas


From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Jimmy Schementi
Sent: Friday, January 09, 2009 1:12 PM
To: ironruby-core at rubyforge.org
Subject: Re: [Ironruby-core] Generic method synxtax

Here are the ideas that stand out to me:

I like Tomas's idea, since it reads like C#/VB, but adding an argument to the front urks me:

content.load of(Texture2D), "mytexture"

Shri, having the "_of" appended to a generic method name and requiring a ".do" after it is how you would work with lambdas (except you do ".call"), but since we're talking about .NET methods, not .NET delegates. So, I'd like the syntax to look more like a method call. Also, as Tomas says, mangling gets more complicated ...

content.load_of(Texture2D).call "mytexture"

How about adding an "of" method to the class/object, to put the class in a generic "mode"? The method probably shouldn't be called "of", but you get the idea. It reads different than C#, but seems the most Rubyesk without changing the arguments.

content.of(Texture2D).load "mytexture"

Thoughts? If no one likes my idea, I think Tomas's is a fine compromise.
~js


From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Shri Borde
Sent: Friday, January 09, 2009 9:58 AM
To: ironruby-core at rubyforge.org
Subject: Re: [Ironruby-core] Generic method synxtax

Yup, it can theoretically cause an ambiguity, but in reality, I doubt we will ever run into a case of a class having both Foo<T> and FooOf. We can scan all of .NET 3.5 to see if there is any clash of this form. And as you say, if we really cared, we could define some priortization scheme to deal with it and/or have some other scheme like using Kernel#send to be able to get to the lower priority case. We have to deal with this in other cases too where Ruby keyword names clash with .NET api names, right? I think we should optimize for the 99.9% case (there will be no clashes) rather than chosing a suboptimal syntax that can handle the 0.1% case.

Yes, we should probably do the "Of" suffix as well if we do "_of".

Thanks,
Shri

From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Tomas Matousek
Sent: Friday, January 09, 2009 12:16 AM
To: ironruby-core at rubyforge.org
Subject: Re: [Ironruby-core] Generic method synxtax

That might also work. There might be ambiguities however (as with any name prefixes/suffixes). What if there are methods, for example, GetClass<T>() and GetClassOf(object)? What would get_class_of(String) mean? Maybe we could define a priority. But that is IMO too magical.

Also, the mangling works in two steps:

1)      the method name is tried as written (thus you can use Array.ConvertAll(...))

2)      mangled name is tried if 1) fails.

Hence we might need to handle -Of suffix as well to be consistent, if the PascalCase style is preferred by the user. And that would lead to more ambiguities.

Tomas

From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Shri Borde
Sent: Thursday, January 08, 2009 10:58 PM
To: ironruby-core at rubyforge.org
Subject: [Ironruby-core] Generic method synxtax

Another suggestion is to use name mangling to append "_of" to the method name. This could return an object with a method called "do", "call", or something like that. Since we already do name mangling from PascalCase to under_scores, this could work out. It avoids having to pass the type parameter along with the other arguments.

Array.convert_all_of(Fixnum, String).do data, lambda {|x|x.to_s }
Content.load_of(Texture2d).do "mytexture"
System.Activator.create_instance_of(String).do
System.Linq.Enumerable.count_of(String).do string_array

From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Jim Deville
Sent: Wednesday, November 26, 2008 4:12 PM
To: ironruby-core at rubyforge.org
Subject: Re: [Ironruby-core] IronRuby and XNA. Super and Generics

For the record: I really like the chaining idea. I don't know if it would work, but I like it.

From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Orion Edwards
Sent: Wednesday, November 26, 2008 4:10 PM
To: ironruby-core at rubyforge.org
Subject: Re: [Ironruby-core] IronRuby and XNA. Super and Generics

Ah I see. Completely agree with the reasoning behind 'stuff appears when you require mscorlib' too.

Possible idea - make 'of' a proc rather than a method, so it uses square brackets instead of round ones.

d = Dictionary.of[String, String]

t = Load of[Texture2d], "sometexture"   # awesome, but only because 'Load' is a conveniently nice name


IMHO the [] "looks more like templates" than if it used () - but I'm biased on account of having never written a line of VB.net in my life.
The VB coders will probably have the exact opposite reaction to me :-)

Even so, I'm not entirely sure that copying VB is neccessarily the right thing...
Examples for thought:

data = [1,2,3,4]

Array.convert_all of[Fixnum, String], data, lambda{ |x| x.to_s }

Array.convert_all of(Fixnum, String), data, lambda{ |x| x.to_s }

Array.convert_all T[Fixnum, String], data, lambda{ |x| x.to_s }

Array.convert_all T(Fixnum, String), data, lambda{ |x| x.to_s }


I guess that any of these will be fine once one becomes used to them...
Here's one from left-field which probably won't work, but how about using a proxy like andand does

Array.with(Fixnum, String).convert_all data, lambda{ |x| x.to_s }

Array.T(Fixnum, String).convert_all data, lambda{ |x| x.to_s }

T(Texture2d).load "sometexture"

Tomas Matousek wrote:
It's like:

    Sub Foo(Of T)(ByVal a As T)
    End Sub

    Sub Main()
        Foo(Of Integer)(1)
    End Sub

Tomas

From: 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: Wednesday, November 26, 2008 3:27 PM
To: ironruby-core at rubyforge.org<mailto:ironruby-core at rubyforge.org>
Subject: Re: [Ironruby-core] IronRuby and XNA. Super and Generics

That makes sense.

Dictionary.of(T1, T2) looks good, but how do they handle invoking generic methods?
I don't have VB.net installed, and all the samples I can find all rely on type inference rather than explicitly specifying the type

Do they do Load( of(Texture2d), "blah" ) ??

Thanks.

Tomas Matousek wrote:
VB.NET has (Of T) syntax for generic parameters, so it would be familiar to VB programmers :)
I don't see a strong connection between "of" and "typeof".

"T" is interesting, though we already use "of" for generic types (e.g. Dictionary.of(String, String)) so it might be better to stick with one concept for all generic parameters.

We don't want to extend Ruby syntax. Adding new methods is safe since it could always be implemented as monkey patching and could be done conditionally, for example after you require 'mscorlib' the new methods appear. If you don't use CLR no IronRuby/CLR specific methods should be available (it currently doesn't work that way, but that's the plan).

Tomas

From: 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: Wednesday, November 26, 2008 12:38 PM
To: ironruby-core at rubyforge.org<mailto:ironruby-core at rubyforge.org>
Subject: Re: [Ironruby-core] IronRuby and XNA. Super and Generics

While I can certainly see the elegance of the 'of' suggestion, it can't help but to trip my "code smell" warning, because 'of' implies 'typeof', and it treats the types as method arguments.

I can't help but think that every new programmer, seeing this:

myTexture = content.load( of(Texture2D), "mytexture" )
is going to wrongly infer that it's calling this CLR method

content.Load( typeof(Texture2D), "mytexture" )
The relative lack of such counter-intuitive assumptions is IMHO one of the best things about ruby. I'd really like to see IronRuby keep this up

Perhaps it would be more clear if 'of' was 'T' ?? -  eg:

myTexture = content.load( T(Texture2D), "mytexture" )
But that doesn't really solve the 'types aren't method arguments' problem

How about this:

myTexture = content.load[Texture2D]( "mytexture" )

myTexture = content.load[Texture2D] "mytexture"
Both of these produce a syntax error in MRI, which IMHO is a good thing as it means IronRuby is not breaking any existing functionality.
MRI is never going to have to call generics, and as far as I can tell there won't be any overlaps with existing ruby syntax  (eg Proc#[], etc), so it seems safe.
It also is consistent with the existing syntax for instantiating generic classes.

The deal-breaker I guess would be how hard (or possible) this syntax is to implement with the IronRuby parser, etc.



Tomas Matousek wrote:

I'm thinking of something like:



myTexture = content.load of(Texture2D), "mytexture"



I.e. we would add Kernel#of method that takes a list of classes/modules and returns a special object representing generic parameters that binder would use for selecting the right method.



We are open for more ideas.



Tomas



-----Original Message-----

From: ironruby-core-bounces at rubyforge.org<mailto:ironruby-core-bounces at rubyforge.org> [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Dudu Baião

Sent: Wednesday, November 26, 2008 10:49 AM

To: ironruby-core at rubyforge.org<mailto:ironruby-core at rubyforge.org>

Subject: Re: [Ironruby-core] IronRuby and XNA. Super and Generics



Thanks John!



I will do that.



Just for curiosity, what will be the syntax to call generic methods? :)





2008/11/26 John Lam (IRONRUBY) <jflam at microsoft.com><mailto:jflam at microsoft.com>:



We can't consume generic methods today. It's on the list of things to do though.



You can work around this by defining a concrete method in C# that calls the appropriate generic method and call the concrete method from IronRuby.



Thanks,

-John



-----Original Message-----

From: ironruby-core-bounces at rubyforge.org<mailto:ironruby-core-bounces at rubyforge.org> [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Dudu Baião

Sent: Tuesday, November 25, 2008 6:26 PM

To: ironruby-core at rubyforge.org<mailto:ironruby-core at rubyforge.org>

Subject: [Ironruby-core] IronRuby and XNA. Super and Generics



Hi guys!



Im playing with XNA trying to run the simplest xna example: To show a

SpriteBatch on screen.



After some tests I found some problems:



1- The "Microsoft::Xna::Framework::Game" expects that we implement

some methods like "Update", "Drawn" etc, and inside the method we have

to call the base class (super) actual method passing some parameters.



If I try to do this:



   def update(game_time)

     super(game_time)

   end



I get this error:



my_game.rb:23:in `update': wrong number or type of arguments for

`update' (ArgumentError)

   from Snippets.scripting:0:in `Update'

   from Microsoft.Xna.Framework.Game:0:in `Run'

   from program.rb:23:in `main'

   from :0



2- XNA uses generic functions to load the game contents. How can I

convert the code above to IronRuby?



// This is a texture we can render.

Texture2D myTexture;



protected override void LoadContent()

{

   myTexture = Content.Load<Texture2D>("mytexture");

}



Can IronRuby consume generics?



Thanks!

_______________________________________________

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



--
Orion Edwards
Web Application Developer

T: +64 7 859 2120
F: +64 7 859 2320
E: orion.edwards at open2view.com <mailto:orion.edwards at open2view.com>

Open2view.com
The Real Estate Website

[cid:image001.jpg at 01C97267.EC66D550]




































________________________________








































_______________________________________________

Ironruby-core mailing list

Ironruby-core at rubyforge.org<mailto:Ironruby-core at rubyforge.org>

http://rubyforge.org/mailman/listinfo/ironruby-core



--
Orion Edwards
Web Application Developer

T: +64 7 859 2120
F: +64 7 859 2320
E: orion.edwards at open2view.com <mailto:orion.edwards at open2view.com>

Open2view.com
The Real Estate Website

[cid:image001.jpg at 01C97267.EC66D550]


































________________________________


































_______________________________________________

Ironruby-core mailing list

Ironruby-core at rubyforge.org<mailto:Ironruby-core at rubyforge.org>

http://rubyforge.org/mailman/listinfo/ironruby-core



--
Orion Edwards
Web Application Developer

T: +64 7 859 2120
F: +64 7 859 2320
E: orion.edwards at open2view.com <mailto:orion.edwards at open2view.com>

Open2view.com
The Real Estate Website

[cid:image001.jpg at 01C97267.EC66D550]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/ironruby-core/attachments/20090109/a1d06577/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.jpg
Type: image/jpeg
Size: 8147 bytes
Desc: image001.jpg
URL: <http://rubyforge.org/pipermail/ironruby-core/attachments/20090109/a1d06577/attachment-0001.jpg>


More information about the Ironruby-core mailing list