Metaprogramming
Much of Amethyst’s metaprogramming is covered by features defined elsewhere:
@[Attribute]— compile-time metadata and code generation hooks → Attributesproperty/getter/setter— declarative accessor generation → Classesextension class— controlled open classes → Modulesdef 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?
Section titled “responds_to?”responds_to? is available on every object. The compiler resolves it at the
appropriate level based on what it knows:
user = User.newuser.responds_to?(:name) # compile-time: true — name is defined on Useruser.responds_to?(:fly) # compile-time: false — fly is not defined on User
obj: Any = get_somethingobj.responds_to?(:name) # runtime: resolved via .NET reflectionResolution order:
- Compile-time — type fully known; folded to a constant
trueorfalse - Reflection — type is
Anyor unknown at compile time; checked via .NET reflection - DynamicMessages — type includes the
DynamicMessagesmixin; delegated to the mixin’sresponds_to?implementation
Calls a method by name at runtime. Return type is Any? since it cannot be known
statically:
user.send(:name) # calls user.nameuser.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.
Dynamic Messages
Section titled “Dynamic Messages”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 endendExample — Proxy
Section titled “Example — Proxy”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) endendCompiler Behaviour
Section titled “Compiler Behaviour”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
Operator Overloading
Section titled “Operator Overloading”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 endendSupported 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.
Module Hooks (deferred)
Section titled “Module Hooks (deferred)”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 endendCompile-Time Attribute Macros [TBD]
Section titled “Compile-Time Attribute Macros [TBD]”@[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.