2016-06-10 25 views
0

在Cadence SKILL(专有EDA语言,基于LISP & SCHEME)中,可以在过程中定义参数类型。
如果给出错误类型的参数,它将会出错。请参阅下面的shell报告:Ruby:类型跳棋参数,类似于Cadence SKILL

procedure(foo(ko "t") printf("Hey %s\n" ko)) 
>foo 
>foo("1") 
>Hey 1 
>t 
foo(1) 
>*Error* foo: argument #1 should be a string (type template = "t") - 1 

有没有像Ruby那样漂亮的东西?也就是说,在方法接口定义中,不是主体,类型检查完成了吗?
谢谢。

+0

否。Ruby没有内置类型注释。 –

回答

1

你可以把它“漂亮”是这样的:

module FirstArgumentIsAString 
    module Initializer 
    def initialize(word) 
     fail 'Word must be String' unless word.is_a?(String) 
     super 
    end 
    end 

    def self.included(klass) 
    klass.send :prepend, Initializer 
    end 
end 

class Foo 
    include FirstArgumentIsAString 
end 

y = Foo.new(2) 
> Uncaught exception: Word must be String 
+0

哇。几乎我想要的,我可以调整这一点,使其成为我所设想的。谢谢。 – user1134991

0

你总是可以做

fail 'Keep input as a string' unless variable_name.is_a?(String) 

,而不是思维的动态类型语言的方式,努力实现鸭打字