-
-
Save waterlink/d7f42793d465e11c398c to your computer and use it in GitHub Desktop.
Draft implementation for Rec
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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