Forums

extending the dynamic typing how-to page

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

extending the dynamic typing how-to page

Postby jonathandavid » Tue Jan 13, 2009 11:45 am

I was reading the How-To on dynamic types. At the end, there is a class Notes which uses two "add" methods, one dynamic and the other statically typed, to explain the benefits and drawbacks inherent to each approach. This is all sound, however since Cobra supports generics I think maybe another version of "add" could be added, in which it is implemented as a generic method:

class Notes
# ...dynamic and static versions of add...

def add<of T>(a as T,b as T) as T
return a + b


I'm not listing the benefits / drawbacks of this approach because I don't know enough about how generics are implemented in the CLR. My guess would be:
(*) benefits: great flexibility, fast at runtime
(*) drawbacks: code bloat??? slower compilation???

In any case my point is that it might be interesting to extend this How-To page so that it discusses the generics approach as well.

BTW I don't know if Cobra supports generic methods at this stage, but I'm sure it will support them in the future as they are part of C#.
jonathandavid
 
Posts: 159

Re: extending the dynamic typing how-to page

Postby jonathandavid » Tue Jan 13, 2009 12:16 pm

On a (not so) related note, I've just learned that the syntax "x to !" is used to cast away the "nilability" from a type. I don't find that particularly intuitive, may I suggest the syntax "x to not ?"

It's slightly more verbose (which doesn't matter much as this construct will be rarely used), but the intent is much more clear, IMHO.
jonathandavid
 
Posts: 159

Re: extending the dynamic typing how-to page

Postby Charles » Tue Jan 13, 2009 12:27 pm

jonathandavid wrote:I was reading the How-To on dynamic types. At the end, there is a class Notes which uses two "add" methods, one dynamic and the other statically typed, to explain the benefits and drawbacks inherent to each approach. This is all sound, however since Cobra supports generics I think maybe another version of "add" could be added, in which it is implemented as a generic method:

class Notes
# ...dynamic and static versions of add...

def add<of T>(a as T,b as T) as T
return a + b

...

Have you tried a generic "add" method in C# or Cobra? .NET does not support math on generic arguments. There's not even a constraint you can use like:
def add<of T>(a as T, b as T) as T
where T must be INumber
...

I don't know why they don't support it. Structs can implement interfaces and each math type is backed by a struct such as System.Decimal and System.Double.

I think JVM has the same limitation.

This is a large reason why Cobra has the configurable "number" type. Otherwise there would be no easy way to switch between float32, float64 and decimal to see the effect on your code.

Although I still agree that a How To on generics would be useful.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: extending the dynamic typing how-to page

Postby Charles » Tue Jan 13, 2009 12:30 pm

jonathandavid wrote:On a (not so) related note, I've just learned that the syntax "x to !" is used to cast away the "nilability" from a type. I don't find that particularly intuitive, may I suggest the syntax "x to not ?"

It's slightly more verbose (which doesn't matter much as this construct will be rarely used), but the intent is much more clear, IMHO.

This is hard for me to swallow because I've gotten so use to "to !". I got the "!" from Spec# which uses it to mean "not null".

I don't know.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: extending the dynamic typing how-to page

Postby Charles » Tue Jan 13, 2009 12:56 pm

Just to be explicit: Yes, Cobra already has generic methods.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: extending the dynamic typing how-to page

Postby jonathandavid » Tue Jan 13, 2009 1:17 pm

Chuck wrote:Have you tried a generic "add" method in C# or Cobra? .NET does not support math on generic arguments.


What a pity, I had forgotten that limitation (which makes me appreciate how cool C++ templates are). At least MS seems to acknowledge that this an important flaw, and even state their intention to improve it some day. The INumber solution you propose, even if it were feasible, would not be satisfactory anyway (what if I want to add strings, or objects of a type that has an overloaded operator + ?). The best solution would probably be a constraint of the kind "HasMethod(operator+)", but I don't think that's going to happen any time soon.

Chuck wrote:This is hard for me to swallow because I've gotten so use to "to !". I got the "!" from Spec# which uses it to mean "not null".


Ok, it was just an idea. BTW, what did you do to learn Spec#? I did a quick search and found virtually no online resources...
jonathandavid
 
Posts: 159

Re: extending the dynamic typing how-to page

Postby Charles » Tue Jan 13, 2009 1:35 pm

I flew to Microsoft and attended the Lang.NET summit. :-)

I also poked around their online resources, but yeah, it wasn't the best experience. Their site is at http://research.microsoft.com/en-us/projects/specsharp/

While I agree INumber does not cover all cases, it would be a huge improvement over the current situation.

In any case, we have dynamic binding so you can always go that route:
def add(a, b) as dynamic
return a + b
Charles
 
Posts: 2515
Location: Los Angeles, CA


Return to Discussion

Who is online

Users browsing this forum: No registered users and 14 guests