Crystal is a compiled, statically typed programming language that has a syntax very similar to Ruby. Crystal uses much of Ruby’s syntax and the object model, but it compiles to native code, which makes Crystal programs run faster than Ruby ones.
The dynamically typed nature of Ruby makes it a very expressive language. However, this expressiveness comes at the cost of performance, making it much slower than compiled languages like Go or Java.
Crystal has static types, which helps catch a lot of errors at compile time. Don’t worry, though - you won’t be writing verbose type annotations everywhere. The Crystal compiler infers the type of your variables, allowing you to take advantage of static typing while writing Ruby-like code.
Running your first crystal program
Let’s start off with a hello world program in Crystal.
You can run the program by using the crystal run
command.
Crystal programs use the .cr
file extension.
If you wish to only create the crystal executable,
you can use the crystal build
command.
This would create a compiled executable file called hello
,
which you can then run.
Creating a crystal project
Basic data types
- Number
- Boolean
- Tuple
- Boolean
- String
- Array
- Hash
- Struct
Arrays and hashes
Type inference
Methods and overloading
Generic types
Concurrency
spawn
creates a new Fiber to execute the block.
The program continues running after creating fiber.
Channels are a way to send messages to a fiber.
Metaprogramming
Links
- Crystal Docs
- Type Inference (part 1)
- Type Inference Rules
- Building a Realtime Chat Application Using Crystal and Kemal
- Crystal - Statically Typed Ruby - Slides from a talk by Vagmi Mudumbai