Skip to content

Metaprogramming

Much of Amethyst’s metaprogramming is covered by features defined elsewhere:

  • @[Attribute] — compile-time metadata and code generation hooks → Attributes
  • property / getter / setter — declarative accessor generation → Classes
  • extension class — controlled open classes → Modules
  • def name / def name= — auto-promoted to .NET properties → Classes
  • Protocols and mixins — structural contracts → Modules
  • Method name mangling (?, !) → Overview

This document covers the remaining runtime metaprogramming features.


responds_to? is available on every object. The compiler resolves it at the appropriate level based on what it knows:

user = User.new
user.responds_to?(:name) # compile-time: true — name is defined on User
user.responds_to?(:fly) # compile-time: false — fly is not defined on User
obj: Any = get_something
obj.responds_to?(:name) # runtime: resolved via .NET reflection

Resolution order:

  1. Compile-time — type fully known; folded to a constant true or false
  2. Reflection — type is Any or unknown at compile time; checked via .NET reflection
  3. DynamicMessages — type includes the DynamicMessages mixin; delegated to the mixin’s responds_to? implementation

Calls a method by name at runtime. Return type is Any? since it cannot be known statically:

user.send(:name) # calls user.name
user.send(:greet, "hello") # calls user.greet("hello")

On well-defined types, the compiler optimises send to a direct call when the method name is a compile-time constant. On Any or DynamicMessages types, it dispatches at runtime.


Types that need to handle arbitrary messages include the DynamicMessages mixin. This is explicit opt-in — not every object can respond to unknown messages.

mixin DynamicMessages
# Required — handle any message not statically defined on the type
requires def method_missing(name: Symbol, *args: Any): Any
# Optional — declare which dynamic messages this object responds to.
def responds_to?(name: Symbol): Bool
false
end
end
class Proxy
include DynamicMessages
def initialize(@target: Any)
end
def method_missing(name: Symbol, *args: Any): Any
@target.send(name, *args)
end
def responds_to?(name: Symbol): Bool
@target.responds_to?(name)
end
end

When calling an unknown method on a DynamicMessages type:

  • The call is allowed and typed as Any
  • At runtime it routes through method_missing

When calling an unknown method on a non-DynamicMessages type:

  • Compile error

Operators are defined as methods with conventional names:

class Vector
getter x: Float
getter y: Float
def +(other: Vector): Vector
Vector.new(@x + other.x, @y + other.y)
end
def <=>(other: Vector): Int
magnitude <=> other.magnitude
end
def [](index: Int): Float
index == 0 ? @x : @y
end
def []=(index: Int, value: Float): Void
if index == 0
@x = value
else
@y = value
end
end
end

Supported operators: +, -, *, /, %, **, ==, !=, <, >, <=, >=, <=>, [], []=, <<, >>, &, |, ^, ~, !

A []? method (nil-returning subscript) can be defined with an explicit nullable return type (see Predicate Methods), and called as obj.[]?(i). The shorthand obj[i]? is not valid syntax — the ? suffix on a subscript call is a compile error.


Deferred along with Compile-Time Attribute Macros, since both need a model for running Amethyst code at compile time. The intent:

Mixins can define self.included and self.extended callbacks that run when the mixin is included or extended:

mixin Trackable
def self.included(base: Class): Void
# runs at compile time when a class includes Trackable
end
end

@[Attribute] can trigger compile-time code generation — the mechanism for Rails-style declarative APIs (belongs_to, validates, etc.) with type safety. Details to be defined.