Amethyst Language Specification
Amethyst is a Ruby-inspired, statically typed language that compiles to .NET IL. Someone familiar with Ruby should be able to pick it up with minimal friction.
- Ruby-inspired syntax (
def/end,do/endblocks, string interpolation, symbols) - Static typing with inference — annotate where it matters, not everywhere
- Non-nullable by default, with opt-out per file or project for C# interop
- Compile to .NET IL with full bidirectional C# interop
- Useful, actionable compiler error messages
- Limited duck typing via structural typing
- Metaprogramming support, bounded to what works with a static type system
- DSL-building capability (Kotlin-style typed receivers in blocks)
- Code should be fun and expressive to write
Non-Goals
Section titled “Non-Goals”- A Ruby runtime or full Ruby compatibility
- Unrestricted dynamic dispatch at runtime
- Scripting-only use — Amethyst is intended for full projects
Language Ethos
Section titled “Language Ethos”Amethyst borrows Ruby’s commitment to developer happiness. Code should read naturally and reward the writer. The following features are preserved from Ruby with this in mind:
- Optional parentheses —
puts "hello"is valid alongsideputs("hello") - Everything is an expression —
if/else,case/when, blocks all return values - Postfix conditionals —
return if invalid?,log message unless quiet? ?and!method naming —valid?,save!are idiomatic- Fluent chaining — APIs are designed to chain naturally
- Enumerable builtins —
map,select,reduce,any?,all?etc. are built in - Truthiness — only
nilandfalseare falsy. Conditions must beBoolor nullable;if "hello"is a compile error - String interpolation —
"Hello, #{name}!" - Symbols —
:role,:nameas lightweight identifiers - Destructuring —
a, b = [1, 2],first, *rest = array
.NET Naming Interop
Section titled “.NET Naming Interop”Amethyst method names are mangled for .NET consumers, following .NET’s own naming habits where one exists.
? methods
Section titled “? methods”name? methods return Bool unless annotated otherwise. The compiled name depends
on the return type:
| Return type | Compiled name | Example |
|---|---|---|
Bool |
IsName |
valid? → IsValid |
T? |
NameOrDefault |
first? → FirstOrDefault |
| Anything else | NameQuestion |
size? → SizeQuestion |
For Bool returns, names that already start with has, can, should, will,
must or needs keep that prefix instead of gaining Is: has_error? compiles as
HasError.
! methods
Section titled “! methods”! marks one of two things: a method that raises instead of returning nil or
false, or a method that changes its receiver in place instead of returning a new
value. A name! with no matching name compiles as Name. When both exist, the
compiler picks the names from their return types, checking these rules in order:
| Pair | Rule | Compiled names |
|---|---|---|
| Raises | name returns T? or Bool, and name! returns T or Void |
name → TryName, name! → Name |
| In place | name! returns Void |
name → Name, name! → NameInPlace |
| Neither | — | name → Name, name! → NameBang |
def find(id: Int): User? # TryFinddef find!(id: Int): User # Find
def sort: Array[T] # Sortdef sort!: Void # SortInPlaceIn-place ! methods return Void, unlike Ruby’s, which return nil when nothing
changed.
Protocols
Section titled “Protocols”A protocol compiles to a .NET interface, and its name gains the
I prefix that .NET interfaces use:
| Amethyst | .NET |
|---|---|
protocol Shape |
interface IShape |
protocol Drawable[T] |
interface IDrawable<T> |
Going the other way, a .NET interface’s name loses its I in Amethyst, so
System.IDisposable is Disposable. The original
name works too: IDisposable and Disposable are the same type.
When both names exist, such as in a .NET namespace with a Shape class and an IShape
interface, the exact name wins: Shape is the class and IShape is the interface.
After implements, which only accepts protocols, Shape still means IShape:
class Square implements Shape # IShape, since the Shape class can't be implementedendInterfaces whose names don’t start with I and a capital letter keep their exact name.
@[Alias("...")] overrides any generated name.
Status
Section titled “Status”This specification is a work in progress. Sections marked [TBD] are not yet decided.
Contents
Section titled “Contents”- Types
- Enums
- Structs
- Methods
- Blocks and Closures
- Attributes
- Classes
- Modules
- Error Handling
- Control Flow
- .NET Interop
- Metaprogramming
- Project System
- Standard Library
- Variables and Constants
- Destructuring
- Literals
- Attribute Macros (deferred)
- Formatting (deferred —
am fmtnot yet implemented)