Last active
October 25, 2015 15:44
-
-
Save osyo-manga/6249c369fdde9884c494 to your computer and use it in GitHub Desktop.
ruby typed
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
| def let **opt, &block | |
| Object.new.instance_eval do | |
| for name, type in opt | |
| if type.class == Class | |
| value = type.new | |
| else | |
| tyep = type.class | |
| value = type | |
| end | |
| self.class.class_variable_set "@@#{name}", value | |
| instance_eval{ | |
| name_ = name | |
| define_singleton_method name do | |
| self.class.class_variable_get "@@#{name_}" | |
| end | |
| type_ = type | |
| define_singleton_method "#{name_}=" do |var| | |
| raise "No match type." unless var.class <= type_ | |
| self.class.class_variable_set "@@#{name_}", var | |
| end | |
| } | |
| end | |
| instance_eval &block | |
| end | |
| end | |
| let n: Numeric, str: String do | |
| # ok | |
| p n | |
| p str | |
| # error | |
| self.n = "homu" | |
| self.str = 42 | |
| end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment