Steepã®Annotationã«é¢ããåå¿é² #asakusa_bashi_rbs
Steepã®manual/annotations.mdãèªã¿ãªãããå®éã«ã³ã¼ããæ¸ãã¦è¦ãããã¨ãããã°ã«ã¾ã¨ããã
å¤æ°
å¤æ°ã®åã String?
ãã String
ã«ããã¨ãã«ä¾¿å©ããã
# @type var value: String value = %w[a b c].sample puts('Hi, ' + value)
ã¢ããã¼ã·ã§ã³ããªãå ´åã sample: () -> String?
ãªã®ã§åæ¤æ»ã¨ã©ã¼ã«ãªãã
app/user.rb:2:14: [error] Cannot pass a value of type `(::String | nil)` as an argument of type `::string` â (::String | nil) <: ::string â (::String | nil) <: (::String | ::_ToStr) â nil <: (::String | ::_ToStr) â nil <: ::String â â Diagnostic ID: Ruby::ArgumentTypeMismatch â â puts('Hi, ' + value) ~~~~~ Detected 1 problem from 1 file
ã¤ã³ã¹ã¿ã³ã¹å¤æ°
å¤æ°ã¨åãããã¤ã³ã¹ã¿ã³ã¹å¤æ°ã®åãå¤ããã¨ãã«ä¾¿å©ããã
# ruby class User def initialize(name) @name = name end def hi # @type ivar @name: String puts('Hi, ' + @name) end def bye puts('bye, ' + @name) end end # rbs class User @name: String? def initialize: (String?) -> void def hi: () -> void def bye: () -> void end
ãã㯠bye
ã®æ¹ã ãåæ¤æ»ã¨ã©ã¼ã«ãªãã
app/user.rb:12:19: [error] Cannot pass a value of type `(::String | nil)` as an argument of type `::string` â (::String | nil) <: ::string â (::String | nil) <: (::String | ::_ToStr) â nil <: (::String | ::_ToStr) â nil <: ::String â â Diagnostic ID: Ruby::ArgumentTypeMismatch â â puts('bye, ' + @name) ~~~~~ Detected 1 problem from 1 file
ã¯ã©ã¹ã¨ã¡ã½ãã
Rubyã®ã³ã¼ãå ã«ã¢ããã¼ã·ã§ã³ãæ¸ãã¨ãRBSãªãã§ãSteepã§åãã§ãã¯ã§ããã
# @type const User : User class User # @type method say: (String, Integer) -> void def say(name, age) puts(name + age) # åæ¤æ»ã¨ã©ã¼ end end User.new.say(1, 2) # ããã¯åã¨ã©ã¼ã«ãªããªã
ãã®ã³ã¼ããSteepã§æ¤æ»ããã¨ã say
ã¡ã½ããã®ä¸ã ãåæ¤æ»ã®å¯¾è±¡ã«ãªã£ã¦ãããã¨ããããã
app/user.rb:5:16: [error] Cannot pass a value of type `::Integer` as an argument of type `::string` â ::Integer <: ::string â ::Integer <: (::String | ::_ToStr) â ::Integer <: ::String â ::Numeric <: ::String â ::Object <: ::String â ::BasicObject <: ::String â â Diagnostic ID: Ruby::ArgumentTypeMismatch â â puts(name + age) # åæ¤æ»ã¨ã©ã¼ ~~~ Detected 1 problem from 1 file
pure
ã¡ã½ããã«å¯ä½ç¨ããªããã¨ãæ示ããã¨ãã«æå®ããã*1
# ruby class User def initialize(name) @name = name end def name @name end end user = User.new(nil) if user.name puts("Hi, " + user.name) end # rbs class User def initialize: (String?) -> void %a{pure} def name: () -> String? end
%a{pure}
ãæå®ããªãå ´åã以ä¸ã®ãããªåæ¤æ»ã¨ã©ã¼ãèµ·ããã
app/user.rb:13:16: [error] Cannot pass a value of type `(::String | nil)` as an argument of type `::string` â (::String | nil) <: ::string â (::String | nil) <: (::String | ::_ToStr) â nil <: (::String | ::_ToStr) â nil <: ::String â â Diagnostic ID: Ruby::ArgumentTypeMismatch â â puts("Hi, " + user.name) ~~~~~~~~~ Detected 1 problem from 1 file
*1:ããã¥ã¡ã³ãã«ã¯è¨è¼ããªã