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.
Hello, world
Section titled “Hello, world”Crystal runs top-level code. For now, Amethyst programs start at a main method.
puts "Hello, world!"def main: Void puts "Hello, world!"endType annotations
Section titled “Type annotations”The colon loses its leading space, and the built-in types take their .NET names.
def greet(name : String, greeting : String) : String "#{greeting}, #{name}!"enddef greet(name: String, greeting: String): String "#{greeting}, #{name}!"endLike 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]).
Named arguments
Section titled “Named arguments”In Crystal, parameters after a bare * must be passed by name. Amethyst marks each
keyword parameter with a double colon instead.
def create(*, name : String, role : String = "user")end
create(name: "Ada", role: "admin")def create(name:: String, role:: String = "user"): Userend
create(name: "Ada", role: "admin")Nil, and only nil
Section titled “Nil, and only nil”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.
names = users.map &.namecity = user.try &.address.try &.citynames = users.map(&:name)city = user&.address&.cityThere’s no not_nil!. Check for nil and let the compiler narrow the type.
Truthiness
Section titled “Truthiness”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.
puts "has items" if items.size # always true, even for 0puts "has items" if items.size > 0Blocks and DSLs
Section titled “Blocks and DSLs”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.
def html(&) builder = HtmlBuilder.new with builder yield builder.to_senddef html(&block: () -> Void with HtmlBuilder): String builder = HtmlBuilder.new yield builder builder.to_sendCallers write the same DSL in both languages. See Type-safe builders.
Modules, split three ways
Section titled “Modules, split three ways”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 |
Macros
Section titled “Macros”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.
Everything else
Section titled “Everything else”| 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) |