Crystal 1.1.0
crystal-lang.org
crystal-lang.org
* Syntax: Crystal’s syntax is heavily inspired by Ruby’s...
* Type System: Crystal is statically type checked...
and so on
I don't really see a "why use Crystal instead of Ruby" (or any other language, really) explanation anywhere. Maybe the things listed on the main page are meant to be that? Even if it's not on the main page a comparison would be nice in the docs (https://crystal-lang.org/docs/) or the reference (https://crystal-lang.org/reference/)
Or, with apologies to the late Douglas Addams, Crystal is almost, but not quite, entirely unlike Ruby.
Crystal is a programming language with the following 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.
Why? 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.
Matz even mentioned Crystal as a study case while explaining what Ruby's static type system would be like - back in 2016[1].
[0] https://manas.tech/blog/2016/04/01/the-story-behind-crystal/ [1] https://youtu.be/1l3U1X3z0CE?t=1755
That said, I do think they should put the "why" in clear text. Ruby made no apologies for being a language "for the programer" while Crystal is meant to be a language "nice for the programmer with slick syntax and a modern type system AND compiles to very efficient bytecode that's comparable with C (at the cost of more time spent compiling to reach those goals)"
What puts me off delving further is the lack of support in various editors / IDEs / Treesitter etc. I think the core development team should really devote some resources to this, as I'm sure I'm not the only one deterred by this dearth of tooling.
Time to compile an optimized binary will be still slow for the foreseeable future.
You can follow the activity here: https://github.com/crystal-lang/crystal/pull/10910
I was pleasantly surprised myself that the interpreter actually already somewhat worked, as you can see in the demo: https://www.youtube.com/watch?v=een_W1YEICw&list=PLe1-A91ZPT...
The creator also claimed that the interpreter only need a few more months to finish.
It’s really fun language, and I don’t have even have any Ruby background!
The main pain points from where I’m sitting are things like debugging and incremental compiling. You can throw lack of tooling in there too I suppose.
The dev team knows and is working on these, but they are a small community based team so don’t expect they same resources as in Go or Swift.
FWIW I’ve been having good experiences with Sublime.
Crystal could take a leaf out of V or Rust's book here. Whatever you think of the languages themselves, the error messages are top notch. Errors that say "Ah. I see what you were trying to do here. Maybe you should try this instead?" are so much more helpful than "Why on earth did you do that?!"
Beginning of a tree-sitter parser here too: https://github.com/keidax/tree-sitter-crystal
https://twitter.com/postmodern_mod3/status/14176736892444672...
[1] https://twitter.com/ioquatix/status/1386594287085780992?s=20
To give a bit of history of myself and programming languages, I first started as an EE/CS major working with ASM/C/C++/Matlab for EE and C++/Java/Lisp for CS. Up until graduation, it was just a given that programming languages were just a concession given to you so you didn't have to write assembly. OO in the form of C++/Java was the "better alternative" to imperative C. Lucky me.
Then I heard about Ruby and Ruby on Rails. After reading "The Ruby Programming Language" and working through the examples, I was blown away by the idea that a language could be for a programmer and be a way to express ideas rather than something we translate ideas into. Metaprogramming and mixins and Procs oh my.
Then I started working through Project Euler questions with Ruby and it all came crashing down with the reality of Ruby's runtime. Ruby gave me expressiveness but just could't give me the speed of my equivalent C/Java code. Granted, I was definitely doing very sub-optimal algos but even then, C/Java just didn't care and gave the answer in seconds, not minutes.
Since then, Java & JS have paid my bills. I've seen the popularity of Ruby die as Rails fell out of style when Twitter moved away.
I've messed around with Crystal, trying to work through the problems in "The Go Programming Language" in Crystal and finding that Crystal is a beautiful language and fast as hell. That said, there's always a trade-off right? Crystal's trade off is in compile time, especially with --release. However, I think that's a reasonable trade-off since your release builds should be on a beefy server and it could save you $$$ if it means your actual production servers can be lean as hell. That said, I'm also aware that this problem is exactly one that the Go language was meant to address (C++'s exponentially growing compile time).
Crystal definitely needs:
1. IDE/Debugger support, understandable for a new language, but the language should be "finished" and tooling needs critical attention
2. Libraries. The classic problem of any new language.
3. Major sponsorship (would greatly help with point 1 & 2)
Not sure how active it is, though.
Pros:
- Elegant syntax. Crystal is highly inspired by Ruby and it has a lovely elegant syntax. Syntax is important to me and the clutter of symbols and the busy syntax is why i struggle with Rust.
- gorgeous type system. F# is the only other language I can think of which has this lovely and complete a type system. The way Crystal handles Nil (its a type) just feels great in practice.
- Perf wise, Crystal is blazing fast. It seems to be marginably faster than most compiled languages like Go, Nim etc in the benchmarks. Though at that level the difference isn't much.
- batteries included stdlib
- decent documentation. It could be better but imo its good enough. I rarely have to look beyond the documentation if I get stuck anywhere.
- small but amazing community. the folks in the community like Ali, blacksmoke, Oleh, Ary and many others etc are super helpful and always ready to teach a newcomer.
Cons:
- Lack of Windows support. The biggest elephant in the room. the good news is that this is under development and even in this release a lot of progress towards a Windows event loop was made. But if you are going to do web dev, you can always go with wsl for the compilation as most of us do.
- Slow compilation speed. Its faster than Rust/C++ but if you are coming from an interpreted language the 5-10 seconds it takes to compile your small project rankles a bit.
- DB drivers need to be better. They currently don't support pipelining, ssl, async etc. Not a big deal as the Crystal drivers are performant enough. but I think if these were there, Crystal would get at least 20-30 ranks in the techempower Fortunes benchmarks.
- IDE tooling is not yet there.
- Small community and hence lack of community packages. Often stuff will have to be done by hand. Things like oauth, openssl conversions etc will need some work from you.
- no http/2 support
- The lack of a BDFL and corporate sponsorship. This means that the language growth is less structured and highly dependent on the community leaders.
Here is an old comment I wrote about Crystal on HN. it is still as true today as it was then;
----------------------------------
I have a love-hate relation with Crystal. Every few months, disenchanted with the core maintainers' priorities, lack of platform and tooling updates and overall deadland syndromes - I denounce Crystal, promise I will never use it again, startup a golang or Rust project, make some non trivial toy stuff and then come crawling back to Crystal. I hate the fact that I love this language and the fact that nothing else (except perhaps f# and nim) seems to have the same effect on me.
My favorites Crystal features are its do-end syntax, dat sexy type system hnnnnghhh... , domain modelling using sum types, null checking using types, ultra simple OOPs, insanely productive std and its sheer performance.
The bits about the language I detest are the overuse of macros, the utter lack of any platform (windows, http/2 etc.) or tooling improvements (IDE support, slow compilation). The core maintainers are amazing but have a weird obsession with just refactoring the language semantics. Things which really would matter for any language usage are just relegated to GH issues which haven't had any comments on it for months (if not years).
The lack of a BDFL and corporate sponsorship really hurts Crystal bad. Its a language without directed growth. For a language which seems to be used the most for web servers, it lacks http/2, db pipelining and async db drivers. There have been plans for redoing the http module for years but it hasn't been done yet.
Yet, I love this language despite all its shortcomings. I find it to be one of the most readable languages out there. The community is full of amazing individuals who are ever helpful and welcoming. The core maintainers are super talented developers who really value quality of code.
Right now I am working on an api servers and have 3 early implementations - one in Rust-actix, one in pure crystal (no framework) and one in Go-Fiber. The joy of using Crystal and feeling like I am in control of the project are reasons which are pushing me towards using Crystal for the project. But I know that by the next few years, we likely still wouldn't have http/2, async drivers, nice IDE support and many other features that I really need.
And so, I will likely have to go with Go. (Rust syntax is just too complex for my taste. And I am not talking about the celebrated borrow checker).
I like go.
Go is simple.
Go is Productive.
Go is... just not Crystal.
-----------------
Epilogue: I did not go with Go. Its error handling and opinionated formatting turned me off so much that I have returned back to crystal. :/
Prologue is the most promising one IMO, but it is still very new and its performance isn't as good. But I do keep a check on it.
Overall, Nim needs something like Lucky, Athena, Azu Toolkit etc to feel like a serious web dev language.
That said, I have always been thinking of rolling with just the stdlib stuff so I may do that one of these days.
I agree that a nice website would be great to have for Jester (and I do intend to create one), but I think you place far too much importance on Jester having one. Jester/HttpBeast is used in production by big web apps[1], why doesn't this inspire enough confidence and possibly even more than the other web frameworks you've mentioned?
I think there is an inherent bias that all humans have for shiny design, you should definitely keep it in mind when choosing a technology.
However, considering the huge amount of languages/webframework pairs (D+Vibe? Nim+Jester? Crystal+Whatever? Go? Rust? etc.) available today, it is understandable that people pick simple criteria like this to decimate their number of choices. The alternative to start playing with each tool for a few days to rank them and pick the best choice is obviously not possible.
Although I use Rust most of the time, I keep having a look at Crystal and appreciate its simplicity and can get alot of things done with it rather than fighting with the borrow checker.
Very happy to see it reach 1.0 and further.
> and corporate sponsorship
Well there are companies (and smaller individuals) that are still sponsoring it every month. [0]
(disclaimer: I was once a coffee sponsor of Crystal myself)
I see, So: "If a company that I don't know about is putting money into a language, it's doesn't really fit my definition of 'real' sponsorship"
It still counts as sponsorship whether if it is a FAANMG company or a tiny startup and there are companies that use Crystal that are sponsoring it monthly. No need to move the goal posts here.
Probably the Golang people did the exact same comparison to Rust when that reached 1.0 in 2015; way before the Rust foundation was formed or any company sponsoring Rust other than Mozilla.
So perhaps Ocaml (The language that Rust was first written in) doesn't count as well since that has a couple of so called 'unknown vaporware companies' and zero FAAMNG companies on the list of 'sponsors' then? [0]
I also never said it wasn't "real" sponsorship or didn't count. Don't put words in my mouth. I said it isn't a level comparison.
> any company sponsoring Rust other than Mozilla
Mozilla? never heard of them. I only know FAANG apparently /s
Also, Ocaml has supporters such as Bloomberg and Mitsubishi. Not exactly mom-and-pop startups.
Again, I'm not exactly sure what you're arguing. I'm literally a sponsor of Crystal. I put my money where my mouth is.
Also you:
> Also, Ocaml has supporters such as Bloomberg and Mitsubishi. Not exactly mom-and-pop startups.
Perhaps Jane Street is also another mom-and-pop startup then. /s
So they (Bloomberg, and Mitsubishi) somehow matter because they're "Not exactly mom-and-pop startups" even though you also said, "the amount of money they are giving matters". Given the OCaml sponsorship levels [0], Bloomberg (Silver) donates around $1250 - $2084 a month and Mitsubishi (Bronze) around $416 - $1249. That is still less than the monthly amount that either Nikola Motor or 84 Codes is putting in combined.
Nikola Motor isn't not a startup and still sponsors and uses Crystal. It seems that they have alot of money left over every month to throw at Crystal even before it reached 1.0.
> I said it isn't a level comparison.
Then congratulations to Crystal for getting sponsors (and companies sponsoring it) even before 1.0 then. They'll probably get more even after 1.0 then.
Never saw that in Rust before the foundation was formed (which should have been done earlier) since its actually Google again (not Mozilla) feeding the majority of the sponsorship funds to Mozilla.
> Again, I'm not exactly sure what you're arguing. I'm literally a sponsor of Crystal. I put my money where my mouth is.
Good and so do the companies that still use Crystal to this day even before 1.0. At least individuals can sponsor it as I mentioned previously. Unlike Go, whose fate is ultimately in the hands of Google for any decision they make for the language.
I find your take on the project leadership interesting, because it is a spot-on description of everything I found to be wrong with Elm. That BDFL was/is not so benevolent. Or maybe he’s well-meaning but terribly annoying. And the language has a mich better claim to being dead than Crystal, where I had not noticed any loss of momentum or activity on GitHub.
(You may well be right on those points as well, as you are clearly more knowledgeable about the dynamics)
Agree 100%. I still haven't written any Crystal, but I read through parts of the code-base, out of curiosity, and it is some of the most pleasant compiler code I've ever read.
Funny you mention this because all the clutter and symbols (operators) in Ruby's syntax is why I never liked it.
It's still pre-1.0 but you might find it interesting, but maybe not for what you're working with right now. https://ziglang.org
[0] https://manas.tech/blog/2020/02/11/nikola-motor-company/
ae = 'a'..'e'
ccae = ['<', *r, '>'] # => ['<', 'a', 'b', 'c', 'd', 'e', '>']
I'm guessing *r in the second line should have been *ae (or ae in the first line should have been named r)
Also
"It is new possible" should prpbably have been "It is now possible"
EDIT: PR submitted. Feel free to create one next time :)
https://github.com/crystal-lang/crystal-website/pull/172/fil...
nine = while 1 do break 9 end
puts nine #=> “nine”
I love Ruby. I am not sure how I feel about this. It looks useful for the odd thing or two, maybe. nine = while 1; break 9; end
puts nine
And yes, it prints "9". irb(main):001:0> nine = while 1 do break 9 end
=> 9
irb(main):002:0> nine.class
=> Integer
irb(main):003:0> puts nine
9
=> nil
puts might be calling .to_s on the variable, which is why you see a string, but the loop does return an integer. 9.to_s
=> "9"I over-edited my comment to fit a narrow screen, breaking the example.
Again, that all could have been a side effect of horribly written Rails code where I was working. The Ruby guru I mention above would often lament about how bad our Ruby codebase was. To me, it just showed "wow it's really possible to write code this bad in this language" and left a sour taste in my mouth.
I usually prefer TypeScript for scripting things. Being able to run it via node and in the browser is a killer feature and I have many small libraries I've written that I reuse in a lot of personal projects. Sometime I use Python but I'm not a huge fan of Python's version and dependency management.
For anything that's not destined for the browser that require speed or a larger codebase (i.e. backend services) I don't think you can go wrong with the JVM. I prefer just plain Java, but also have used Scala and Kotlin extensively and they're all great for speed, collaboration, readability, testability, and have robust ecosystems.