Skip to content

Amethyst for Crystal developers

Still here? Then you want .NET, and that’s the whole difference. Crystal and Amethyst started from the same idea: Ruby’s syntax, with a compiler that knows the type of everything. Crystal compiles to native code through LLVM. Amethyst compiles to .NET IL, so it runs wherever .NET does and can use any NuGet package.

A lot of this will look familiar, because we borrowed it: property, getter and setter; [] of String; @name parameters in initialize; T?; responds_to?; and blocks with a receiver. Imitation is the sincerest form of flattery, and this page is where we own up to it.

Crystal runs top-level code. For now, Amethyst programs start at a main method.

Crystal
puts "Hello, world!"
Amethyst
def main: Void
puts "Hello, world!"
end

The colon loses its leading space, and the built-in types take their .NET names.

Crystal
def greet(name : String, greeting : String) : String
"#{greeting}, #{name}!"
end
Amethyst
def greet(name: String, greeting: String): String
"#{greeting}, #{name}!"
end

Like Crystal, Amethyst lets you leave a parameter’s type out. The compiler works out what the argument must provide from how the method uses it, and checks each call site against that. See Structural Typing.

Public methods are different: leave out the return type and the method returns nil. Crystal infers it.

Crystal Amethyst
Int32 Int
Int64 Long
Float64 Float
Array(Int32) Array[Int]
Hash(String, Int32) Hash[String, Int]
[] of Int32 [] of Int
{} of String => Int32 {} of String => Int

Generics use square brackets, borrowed from Ruby’s RBS type signatures (Array[Integer]), which is also how Python writes them (list[int]).

In Crystal, parameters after a bare * must be passed by name. Amethyst marks each keyword parameter with a double colon instead.

Crystal
def create(*, name : String, role : String = "user")
end
create(name: "Ada", role: "admin")
Amethyst
def create(name:: String, role:: String = "user"): User
end
create(name: "Ada", role: "admin")

T? means “a T or nil” in both languages, and checking for nil narrows the type in both. Crystal goes further, with unions of any types, such as Int32 | String. Amethyst only has the nilable case. For “one of several types”, use a protocol, a common base class or a complex enum.

Crystal
names = users.map &.name
city = user.try &.address.try &.city
Amethyst
names = users.map(&:name)
city = user&.address&.city

There’s no not_nil!. Check for nil and let the compiler narrow the type.

Crystal treats everything except nil, false and null pointers as truthy. Amethyst agrees on what’s falsy, but if only accepts a Bool or a nullable value, so a condition that can never be false doesn’t compile.

Crystal
puts "has items" if items.size # always true, even for 0
Amethyst
puts "has items" if items.size > 0

Blocks behave the way you’re used to: return leaves the enclosing method, next leaves the block, and break leaves the method that yielded.

Crystal changes a block’s receiver with with … yield. Amethyst puts the receiver in the block’s type instead, so callers can see what self will be without reading the method body.

Crystal
def html(&)
builder = HtmlBuilder.new
with builder yield
builder.to_s
end
Amethyst
def html(&block: () -> Void with HtmlBuilder): String
builder = HtmlBuilder.new
yield builder
builder.to_s
end

Callers write the same DSL in both languages. See Type-safe builders.

Crystal’s module is both a namespace and a mixin, and any class can be reopened. Amethyst gives each job its own keyword.

Crystal Amethyst
module Billing as a namespace namespace Billing
module Greetable with methods, then include mixin Greetable, then include
A module of abstract defs protocol with implements
Reopening class String extension class String, visible only where it’s imported

Crystal’s macros run at compile time with access to the program’s syntax tree. Amethyst’s equivalent, attribute macros, hasn’t been designed yet. Until it is, Crystal wins this one outright.

Crystal Amethyst
Native binaries through LLVM .NET assemblies, run by the .NET runtime
shard.yml and shards project.am and NuGet packages
require "json" use System::Text::Json
lib bindings to C libraries .NET interop with any .NET assembly
Pointer(Int32) Pointer[Int], which compiles to C#’s int*
case … in, checked for exhaustiveness case/when, which matches values, types and ranges and isn’t checked for exhaustiveness
struct, which can inherit from an abstract struct struct, which can’t inherit at all
Fibers, spawn and Channel async/await on .NET tasks
crystal spec Amethyst.Test (proposed)
crystal tool format am fmt (deferred)