Sha256: d6684cf38cbcc985e96fece6614d2dc4163342a6b4803216a50f5dc5b5f65cbe

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

require 'haskell/type_list'

module Haskell
  class << self
    def assert_arg_type(meth, args, klasses)

      args.each_with_index do |arg, i|
        if wrong_type?(arg, klasses[i])
          raise ArgumentError, "Wrong type of argument, type of #{arg.inspect} should be #{klasses[i]}"
        end
      end
    end

    def assert_rtn_type(meth, rtn, klass)
      if wrong_type?(rtn, klass)
        raise TypeError, "Expected #{meth} to return #{klass} but got #{rtn.inspect} instead"
      end
    end

    def wrong_type?(obj, klass)
      !(obj.is_a?(klass) || klass == Any)
    end
  end
end

class Module
  private
    def __haskell__
      prepend (@__haskell__ = Module.new) unless @__haskell__
      @__haskell__
    end

    def type(type_list, meth)
      __haskell__.send(:define_method, meth) do |*args, &block|
        ::Haskell.assert_arg_type(meth, args, type_list.args)
        rtn = super(*args, &block)
        ::Haskell.assert_rtn_type(meth, rtn, type_list.rtn)
        rtn
      end
      self
    end
end

class Any; end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
haskell-0.0.2 lib/haskell.rb