Methods
Definition
Section titled “Definition”def method_name(params): ReturnType # bodyendWhen 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.
Implicit Return
Section titled “Implicit Return”The last evaluated expression in a method body is its return value. An explicit
return is not required:
def double(x: Int): Int x * 2end
def greet(name: String): String greeting = "Hello" "#{greeting}, #{name}!" # last expression — implicitly returnedendAn empty method body implicitly returns nil.
Return Type Rules by Visibility
Section titled “Return Type Rules by Visibility”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 messageend
def greet(name: String): String # explicit annotation when returning a value "Hello, #{name}!"endAdding 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 — okend
private def bad_helper some_call_that_returns_Any # error: cannot infer return typeendAn explicit annotation is always accepted and suppresses inference:
private def helper(x: Int): Int x * 2endPredicate Methods
Section titled “Predicate Methods”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?endParameter Kinds
Section titled “Parameter Kinds”Parameters must appear in this order:
- Positional arguments
- Keyword arguments
- Variadic arguments
Positional Arguments
Section titled “Positional Arguments”def greet(name: String, age: Int): StringKeyword Arguments
Section titled “Keyword Arguments”Double colon (::) marks a keyword argument. Keyword arguments must be passed by name
at the call site.
def create(name:: String, role:: String): UserKeyword Arguments with Defaults
Section titled “Keyword Arguments with Defaults”def create(name:: String, dob:: Date = Date.now): UserVariadic Positional (*args)
Section titled “Variadic Positional (*args)”Captures remaining positional arguments as an Array[T]. The element type is
required, as for any other parameter:
def log(*args: String): VoidVariadic Keyword (**kwargs) [TBD]
Section titled “Variadic Keyword (**kwargs) [TBD]”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 TBDFull Example
Section titled “Full Example”def foo(x: Int, name:: String, dob:: Date = Date.now, *args: String, **kwargs): StringCall Site
Section titled “Call Site”greet("Alice", 30)create(name: "Alice", role: "admin")foo(42, "extra1", "extra2", name: "Alice", dob: Date.parse("1990-01-01"), theme: "dark")Splatting at the Call Site
Section titled “Splatting at the Call Site”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 beHash[Symbol, T]. The compiler checks key/value compatibility against the method’s keyword parameters where possible; unmatched keys flow into**kwargsif the method declares one.
Aliases
Section titled “Aliases”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 secondalias collect, mapThe @[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): BoolendBoth spellings are valid in Amethyst code. The alias compiles to a delegating method
in IL, carrying @[AmethystName] for round-trip resolution.
Compiler Representation
Section titled “Compiler Representation”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.