GoRuCo 2016

Micro Talk: Introducing the Crystal Programming Language

Introducing the Crystal Programming Language by Will Leinweber

Developer happiness is what brought me to Ruby in the first place. And of all the new compiled languages, Crystal is the only one that shares this value. The syntax and idioms are entirely Ruby inspired. Although Crystal looks very similar to Ruby, there are big differences between the two. Crystal is statically typed and dispatched. While there are no runtime dynamic features, the compile-time macros solve many of the same problems. In this session, we’ll take a close look at these differences as well as the similarities, and what Ruby developers can learn from this exciting language.

Help us caption & translate this video!

http://amara.org/v/Prke/

GoRuCo 2016

00:00:15.330 Thank you for the introduction. I'm really happy to be here. I've heard so much about this conference over the years, and I'm glad I could make it.
00:00:17.470 If you have any questions about the talk later on, this is where you can get in touch with me. Now, let's talk about Crystal.
00:00:25.510 I've been a contributor to the Crystal programming language for a little over a year and even wrote a Ghost driver for it because PostgreSQL is pretty cool.
00:00:35.410 So, what is Crystal? It is a language that is heavily inspired by Ruby, and if you come to it as a Rubyist, you'll feel right at home. However, it is compiled and uses a toolchain called LLVM.
00:00:42.460 LLVM powers a lot of the Apple ecosystem, PlayStation, and others. That's really important because many advances in other languages benefit Crystal as well, resulting in significant speed improvements.
00:01:02.800 Crystal is entirely self-hosted, which means that the lexer, parser, compiler, and standard library are written in Crystal itself. This makes it easy to contribute to and understand how things work.
00:01:25.600 One key aspect is that if you write your own code, the optimizations can extend to the standard library without any barriers, unlike the separation that exists when working with languages like C and Ruby.
00:01:47.260 The main difference, however, is that Crystal is statically typed with static dispatch. You might think this isn't very Ruby-like, but I will show you examples that demonstrate how it still retains some of Ruby's feel.
00:02:01.030 In many cases, you won't need to declare types explicitly due to Crystal's type inference system. It has a unique way of handling unions of types; if a method can return multiple types, it creates a union.
00:02:28.209 This allows it to feel like duck typing, making the language both fast and familiar. This is meant to get you excited about Crystal.
00:02:49.240 If we compare Crystal on the left and Ruby on the right, you can see that the structure and syntax are quite similar. For instance, it works like Sinatra in Ruby.
00:03:03.169 In performance benchmarks, Crystal is significantly faster and consumes less RAM, achieving two more requests per second while using two orders of magnitude less RAM.
00:03:45.019 Benchmark results should be taken with a grain of salt; however, they clearly indicate that even for regular codes, you can expect significant improvements.
00:03:58.159 Mike Herman, creator of Sidekiq, shared that just three days after starting with Crystal, he was able to build a minimum viable implementation of Sidekiq and experienced immediate improvements.
00:04:14.060 Regarding similarities, Crystal maintains object-oriented principles, blocks, and much of the standard library from Ruby. For example, this code in Crystal operates the same way it does in Ruby.
00:04:29.440 It even has a testing framework that enables you to write specs easily, giving you a familiar environment to work in.
00:04:48.680 However, it's important to note some differences that set Crystal apart from simply being a compiled version of Ruby.
00:05:00.680 For instance, the way properties are defined is slightly different, and there's type annotation involved. One appealing feature in Crystal is that you can simplify the use of instance variables with a straightforward syntax.
00:05:50.060 Crystal also offers a more elegant way for methods to accept blocks and chain requests, streamlining the syntax without needing to revert to the larger block syntax.
00:06:16.560 The biggest difference lies in its type system, as it is a statically typed language. This means that type checking occurs at compile time, enhancing performance and catching errors ahead of time.
00:06:38.520 For instance, Crystal can generate two different methods for a function that accepts multiple types, allowing for efficient coding while maintaining clarity.
00:07:05.480 Union types allow for flexibility in Crystal while providing the benefits of traditional typing. The distinction between compile-time types and runtime types helps prevent runtime errors.
00:07:30.260 In Crystal, if you invoke a function that relies on a specific data type and the wrong type is used, the compiler catches this error before runtime, ensuring safer code.
00:07:47.250 Moreover, Crystal allows type annotations, further organizing code and making it easier for developers to understand the expected data types while improving overall performance.
00:08:02.060 Through comprehensive type definitions and method overloading, Crystal simplifies coding. You can still maintain an elegant interface while avoiding cumbersome runtime type checks.
00:08:37.710 Macros in Crystal also allow for effective use of property readers and writers. For example, pretty-printing code can significantly enhance debugging by providing clearer outputs.
00:09:24.389 Linking to C libraries in Crystal is remarkably straightforward. Just a few lines of code can pull up functions and allow you to execute a query against an active PostgreSQL database.
00:09:51.389 This simplicity is particularly powerful for a new language like Crystal, which benefits from leveraging existing C libraries.
00:10:10.679 Lastly, one of the major distinctions between structures and classes in Crystal pertains to memory allocation. Structs are allocated on the stack, while classes reside on the heap and require garbage collection, influencing performance and efficiency.
00:10:30.520 With structs, you maintain efficiency by avoiding garbage collection, which enhances performance when working with many small objects.
00:10:45.580 Beyond that, string handling in Crystal also optimizes performance by rewriting constructs at compile time to prevent inefficient string concatenation.
00:10:59.560 Moreover, a lot of the concurrency support leans on the Go community's concepts, utilizing co-routines and channels.
00:11:20.130 So, if you're familiar with those concepts, you'll find them similar in Crystal. Thank you very much for your time, and if you're interested in trying Crystal, I encourage you to explore it.