Variable type annotation tells type of local variable.
# @type var x: String
# @type var klass: Class
@type
var
x:
type
Self type annotation tells type of self
.
# @type self: Object
@type
self
:
type
Instance variable type annotation tells type of instance variable.
This annotation applies to instance variable of current context.
If it's written in module
declaration, it applies to instance variable of the module, not its instance.
# @type ivar @owner: Person
@type
ivar
ivar:
type
Global variable type annotation tells type of global variable.
# @type gvar $LOAD_PATH: Array<String>
@type
gvar
gvar:
type
Constant type annotation tells type of constant.
Note that constant resolution is done syntactically.
Annotation on File::Append
does not apply to ::File::Append
.
# @type const File::Append : Integer
@type
const
const:
type
Method type annotation tells type of method being implemented in current scope.
This annotation is used to tell types of method parameters and its body. Union method type cannot be written.
# @type method foo: (String) -> any
@type
method
method:
single method type
Module annotations is about defining modules and classes in Ruby. This kind of annotations should be written in module context.
Instance type annotation tells type of instance of class or module which is being defined.
# @type instance: Foo
@type
instance
:
type
Module type annotation tells type of module of class or module which is being defined.
# @type module: Foo.class
@type
module
:
type
This annotation tells instance variable of instance.
# @type instance ivar @x: String
# @type module ivar @klass: String.class
@type
instance
ivar
ivar:
type@type
module
ivar
ivar:
type
Type assertion allows declaring type of an expression inline, without introducing new local variable with variable type annotation.
array = [] #: Array[String]
path = nil #: Pathname?
#:
type
Type application is for generic method calls.
table = accounts.each_with_object({}) do |account, table| #$ Hash[String, Account]
table[account.email] = account
end
The each_with_object
method has [T] (T) { (Account, T) -> void } -> T
,
and the type application syntax directly specifies the type of T
.
So the resulting type is (Hash[String, Account]) { (Account, Hash[String, Account]) -> void } -> Hash[String, Account]
.
#$
type