2016-06-10 23 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) 

,而不是思維的動態類型語言的方式,努力實現鴨打字