Talks

Crystal: A language for humans and computers

Rubyist, Crystal Evangelist, Creator of Kemal – a lightning fast, super simple web framework written in Crystal.

Balkan Ruby 2018

00:00:08.420 Thanks, everyone.
00:00:14.000 Hi again! It's my first time being in Sofia, and so far, it's been amazing.
00:00:20.760 The people, the places, the culture, especially the food, is wonderful.
00:00:26.160 I really like it here; it feels like I'm at home.
00:00:33.829 It's been a great journey for me.
00:00:39.899 I would like to thank everyone for organizing the first ever Balkan Ruby and inviting me.
00:00:44.910 Also, I'd like to thank you for coming to listen to us.
00:00:52.530 Today, I'm going to be talking about Crystal. As you can see from my t-shirt, I'm really excited about it, and I want to share my journey and experience with you.
00:01:07.470 So, do you know where I come from? This is Istanbul, and it's such a unique place.
00:01:14.640 I believe it's like the center of the world. You can debate about it, but it's the only place where you can cross continents on a daily basis with a bus.
00:01:22.170 This is the Bosporus bridge, which connects Europe with the Anatolian side.
00:01:27.600 For four years, I crossed continents, commuting about four hours a day.
00:01:33.900 I'm pretty sure most of you are familiar with Turkish cuisine. I learned that you have sarma, baklava, and kebabs.
00:01:48.030 One of my favorites is mantı. I'm not sure if you have it, but it's great.
00:01:55.500 So, I really enjoy food, and I want to welcome you all.
00:02:06.070 Also, my name is hard to pronounce. It’s Sardar Doğruyol, but you can call me Seto, which is a combination of the first two letters of my first and last name.
00:02:24.580 Most of my friends, my mother, and my brothers call me Seto, so please feel free to use it.
00:02:39.430 You can find me on GitHub, Twitter, and LinkedIn. Don't hesitate to reach out.
00:02:49.270 This picture captures one of the happiest moments of my life. I was presenting at RubyConf back in 2016.
00:02:57.610 I was even more fit in that picture!
00:03:03.400 I have recently joined the Crystal programming language team as a core member.
00:03:12.310 You can see me active on the issue tracker almost every day, so feel free to say hi.
00:03:18.760 I'm also the author of a framework called Kemal, a web framework written in Crystal.
00:03:24.100 It's inspired by Sinatra from the Ruby world, and I've maintained it for over three years.
00:03:30.970 Currently, I work at a startup called Rainforest, which is based in the U.S., but I work remotely from Istanbul.
00:03:39.070 We provide QA as a service, and you should check our website; it's pretty interesting.
00:03:49.390 I also enjoy learning languages and consider myself a polyglot.
00:03:55.709 I believe some of you can read Japanese, as this word, 'polyglot', is written in Japanese.
00:04:05.950 Learning languages is fun, but enough about me; let's talk about Crystal.
00:04:12.190 How many of you have heard of Crystal before? Wow, that's good! It seems we are doing a good job.
00:04:21.440 First things first: Crystal is not Ruby, and that is certain.
00:04:29.360 It’s not a type of Ruby; it’s a different language, but it takes many idioms and ideas from Ruby.
00:04:34.610 This is because Ruby is an awesome language.
00:04:42.020 In the past, we branded Crystal as fast as Ruby, but that turned out to lead people in the wrong direction.
00:04:49.160 People thought it was just another implementation, like MRI or JRuby.
00:04:54.290 However, Crystal is not just another implementation; it is a different language built from the ground up.
00:04:59.480 We now call it 'a language for humans and computers,' which is unique.
00:05:11.150 There aren’t many languages that share this attribute.
00:05:17.170 You might be asking why. I have some strong proof.
00:05:23.740 How many of you have seen this graph? This graph explains everything scientifically.
00:05:30.490 The developer satisfaction in Ruby is declining.
00:05:41.270 Sorry for the poor quality of the image; it’s generated by a Ruby program. Just kidding! Check the website 'Ruby is Dead' to see more of this.
00:05:49.580 I can assure you, Ruby is alive and kicking.
00:05:54.950 But the real question is why.
00:06:01.370 Building a language is more than just writing code, building compilers, and other technical aspects.
00:06:10.040 It's really about community and the people involved.
00:06:16.940 When we look at Crystal, there are main reasons why it stands out.
00:06:22.610 First and foremost, we are here because of Ruby's efficiency.
00:06:29.300 Ruby gives us a lot of productivity, and we want something similar.
00:06:36.409 We love efficiency, and we want the best of both worlds.
00:06:49.069 We want the compiler to understand what we mean without having to specify everything in detail.
00:06:57.680 Type systems are valuable; we wish to avoid the pitfalls of too much boilerplate.
00:07:06.439 We value DRY principles, especially in mathematics.
00:07:14.000 The last thing is that we don't want to write C code to make things faster.
00:07:22.879 So, how is this possible? The good thing is that Crystal is real.
00:07:29.029 It's not just a toy project; it’s being used in production by companies.
00:07:36.379 So how is it functioning? The simple answer is that it's built on ideas from Ruby while using LLVM.
00:07:41.870 For those unfamiliar, LLVM is a compiler infrastructure toolchain that many languages, such as Swift and Objective-C, utilize.
00:07:56.750 It is a solid project, backed by corporations like Apple and Google.
00:08:03.580 The original creators of Crystal thought about taking Ruby and making it work.
00:08:11.719 It sounds crazy, but I love this idea.
00:08:17.509 They believed that all our dreams could come true if we had the courage to pursue them.
00:08:24.169 It's mainly attributed to two guys from Argentina who had the visionary idea of creating Crystal.
00:08:30.420 Crystal doesn’t have any big corporation backing; it’s just a community effort.
00:08:35.610 In fact, six years ago, a brilliant and friendly individual named Ary committed the first initial commit on September 5, 2012.
00:08:50.280 You can consider Crystal a young language, but it has matured significantly in its short life.
00:09:03.090 One year later, the first version of Crystal was released, and it was initially written in Ruby.
00:09:08.310 Ary had a fondness for Ruby, and you could see that the project was heavily based on it.
00:09:15.570 Eventually, Crystal became self-hosted.
00:09:23.460 Self-hosted languages are written in their own language, which means Crystal is capable of writing and compiling its own code.
00:09:34.200 As seen from the master branch, about ninety-eight percent of Crystal's codebase is written in Crystal.
00:09:40.750 This allows Crystal to give you the ability to read its source code, which is fantastic.
00:09:46.340 I loved Ruby and wanted to contribute, but learning C extensions was quite challenging.
00:09:53.490 Therefore, I preferred a self-hosted language instead.
00:10:01.710 In April 2015, I was looking for something new that had the best of both worlds.
00:10:07.710 I discovered Crystal, and it truly changed my life.
00:10:13.329 It felt like I had found my calling.
00:10:21.420 Fast forward to today, it's been more than three years since I joined the community.
00:10:27.370 We are significantly more established as a programming language now.
00:10:32.470 Let's take a look at some statistics.
00:10:39.519 We have over 250 contributors from all around the world.
00:10:44.949 We have more than 10,000 stars on GitHub and over 2,500 packages.
00:10:53.540 As of now, it is actually more than 3,000 packages.
00:11:01.110 We rely on community donations and have raised over $2,000 per month.
00:11:06.750 We're grateful to everyone who contributed.
00:11:14.220 This year, we've seen tremendous growth.
00:11:21.060 Lastly, we have a core developer working on Crystal full-time.
00:11:27.990 It's fantastic that we now have sponsorship to support our developer.
00:11:34.110 We have an exciting new release coming next week, version 0.25.
00:11:39.120 We have optimized many features, and I hope we can successfully release it soon.
00:11:46.340 Now, you might be wondering what makes Crystal stand out.
00:11:53.270 I'll give you an overview, but I'm not going to delve too deeply.
00:11:59.320 First and foremost, it's human-friendly.
00:12:04.950 Drawing inspiration from Ruby, which is considered one of the most human-friendly languages, Crystal seeks to emulate that.
00:12:11.750 For instance, this simple statement prints 'Hello,' which is very approachable.
00:12:19.540 Similarly, this line represents a complete HTTP server written in just five lines of code.
00:12:26.560 This simplicity makes it easy to use.
00:12:29.390 Moreover, Crystal features a powerful type system. It is strongly typed, unlike Ruby.
00:12:39.200 Narratively, the type system looks familiar because you don't need to specify types explicitly at most times.
00:12:46.500 The compiler is intelligent enough to infer types and optimize them.
00:12:54.920 You can also check the types at compile time through methods.
00:13:00.790 Crystal offers a range of numeric types, including integers and floats, and we are currently implementing 128-bit integers.
00:13:07.300 The array types also offer flexibility, allowing for a combination of various data types.
00:13:14.180 This feature gives it a dynamic language feel, permitting a great deal of simplicity.
00:13:21.030 One of Crystal's greatest features is Union types.
00:13:29.409 They have been described as the missing piece in many compiled languages.
00:13:32.879 For example, consider this expression: if one plus two equals three, then one else a equals 'hello.'
00:13:40.039 The compiler must output all possible types, so utilizing Union types solves such problems.
00:13:47.880 Like this, the type can be expressed as an integer or string.
00:13:56.000 This is quite flexible since you can use as many variants as you want.
00:14:04.050 The compiler optimizes the data types for better performance.
00:14:11.370 If you look at runtime values, it's still just an integer.
00:14:17.150 Another feature I really like is method overloading.
00:14:21.720 How many of you have used method overloading in any language?
00:14:27.600 It's quite useful, especially for making code cleaner.
00:14:34.620 In Ruby, we often face cumbersome management of multiple types.
00:14:37.200 In Crystal, you can define overloaded methods clearly with differentiating type signatures.
00:14:45.340 You can specify the return type after a colon, allowing flexibility in its use.
00:14:52.420 The compiler ensures that you call the appropriate method matching your types.
00:14:59.600 If a type doesn’t fit the overloads, you won’t compile, meaning you handle errors more easily.
00:15:06.160 Next, let's address reference checks.
00:15:13.540 Many of us experience difficulties with nil handling in Ruby.
00:15:20.870 In Crystal, the compiler ensures that you correctly handle potential nil cases.
00:15:28.260 For example, if an expression can yield a nil value, the compiler will flag that potential issue.
00:15:37.020 This prevents undefined method exceptions by verifying types at compile time.
00:15:45.170 Adopting macro-based metaprogramming further values performance.
00:15:50.390 Macros are processed at compile time, eliminating performance penalties.
00:15:57.139 Unlike Ruby, you get to define methods dynamically without runtime performance hits.
00:16:05.050 The cool thing about this approach is that if there's a problem with your syntax at compile time, the compiler will let you know.
00:16:14.140 Now, let's talk about open classes.
00:16:22.340 Some developers love monkey-patching… and others loathe it.
00:16:29.930 The point is, Crystal allows you to reopen a class and define methods similar to Ruby's behavior.
00:16:36.690 No performance is lost on that front either.
00:16:41.679 Built-in concurrency is another noteworthy feature of Crystal.
00:16:48.240 Crystal utilizes fibers, which are lightweight threads inspired by ideas from Go and Closure.
00:16:55.190 Being cooperative and utilizing message passing, it avoids shared memory issues.
00:17:01.520 This model follows the Communicating Sequential Processes (CSP) paradigm.
00:17:09.010 Let me show you how it operates with a simple concurrency example using fibers.
00:17:15.960 It is implemented with channels to send messages between them.
00:17:22.250 You can pass values and receive them back using channel receive.
00:17:29.920 The operations are blocking, ensuring data consistency.
00:17:36.480 Currently, Crystal is not multi-threaded but leverages an event loop, making it exceptionally fast.
00:17:44.300 Now let's discuss native code.
00:17:51.170 When you use Crystal, it produces a single native executable tailored for your platform.
00:17:58.020 It can create an ELF for Linux, a Mach-O for macOS, or a compatible executable for Windows.
00:18:05.940 You can simply use Crystal build to create a standalone binary.
00:18:12.210 But the Windows port is still under development.
00:18:17.270 The community is welcome to contribute in aid of this port.
00:18:23.040 Additionally, we have C bindings that make it easy to use C libraries.
00:18:30.260 This means you don’t have to write extensive C code to interface with C libraries.
00:18:37.270 Here’s a simple binding from Crystal to the LibGMP library.
00:18:43.640 You can leverage the 'lib' keyword and access its methods easily.
00:18:50.770 Creating bindings is straightforward and quick, as demonstrated by my friend's PostgreSQL binding.
00:18:57.740 Now, when it comes to testing, we do have high hopes.
00:19:05.200 Crystal has a built-in testing framework called Spec.
00:19:12.670 It's akin to RSpec, though more straightforward.
00:19:19.080 You would work with describe blocks but without the added context.
00:19:25.180 You can write tests directly in Crystal's source, enabling clear visibility.
00:19:30.150 To add dependencies, we use 'shards.'
00:19:36.270 When you create a new project, the command generates a shards.yml for package management.
00:19:42.920 It fetches dependencies directly from sources like GitHub.
00:19:48.500 The crystal shards XY is an excellent place to find and explore available packages.
00:19:55.260 And today, we already have over 3,000 up-to-date shards.
00:20:02.750 It's steadily increasing, showcasing our active community.
00:20:10.490 Some features bring a visual pleasure similar to what Ruby offers.
00:20:19.030 Crystal Play, an interactive playground for experimentation, comes built-in.
00:20:26.590 Once installed, just utilize 'crystal play' and have fun!
00:20:35.310 I wish to demonstrate it here, but unfortunately, my PC is having issues.
00:20:42.250 The overall development experience is enhanced with built-in formatting tools.
00:20:48.980 Whenever you save a file in the editor, it formats automatically.
00:20:55.840 It's a feature I truly admire and wish Ruby had.
00:21:04.090 And in Crystal, we maintain coding standards by using double quotes exclusively.
00:21:11.450 Furthermore, it reads like English; there are no odd inclusions as seen in Ruby.
00:21:19.540 By avoiding global variables, we ensure better data integrity.
00:21:27.680 Last year we made a conscious decision to remove global variables from our codebase.
00:21:34.600 Instead, we now rely on classes and static variables.
00:21:41.250 Now, you may wonder if anyone actually uses Crystal in production.
00:21:51.140 No surprise! Many companies actively use Crystal.
00:21:58.300 This is rapidly increasing; we have over 30 companies using it as we speak.
00:22:05.150 Crystal is applied in various sectors, not just the web domain.
00:22:11.830 Cybersecurity and AI companies are also adopting Crystal.
00:22:20.760 For example, a startup called Deploys uses it for DNA analysis.
00:22:29.920 They transitioned from Ruby and reported a tenfold speed improvement in Crystal.
00:22:39.000 Imagine real-world cancer patients benefiting from Crystal code! That's incredible!
00:22:48.740 Regarding performance, I opted to showcase real use cases instead of benchmarks.
00:22:55.790 In my current role at Rainforest, we looked at several projects to improve them.
00:23:03.040 We had one service that was critical and operated with a 700 ms 99 percentile response time, and I rewrote it in Kemal.
00:23:11.600 Now, our response time is down to 50 ms on average.
00:23:22.470 The graphs reflect performance gains and drastically reduced memory consumption.
00:23:31.340 While it previously averaged around 100 MB with Ruby, it now operates at a mere 2 to 3 MB.
00:23:39.150 The garbage collector also performs quite well.
00:23:44.780 Featuring a lightweight framework like Kemal positions Crystal competitively.
00:23:51.440 Looking at benchmarks from TechEmpower, Crystal is comparable to languages such as Go and Lua.
00:23:58.050 Despite being single-threaded, its fibers are impressively efficient.
00:24:04.650 The results are promising, indicating a bright future.
00:24:10.210 Now, for those seeking resources, we offer several.
00:24:19.000 We have a comprehensive Crystal book available on GitHub.
00:24:27.700 Additionally, I wrote a guide titled 'Crystal for Rubies,' which offers a quick, free read.
00:24:34.990 It distills major concepts and is available in PDF and Kindle formats.
00:24:42.450 We also have an active community channel where you can engage.
00:24:51.480 There are over 500 participants currently, and it's a growing resource.
00:24:57.630 I have stickers here for anyone interested!
00:25:02.860 On a related note, I'm writing a book called 'Learning Crystal Programming.'
00:25:08.700 Though still in progress, I hope to finish it soon.
00:25:15.040 Writing a book is undoubtedly a challenge.
00:25:23.760 Thank you for listening, and feel free to ask any questions!