T
T. Onoma
Greetings all Type Checkers!
I have implemented a version of euphoria's type system in Ruby.
# the good stuff
module Kernel
def typechecking(x=nil)
return $_tc if x == nil
$_tc = x ? true : false
end
end
class Module
def type(tname, &check)
define_method(tname) do |*v|
if $_tc
unless v.all? { |y| check.call(y) }
raise TypeError
end
end
return *v
end
end
end
# demonstrate how it feels
class TypeTest
type :big4 do |x|
x > 4
end
def ameth(x)
# -- type checking --
big4 x
# -- rest of code --
puts x
end
end
typechecking true # turn type checking on
t = TypeTest.new
t.ameth(5) # => 5
t.ameth(2) # => TypeError
Now if you can just get matz to add a little sytax sugar for type and inline parameter methods:
class TypeTest
type big4(x)
x > 4
end
def ameth(big4 x)
puts x
end
end
would that quell the need?
-t0
I have implemented a version of euphoria's type system in Ruby.
# the good stuff
module Kernel
def typechecking(x=nil)
return $_tc if x == nil
$_tc = x ? true : false
end
end
class Module
def type(tname, &check)
define_method(tname) do |*v|
if $_tc
unless v.all? { |y| check.call(y) }
raise TypeError
end
end
return *v
end
end
end
# demonstrate how it feels
class TypeTest
type :big4 do |x|
x > 4
end
def ameth(x)
# -- type checking --
big4 x
# -- rest of code --
puts x
end
end
typechecking true # turn type checking on
t = TypeTest.new
t.ameth(5) # => 5
t.ameth(2) # => TypeError
Now if you can just get matz to add a little sytax sugar for type and inline parameter methods:
class TypeTest
type big4(x)
x > 4
end
def ameth(big4 x)
puts x
end
end
would that quell the need?
-t0