Skip to content

Methods

def method_name(params): ReturnType
# body
end

When the return type is omitted, private methods infer it and all other methods return Nil (see Return Type Rules by Visibility). Annotating it enforces a specific API/ABI.

The last evaluated expression in a method body is its return value. An explicit return is not required:

def double(x: Int): Int
x * 2
end
def greet(name: String): String
greeting = "Hello"
"#{greeting}, #{name}!" # last expression — implicitly returned
end

An empty method body implicitly returns nil.

Public, protected, and internal methods — the return type is never inferred. Omitting the annotation declares Nil: the method returns nil whatever its last expression is, and compiles to void in IL (see Nil). This is idiomatic for methods that exist for side effects:

def log(message: String) # return type is Nil — no annotation needed
puts message
end
def greet(name: String): String # explicit annotation when returning a value
"Hello, #{name}!"
end

Adding an explicit : Nil is valid but redundant; am fmt leaves it as written. Annotate : Void when the method should produce no value at all.

Private methods — the return type is inferred from the last evaluated expression. If the last expression does not resolve to a concrete type (i.e. its type is Any, ambiguous, or unresolvable), the compiler emits an error:

private def helper(x: Int)
x * 2 # inferred as Int — ok
end
private def bad_helper
some_call_that_returns_Any # error: cannot infer return type
end

An explicit annotation is always accepted and suppresses inference:

private def helper(x: Int): Int
x * 2
end

A method whose name ends in ? returns Bool when its return type is omitted, whatever its visibility. An explicit annotation overrides this:

def valid? # Bool
@errors.empty?
end
def []?(index: Int): T? # explicit — returns T?
end

Parameters must appear in this order:

  1. Positional arguments
  2. Keyword arguments
  3. Variadic arguments
def greet(name: String, age: Int): String

Double colon (::) marks a keyword argument. Keyword arguments must be passed by name at the call site.

def create(name:: String, role:: String): User
def create(name:: String, dob:: Date = Date.now): User

Captures remaining positional arguments as an Array[T]. The element type is required, as for any other parameter:

def log(*args: String): Void

Captures remaining keyword arguments. Like every other parameter, **kwargs must be typed, but the syntax for that is not yet decided — for example, whether the annotation names the value type (**kwargs: String) or the whole collection.

def log(**kwargs): Void # placeholder — type syntax TBD
def foo(x: Int, name:: String, dob:: Date = Date.now, *args: String, **kwargs): String
greet("Alice", 30)
create(name: "Alice", role: "admin")
foo(42, "extra1", "extra2", name: "Alice", dob: Date.parse("1990-01-01"), theme: "dark")

An array can be expanded into positional arguments with *:

args = ["Alice", 30]
greet(*args) # same as greet("Alice", 30)

A hash can be expanded into keyword arguments with **:

opts = { name: "Alice", role: "admin" }
create(**opts) # same as create(name: "Alice", role: "admin")

Both can be combined with regular arguments. The splat must appear after any regular positional arguments and before keyword arguments:

prefix = [42]
extras = ["extra1", "extra2"]
kw = { theme: "dark" }
foo(*prefix, *extras, name: "Alice", **kw)

Type constraints:

  • *array — the array’s element type must be compatible with the expected positional parameter types. The compiler checks this statically where the type is known.
  • **hash — the hash must be Hash[Symbol, T]. The compiler checks key/value compatibility against the method’s keyword parameters where possible; unmatched keys flow into **kwargs if the method declares one.

Methods can have multiple names via alias. Amethyst adopts Ruby’s convention of providing multiple spellings for the same method:

alias respond_to?, responds_to? # new name first, original second
alias collect, map

The @[Alias] attribute provides the same at the .NET level, useful when the canonical name needs to differ from the compiled name:

@[Alias("respond_to?")]
def responds_to?(name: Symbol): Bool
end

Both spellings are valid in Amethyst code. The alias compiles to a delegating method in IL, carrying @[AmethystName] for round-trip resolution.

Behind the scenes, argument kinds are tracked via internal attributes applied by the compiler:

  • Positional args: @[AnonymousArgumentAttribute]
  • Keyword args: @[KeywordArgumentAttribute]

These are not written by the user.