From: bbense+comp.lang.ruby.Sep.27.02@... Date: 2002-09-28T02:23:28+09:00 Subject: Re: adding overload to ruby -----BEGIN PGP SIGNED MESSAGE----- In article <006601c265b9$47d98cb0$845186cb@nosedog>, Gavin Sinclair wrote: > >----- Original Message ----- >From: >> >> - - Some thing that I would like to see more support for is >> the idea of Interfaces from Objective C. This is a kind >> of contract of methods that you support. Modules and >> the kind_of? method sort of support this, but not in >> a very straightforward way. Also, I think much of the >> demand for various kinds of "typed" Ruby could really >> be satisfied by rethinking the structure of error objects. >> >> - - Booker C. bense > >I don't know Objective C, but I presume the _interfaces_ are a similar idea as >in Java, i.e. a class full of method definitions but no implementations. Thus >you can say something is "Comparable" because it implements (by contract) the >method "comapre". - - I misrememebered the name, it is actually "protocol" that was introduced as a compile-time check in Objective C. Basically, it was a kind of contract that you would support a certain set of method with a given signature. Since Objective-C was compiled it's really not the same problem at all. > >My point: I don't see how this is missing in Ruby, given the message-sending >instead of method-calling paradigm, and especially given Mixins, which affect >the #is_a? result. (You mix in Enumerable, then your object #is_a? >Enumerable.) > >That is, you don't need to declare something as "Comparable" in order to >compare it. You just send the message "compare" and see what happens. > >So that leaves your comment "not in a very straightforward way". I'd like to >know what you mean, given your stated objection to static typing and anything >that resembles it. - - I retract the "not in a very straighforward way" comment. What meant is that if for example you want to have the method "print" in a Mixin, it's a bit awkward to override that method in your class. I tend to think of Mixin's as "meta-methods" and a protocol as having more specific methods. Using mixin's implies a specific implementation of a certain method, a protocol implies nothing other than that you will respond to a messages listed in the protocol. > >I'm also interested in what you mean and have in mind by "rethinking the >structure of error objects". - - Currently all you get back is an ErrorType and a string. This is nice and lightweight, but sometimes the string is well "cryptic". What if for example, when you raise a TypeError you returned the Class that you really needed? For example: irb(main):001:0> foo = 1 1 irb(main):002:0> File.new(foo) TypeError: wrong argument type Fixnum (expected String) from (irb):2:in `initialize' from (irb):2:in `new' from (irb):2 irb(main):003:0> begin irb(main):004:1* File.new(foo) irb(main):005:1> rescue => error irb(main):006:1> gerr = error irb(main):007:1> end - - What if gerr.required_class.new gave me an instance of the class that I really needed? Or more simply eval(gerr.required_class).new - - In this example I can easily parse the error string, but in general it's not this simple since there is no standard format for the string ( or even a standard on when to use ArgumentError vs TypeError ). If you had to supply the type you wanted to use TypeError, that would help standardize things quite a bit. - - Here's another example irb(main):001:0> foo = "tempest" irb(main):002:0> bar = [ 1,2 ] [1, 2] irb(main):003:0> File.new(foo,bar) TypeError: failed to convert Array into String from (irb):3:in `initialize' from (irb):3:in `new' from (irb):3 - - There's no mapping of the error to the argument or the object that caused the error. - - Booker C. Bense -----BEGIN PGP SIGNATURE----- Version: 2.6.2 iQCVAwUBPZSQ0WTWTAjn5N/lAQEUGwQAihSpq5vO02H9KUAl3j3NYfTun3gOAEhR +nf9N8FOo4RT8BfIiLAeqivn/9D6WtAtHZxOazGZGuyvoUYJJMjIwo7MndaqYuxM lFJZrPa3gYyRJGaQN9squRlV6TIGP+ssGnvnK34CjAl+VhiYB7aeAm79L7SsvBDl s5AWfces4PI= =zy9o -----END PGP SIGNATURE-----