quack! quack! I added duck typing capability to my euphoric type checking
system:
module Kernel
def typechecking(x=nil)
return $_tc if x == nil
$_tc = x ? true : false
end
end
class Module
def type(tname, *resp, &check)
define_method(tname) do |*v|
if $_tc
unless v.all? { |y| resp.all? { |z| y.send(:respond_to?, z) } }
raise TypeError, "#{v}"
end
unless v.all? { |y| check.call(y) }
raise TypeError, "#{v}"
end
end
return *v
end
end
end
Probabably could be written a little more concise. now example:
type :big4, '>', 'to_i', 'succ' do |x|
x > 4
end
def ameth(x)
big4 x
#---
puts x
end
big4 will make sure parameter responds to #> #to_i and #succ and is also
greater than 4.
i also have a some better ideas for sugar too:
class TypeTest
type big4(x)
'>', 'to_i', 'succ'
unless
x > 4
end
def ameth(big4 x)
puts x
end
end
getting better? we may have other clauses besides 'unless' too (?) seems like
i'm starting to converge on Sean's idea from another angle. except mine is
dynamic runtime, but it can be turned offf globally or locally.
BUT!!!!!!!!!!! i have discovered a problem with all type systems! see next
post....(or 2)
-t0