パルカワ2

PerlとRubyとイチャラブ

type_constraintsというgemを作った。

見たまんま

require "type_constraints"

TypeConstraints.setup do
  type :Array do
    constraint -> v { v.kind_of?(Array) }
  end
  subtype :ArrayOfString, :Array do
    constraint -> v { v.all?() {|v| v.kind_of?(String) } }
  end
  subtype :ArrayOfHisa, :ArrayOfString do
    constraint -> v { v.all?() {|v| v === "5518" } }
  end
end


puts "testing Array"
p TypeConstraints.check?(:Array, []) #=> true
p TypeConstraints.check?(:Array, {}) #=> false

puts "testing ArrayOfString"
p TypeConstraints.check?(:ArrayOfString, ["1000", "2000"]) #=> true
p TypeConstraints.check?(:ArrayOfString, [1000, 2000])     #=> false

puts "testing ArrayOfHisa"
p TypeConstraints.check?(:ArrayOfHisa, ["5518"]) #=> true
p TypeConstraints.check?(:ArrayOfHisa, ["hisa"]) #=> false
p TypeConstraints.check?(:ArrayOfHisa, [5518])   #=> false

まだ色々足りてないけど、rubygemsにもあげた。
https://rubygems.org/gems/type_constraints
https://github.com/hisaichi5518/type_constraints