On Sun, 2 Apr 2006, Robert Dober wrote:

> Dear group
>
> there was a recent thread about "Boolean" and it braught me to my favorit
> thing I would love to have in Ruby2.
>
> *typed parameters*
>
> My code is completely filled with things like this:
>
> def never_saw_a_better_method( clever, smart, stupid )
>            assert_type clever, String
>           assert_type  smart, Hash
>          assert_type  stuoid, Boolean
> ....
> def cool( clever : String, smart : Hash, stupid : Boolean )
>
> and of course
> def x(y)  would be equivalent to def(x : Object )

Howabout what I do..


def never_saw_a_better_method( clever, smart, stupid )
   clever.static_type_check String
   smart.quacks_like :each_pair
   stupid.poymorphic_type_check SomeBaseClass
end


# Abstract base class for all the type check exceptions
class TypeCheckException  < Exception
end

# This exception is thrown in event of a method being invoked with an
# object of the wrong duck type.
class DuckTypingException < TypeCheckException
end

# This exception is thrown in event of a method being invoked with a
# parameter of of the wrong static type.
class StaticTypeException < TypeCheckException
end

# This exception is thrown in event of a method being invoked with a
# parameter of of the wrong polymorphic type.
class PolymorphicTypeException < TypeCheckException
end


class Object

   # Raise a DuckTypingException unless the object responds to all symbol.
   def quacks_like( *symbols)
     symbols.each do |symbol|
       raise DuckTypingException, "Duck typing error, expected this object to respond to :#{symbol}, but found class #{self.class}\n\t\t#{symbol.inspect}" unless
         respond_to? symbol
     end
   end

   def static_type_check( klass)
     raise StaticTypeException, "Static type check error, expected object to be exactly class '#{klass}', found '#{self.class}'\n\t\t#{self.inspect}" unless
       self.class == klass
   end

   def polymorphic_type_check( klass)
     raise PolymorphicTypeException, "Polymorphic type check error, expected object to be a kind of '#{klass}', found '#{self.class}'\n\t\t#{self.inspect}" unless
       self.kind_of? klass
   end

end


if $0 == __FILE__ then
   require 'test/unit'

   class TC_Utilities < Test::Unit::TestCase

     def test_utilities
       assert_raise( DuckTypingException) { nil.quacks_like( :call)}
       assert_raise( DuckTypingException) { 1.quacks_like( :+, :call)}
       1.quacks_like( :+, :-, :*)

       assert_raise( StaticTypeException) { nil.static_type_check( String)}

       assert_raise( PolymorphicTypeException) {"foo".polymorphic_type_check( Array)}

       begin
         2.static_type_check( String)
       rescue TypeCheckException => details
         puts details
       end
       begin
         2.polymorphic_type_check( String)
       rescue TypeCheckException => details
         puts details
       end
     end

   end


end



John Carter                             Phone : (64)(3) 358 6639
Tait Electronics                        Fax   : (64)(3) 359 4632
PO Box 1645 Christchurch                Email : john.carter / tait.co.nz
New Zealand

Carter's Clarification of Murphy's Law.

"Things only ever go right so that they may go more spectacularly wrong later."

From this principle, all of life and physics may be deduced.