Skip to content

Instantly share code, notes, and snippets.

@waterlink
Created March 26, 2015 00:27
Show Gist options
  • Select an option

  • Save waterlink/d7f42793d465e11c398c to your computer and use it in GitHub Desktop.

Select an option

Save waterlink/d7f42793d465e11c398c to your computer and use it in GitHub Desktop.
Draft implementation for Rec
class DatatypeBase
def initialize(blk)
@blk = blk
end
def _resolve!
@raw_contract = instance_eval(&@blk)
end
def Rec(name, &blk)
RecExtension.Rec(name, self.class, &blk)
end
end
module RecExtension
def self.Rec(name, base=DatatypeBase, &blk)
klass = Class.new(base)
instance = klass.new(blk)
klass.send(:define_method, name) do
instance
end
instance._resolve!
instance
end
def Rec(*args, &blk)
RecExtension.Rec(*args, &blk)
end
end
RecExtension.Rec(:a) { Rec(:b) { [ Integer, a => b ] } }
# => #<#<Class:0x0000000203b838>:0x0000000203b7c0 @blk=#<Proc:0x0000000203b888@(irb):3>, @raw_contract=#<#<Class:0x0000000203b630>:0x0000000203b5e0 @blk=#<Proc:0x0000000203b658@(irb):3>, @raw_contract=[Integer, {#<#<Class:0x0000000203b838>:0x0000000203b7c0 ...>=>#<#<Class:0x0000000203b630>:0x0000000203b5e0 ...>}]>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment