 Hoping you're having a good time. So as Eric said, we're here to talk a bit about crystal I know if most of you have heard of it or not. It's a pretty new language, but it's also fun Before I get into crystal a little bit more about me really fast. So this is me This is my handle pretty much everywhere if you want to find me This is a company I work for cost of visual in Braga. They were kind enough to get me here So thanks to them. As Eric said RubyConf Portugal is happening in a month We're having a pretty good lineup. Okay, so after Grimm, Koishi, Sean Griffin Klamnick Among others PJ that spoke yesterday at RubyConf is gonna be the MC So the venue is fabulous. You should definitely think about going. Also, we're having a raffle Sometime today should be announced And we are giving away a free ticket for someone here. So Please be excited about that now crystal Okay, so crystal is an efficient Ruby like language. Okay, so really fast and Syntax similar to Ruby you're gonna see more about it in a while One of the different things about crystal is that it is compiled So and it is more strongly typed than Ruby So and it also has static checks which bring some more safety than Ruby does It also has very simple C bindings, which is nice for a lot of reasons that we might discuss in a while as well Another very cool thing about crystal is that it's a hundred percent self-hosted that that means that It's all crystal is crystal So if you understand the the syntax, which is again very should be familiar to you if you know Ruby It's very easy to just dig into the parser the lexer into the core of the language and just contribute Which is also very nice As I said crystal is a typed language Okay, but it does have a very good type Inference so you kind of all the time it feels like you're writing Ruby and not worrying too much about types If you don't want to which you probably will want to if you start writing a type code But that means that you can set your variables just as you would in in Ruby, right? So you have a number that's a 42 So it's an integer or a fixed number that would be the equivalent in Ruby in crystal It's called in 32, but it just works as you would expect it to work in in Ruby You can do the same thing with arrays Okay, so you have an array of numbers you can append to it again same syntaxes Ruby but Now the first small difference between crystal and Ruby if you check the class it's not not just array Okay, it's an array of in so we an array of integers This is a bit different and this is gonna be important in a while So here if this was Ruby you would expect it to work, right? So you have an array you append an array of integers you append an integer then you append a string and you get the class What could possibly go wrong, right? Well in crystal it does not work and it what this error. This is the error. It's gonna give you at compile time Okay, so this happens the program will not even run and what what's it telling you is that? You cannot append a string to an array of integers. Okay, so this is what it's telling you In crystal the type of The type inference system works when you're setting the variable So you set an array of integers you cannot then say oh, but I want the string as well. That is not safe Crystal will not let you do that. However. This is pretty easy to fix one of the ways would be to just Create an array that has integers and a string then crystal will know Okay, so you can see here that the type is not array of events anymore It's an array of these little strange thing which is a union type which are very important crystal This means that the array can either have strings or integers. Okay, and you can have more types as well Of course, this is not always the best way of initializing an array. Don't want to have Elements with all the types you need in the in the array so you can also do Do this? Okay, you can say that's an array of strings or ends and will work just fine as well Now that we know a bit more about how types work insist in crystal and the most important part here Remember is the union type. Okay, that's I think the most different thing But there's a lot that crystal can do for you at compile time now For instance, let's look at this Ruby example Very simple method called string size it takes a string and Prince the length of the string if you then try to call it two different in two different places If you run this code, you'll get 16. That's the length of that that string and then boom Okay, so you have an error undefined method lens for nil if you've been coding Ruby for a while you probably have seen a lot of similar errors and This is the problem here is not the actual error I think the problem is that it happens in runtime. Okay, so you might only find out when you're already running You already have deployed this code or if you have a very strong Test suite you might catch it early. Hopefully, but you never really know In crystal if we take like the exact same code It doesn't have the the prints there, but imagine it does You would get a very similar error ever saying that length is not defined for nil Okay, now we expect that however These are these error happens at compile time not run time. Okay, so the program won't even run It will tell you there's a there's a nil An undefined method for nil before it even runs. So that problem is solved Now let's take a look at a Bit more complex example taking the the same example from before the array of strings and ints and Let's go over what it's doing here. So we have an array of ints It takes an array sorry an array of strings or ints and it we append an int and then a string We take the array we call the we get the Ray and and then we ask for the size of that element if you do if you don't know it size is only available for strings not for ints, okay, and But this should work in Ruby right because the the last element is a string It has us the size method duck typing and everything it should work in crystal. It doesn't okay We get undefined method size for ints. This is because Crystal makes sure that any method you call on an element of the array must be valid for all the types in the union Type of the array. Okay, so it's really safe that way because it cannot really make sure that you're not messing it up along the way So if you if you're doing something that can potentially introduce a bug It will not let you run the program C bindings as I said like crystal has a very good integration we'd see This is because crystal is a Compiles to LLVM bytecode. So it's really easy to integrate it see it's gonna be very similar But if you ever try to do it to have tried to do it to Ruby You might have noticed that is not that easy not very easy to get started anyway You probably will need to write some C and worse than that You'd need to learn like Ruby syntax or Ruby see implement implementation like the RBN needs and all that Basically, it's a bit of a pain. I think not fiddle is something that's happening and it looks nice I think we have a talk tomorrow about fiddle. I haven't given it a Close look, but it looks better. I think But in crystal is it's already very simple to integrate with C for instance. Let's say you want to have the a to I function that from the standard Library standard C library That's it. It just say that you want to use libc You declare the header of the function and you can use it under the libc namespace Okay, this is a bit cheating because libc is already in the core. So you can just use it However, if we wanted like a random C library, we needed to use it to be a Bit trickier like one line extra You have to call that link on top for instance if we need the read line This is an example of using the read line C library and you just need to link it and Declare a header again. That's a function for read line. And then we have a code just using read line this would prompt for a name you can give it a name and it will print it as a string the Small like difference you have to note here is the string new That's because if you if you also notice on the header, we're saying that the return value That's what's coming after the the colander. So the pointer you end so we get a pointer back and So we have to cast it to a string so we can use it as a regular string but other than that it's very very simple to use and Most of the core of crystal actually uses this for a lot of reasons some some Some of the parts of the code they didn't yet have time to rewrite So we can you can just leverage a lot of what the C community has done Which is a much bigger community than community than what crystal is right now Another important part of of crystal are something called generics that We don't really have in Ruby But there are in other languages and the idea is that let's stick these Ruby code This is a full class very simple class You have a not to reader with a value and you have an initializer that takes a value So very very simple class and then we use it okay We create the class we create an instance of the class with an integer and We get the absolute value of the for the for that for that integer. We get one fine Then we create another instance with a char and we get the ASCII code for that char fine It works. Well, no problem there everything in in ruby just just works However, if you try the same exact thing in crystal minus the slight differences in the syntax, which We don't have the utter reader. It's called getter and you have the setter and property for the utter Accessor so it but it's very similar and we have the coffee scripts like like initializer We don't have to set the the value and like repeat yourself three times. You can just do it once But then if we try to the same exact code create a foo for in then a foo for char and call different methods on it on different instances It won't work. Okay, we get the similar error to what we had before We have an undefined method ABS for char Why is this this is because of how again how type inference works in crystal crystal needs to find a type for this value, right? You're not saying what the type of the value is of the value variable The way crystal does it is it goes through every every place where you initialize this class and It sees the the types. So here's an integer. Here's our here's a char So what's the type of value? Well, it's a union of ins and chars. Okay, so This won't work because it's not available for chars Also, again, this is a compile time error. So these won't even run You can fix it pretty easily by having some something called generics So you can say this is a foo of T of type T like a generic type And you say that that T is gonna be defined by the type of the value What this is telling crystal is that each instance of foo can have a different type a type T That is defined when we initialize it and it's defined by the type of the value So in this situation this instance of foo will be a foo of int and this instance of foo will be a foo of chars and then it will just work again like it works in Ruby This is how arrays are implemented. This is how hashes are implemented So each instance of an array can have its own type and it can be an union type Okay, so we saw arrays that had integers and and strings So those are arrays of union types However, we do not have an array with all the types that we use in all arrays. So that would be a mess another difference between crystal and Ruby that I find really interesting is That you can you have method overloading and if you're not familiar with method overloading basically It means that you can define the same method over and over again number of arguments so the arity If he has a different number of arguments, it's a different method It has different types even though it has the same number of arguments. It's a different method What in crystal we call like the yieldness of a method So if a method yields to a block then it's a it's different even though It might have the same number of arguments and the same types if it yields and another method doesn't they are different What this means is that we can you can do something like this Okay, so of course this is biased towards the crystal implementation But you can see the same thing written in Ruby and in crystal. So you have a some Method that can take different number of arguments It can take different types of arguments so you can pass it in strings or integers and it will just work In crystal is very easy. Okay, so it's one line per method And you just do everything on the header of the method definition In Ruby you have to get everything and then do it inside of the method This is I think an interesting thing and would be nice to have in Ruby. Okay last part of of my talk is starting and it's about charts So what are shards charts is the name we we've got for gems in in crystal Okay, so if you build a library in crystal for now at least it's called chart and how does like dependency management work for now and again crystal is a very new language that's evolving a lot so the the project the dependency system is not Great yet, but the way it works right now is that you need to have a file called project file Similar to the gem file has its own DSL you say depths and then you see there you can get a Library from github as long as the library is well built The the thing about crystal though and then you can just use it like can require it and use it the thing about like dependency system as it is right now is that Probably how it will be is that it only works on github and bit bucket and all that but the idea is that it's a decentralized system So there's no ruby gems. There's no place where you have all the meta information I don't know if this is gonna be like like that forever But right now it is there's a lot of there's a side project called charts going on That's trying to build a better dependency management system and Which are hard if you have have tried to do one and So not really set in stone how this works But the the idea of the of the creators of the language is to make it decentralized So you just have you have a repo on github a repo on bit bucket. You just use it. There's no central place Where everything is? This is all fun and nice, but it makes discoverability really hard Because you can just you can use Ruby gems and just search for a gem for instance And you cannot do that in crystal because there's no central place. You could search in github, but that's more painful so there's a website called crystal shards and you can Look at shards in Different orders like you can see the ones that have more stars so you can see the more more popular shards You can see the ones that have been more recently updated the ones that have more forks you can see a lot of stuff here and This works for now at least just because there are not that many you can see like a hundred and eighty shards So that's not a lot But it's growing. It's growing like in maybe two months or three months You got from 60 to 180 something a community that's thriving right now So we're very very good time to come to to the community and I like to stress that that there's a lot of Opportunities if you like to contribute to open source or you want to start contributing to contributing to open source Because there's a lot of stuff that is not done in crystal So even in the language in the core of the language you can contribute to that That's there's a lot of stuff to do there, but even in if you want to build libraries to do stuff for instance maybe Six months ago. There was no postgres adapter So someone started building it, but there's my sequel adapter now as well There's a lot of here you can do if you like and that kind of stuff Okay now I'm gonna do a really quick demo and try not to blow too much things Let's see if we can do this Okay, is it the font is the font good enough can you see it there? Okay, so I have here a Fibonacci sequence generator. Okay, very simple a recursive one It's very simple and it's written in Ruby and it just it's printing the element Number 40 of the sequences so it generates a sequence and it gets the 40th Elements and it prints it. Okay, so very simple code. It should work just fine If we do it in Ruby You have to wait a while not too much hopefully about Sometime Okay, it worked. I forgot to put the time here. Let's do it again We have some time it should be about eight seconds if I'm not mistaken or it might be taking longer because I'm not plugged in Okay, nine seconds So this is the Ruby implementation, okay, and if you notice it's the 40th if you try to do like the 45th You might just grab a share and wait Because it's gonna take a while In crystal we can try it Okay, we can just pass in in this particular instance because it's such a simple Program that the code is exact the exact same in crystal so you can just pass it in a Ruby file and it will just work and Okay, it took one second However, what we are doing here is that we are compiling the program and then running it so we can compile it first So you can do crystal build that will compile your file and you also have a release Flag that makes it even faster. So we compile it and then we can time it boom 0.2 seconds and it does the same thing if you notice like it generates It's here. So and it took the same code. So it's very very fast especially especially for CPU bound Programs you can almost write Ruby and make it like super fast So please try some crystal and Hopefully you can get we can get some of you to the community. That would be great Another thing that I can mention that is super fast in crystal is Jason parsing So if you have a lot of that crystal is super super fast doing that as well It has even some like a small DSL that lets you define the structure of your Jason and it's super fast Parsing that so that's a very good use case to start using crystal if you have one if you have that problem Okay, that's it. Thank you So I will be do the be doing the question moderation. Are there someone sending already great Louis, thank you Personally, I had the impression that the type system looked like the one used by the ML language Where does crystal draw its inspiration for the type? So I have to repeat the question right or it has okay So what why is where does the type language the type system come from in crystal? To be honest, I'm not really sure So but it takes a lot of inspiration from a lot of languages I know it takes inspiration from from Ruby obviously from coffee script. It takes some inspiration From Elixir it takes some inspiration maybe from there it got kind of a bit of the type system and not the Creators of the language are really going around like to conferences and talking to people about new ways to do languages It has it also a thing. I didn't mention here, but crystal has a macro system so it can get You can do some fancy stuff at compile time And it also takes that from other languages. So there's a obviously it's a it's a new language So it takes a lot of from other languages not I don't know for sure what comes from which language Any other questions? One of the back there. Come here. I meet your halfway Thanks, just a short question. It looks really great I have a question. Do you have a community code of conduct for your developers? Sorry I have a question if you have a community code of conduct for your developers and Do you have a core team and what's the gender distribution? Sorry, I didn't understand the question. Sorry. Could you repeat? Do you have a code of conduct a community code of conduct and do you have a core team and How does the gender distribution? Oh, okay. Okay, thanks So yeah, that there's a core team. I'm not on the core team, but I do help and I'm don't think there's a kind of conduct actually or if it is it's pretty recent as Far as distribution goes. It's pretty bad. So sorry the diversity or distribution of It's it's Can you hear me? Yeah, it's it's mostly guys. So but please join we will I think For sure so if you There's a lot of of women here. So please join we're the community is very is very friendly I think to everyone and very welcoming. It's just it's just a small community and just happened to be that way There's I don't think there's any like bias towards any particular So it's a very young language and there needs to be a lot of work. So this is one of them any other There you go You mentioned it's a young language is to the creators considered production ready. Sorry the creators do is it considered production ready? Oh production ready That's a good question. So it's not one one point oh yet. So it's not a hundred percent stable, but I think it is ready like first small Things like I was saying like if you want to parse Jason You have like a microservices that parses Jason ready for that to build like a whole app in crystal Probably not a good idea yet Also because you don't have a lot of frame that you have you have some frameworks now There are starting to to be built but very very very early stage. Everything is very early stage So you to do small scripts and to do a small? subset of things Awesome Actually like crystal shards is built on crystal and it's has been running for a couple months So it does work, but I wouldn't advise it to build it like your business on top of it yet Please don't do that yet Good point one more question Are there some known? Limitations in made up programming Things where you can say from the beginning this will never work in crystal compared to Ruby Just because of its concept Well, there's like a fall stuff like that Some of them it's not all but some of the metaprogramming thing you can do in Ruby Because crystal needs to like can only take information that it has at compile time. So anything that you need Runtime information to be able to do it won't work like for instance There is a method missing in in crystal, but is limited compared to The Ruby method missing you cannot do everything Ruby method missing does I mean, it's not as powerful as Ruby is. I think that's the main thing. Thank you, Luis. Thanks