Solargraph

A Ruby Language Server

Type Detection

Solargraph uses three methods to determine types: tagging, inheritance, and inference.

Tagging

The following YARD tags can be used to define types:

@return

The return type of a method or attribute.

class MyClass
  # @return [String]
  attr_accessor :my_string

  # @return [Integer]
  def my_integer
  end
end

@param

The type of a method parameter.

class MyClass
  # @param num [Integer]
  def my_method(num)
  end
end

@type

The type of a variable.

# @type [String]
my_string = get_a_string

Inheritance

When a method doesn’t have its own @return or @param tags, Solargraph will check if tags exist in a super method.

module MyModule
  # @return [String]
  def my_string
  end
end

class MyClass
  include MyModule
  def my_string # <= Inherits String return type from MyModule
  end
end

Inference

If no type tags are found, Solargraph will attempt to infer a type from static code analysis.

class MyClass
  def my_string
    'my_string'
  end
end

MyClass.new.my_string # <= Inferred to be a String

Type Display Syntax

When using intellisense, types are displayed with different operators depending on how the type was detected.

Tagged types are displayed with =>.

Tagged type

Inferred types are displayed with =~.

Inferred type

Types that are inherited from superclass methods are displayed with =^.

Inferred type