Crystal

Like Ruby, but with types and stuff

👋🏼

About contempt

Outline

  • History / Basic intro
  • Technical background
  • Syntax / Features
  • Possibilities
  • The bad
  • Q&A

History / Basic intro

First commit Sep 2012

Goals

  • Have a syntax similar to Ruby (but compatibility with it is not a goal)
  • Statically type-checked but without having to specify the type of variables or method arguments.
  • Be able to call C code by writing bindings to it in Crystal.
  • Have compile-time evaluation and generation of code, to avoid boilerplate code.
  • Compile to efficient native code.

History / Basic intro

Reasons

We love Ruby's efficiency for writing code.

We love C's efficiency for running code.

We want the best of both worlds.

We want the compiler to understand what we mean without having to specify types everywhere.

We want full OOP.

Oh, and we don't want to write C code to make the code run faster.

History / Basic intro

How does it look?

Valid Ruby and Crystal!

History / Basic intro

What about performance?

4X

21X

History / Basic intro

Technical background

Technical background

Compilers are hard

Technical background

 The LLVM Compiler Infrastructure

https://en.wikipedia.org/wiki/LLVM#/media/File:LLVM_Logo.svg

Technical background

Does (most of) the hard work!

Technical background

Hello World in LLVM IR

Technical background

It's easy to generate IR

https://www.slideshare.net/crystallanguage/crystal-internals-part-1-70673255

Technical background

  • Targets multiple archs (x86,x64,arm,ppc,wasm,etc.)
  • Project optimizations improve your compiler for free
  • Linker for ELF,PE,mach-O,etc
  • Standardized debugging

Syntax / Features

Syntax / Features

Everything is an object

Syntax / Features

Basic Types

Literal Value
Nil nil
Bool true / false
Int32, Int64, Integer 1, -2, 19_i64, 8_u8
Float64, Float 1.0, 1.0_f32
Char 'a'
String "a"
Symbol :a
Array [1,2], [] of Int32
Hash {"a":1},{} of Symbol => Int32
Tuple {1,"a",:b}

Syntax / Features

Arrays

Type inference! Union types!

Syntax / Features

Arrays

Quality error messages

Syntax / Features

Syntax / Features

Syntax / Features

Procs

Syntax / Features

Requiring code

GEM_HOME

  • If a file named "filename.cr" is found in the require path, it is required.
  • If a directory named "filename" is found and it contains a file named "filename.cr" directly underneath it, it is required.
  • If a directory named "filename" is found with a directory "src" in it and it contains a file named "filename.cr" directly underneath it, it is required.
  • If a directory named "filename" is found with a directory "src" in it and it contains a directory named "filename" directly underneath it with a "filename.cr" file inside it, it is required.
  • Otherwise a compile-time error is issued.

Just 'src' and 'lib' matter in the project root

Syntax / Features

Classes

Syntax / Features

Classes

Syntax / Features

Classes

Syntax / Features

Classes

Syntax / Features

Type aliases

Syntax / Features

Generics

Syntax / Features

Method overloading

Syntax / Features

Type fencing

Syntax / Features

Return types

Syntax / Features

Visibility

Syntax / Features

No $global vars!

Syntax / Features

C bindings

Syntax / Features

Finalizers

Syntax / Features

Structs

Structs inherit from Value so they are allocated on the stack and passed by value: when passed to methods, returned from methods or assigned to variables, a copy of the value is actually passed (while classes inherit from Reference, are allocated on the heap and passed by reference).

Therefore structs are mostly useful for immutable data types and/or stateless wrappers of other types, usually for performance reasons to avoid lots of small memory allocations when passing small copies might be more efficient

From the docs:

Syntax / Features

Enums

Syntax / Features

Macros

Run at compile-time

No runtime dispatch

Syntax / Features

Monkey-patching

Syntax / Features

Monkey-patching

Syntax / Features

Shards

Syntax / Features

Shards

Syntax / Features

https://shards.info

https://shardbox.org

https://shards.info

https://shards.info

No central repo!

Syntax / Features

Channels

Syntax / Features

crystal play

Web based REPL

Syntax / Features

crystal tool formatter

Built-in code formatter!

Syntax / Features

Written in Crystal!

Syntax / Features

Written in Crystal!

Syntax / Features

Single binaries are easy

Syntax / Features

Stdlib quality / modernity

  • Websockets
  • Gzip
  • JSON
  • OAuth2  / OpenSSL
  • SemVer

Syntax / Features

Speed!

Lucky 0.18

Rails 6.0.2

100X!

Syntax / Features

Speed!

https://github.com/kostya/crystal-benchmarks-game

Possibilities

Possibilities

Fast, type-checked web apps

Possibilities

Fast, type-checked web apps

Possibilities

New kinds of projects

https://github.com/ffwff/lilith

Possibilities

New kinds of projects

Possibilities

Use your Ruby skills

Possibilities

Written in Crystal!

The bad

The bad

  • No Windows version yet
  • Still a ways to go for 1.0
  • Questions about direction
  • Compilation time for bigger codebases

Crystal: like Ruby, but with types and stuff

By Bruno Antunes

Crystal: like Ruby, but with types and stuff

  • 25