5 ms·
Notes on the Crystal Language
- bhaney 2y ago> Can you magically return/break out of iterator functions like you can in Ruby? Sure can. $ cat break.cr (1..).each do |x| break if x > 2 puts x end $ crystal break.cr 1 2 $ The answer to Crystal questions that end in "like you can in Ruby" is usually "yes." The language really is just Ruby with static typing, warts and all. Most random snippets of Ruby will run just fine when compiled as Crystal.
- dangitman 2y ago[dead]
- woodruffw 2y agoI agree this is weird, but I think it's largely an analogy issue: Ruby and Crystal both explicitly document blocks as not being closures, even when they can be converted into closures. They're their own weird thing. Crystal also documents that it will fail if the block contains return or break statements[1]. [1]: https://crystal-lang.org/reference/1.13/syntax_and_semantics/capturing_blocks.html#break-and-next https://crystal-lang.org/reference/1.13/syntax_and_semantics...
- deleted 2y ago[deleted]
- straight-shoota 2y agoI'm a core developer of Crystal. Note that there are two different flavours of blocks in Crystal: inlined and captured ones. `return` and `break` are supported in inlined blocks.
- Trasmatta 2y agoCrystal is beautiful. I wish I had a use case for it.
- pmontra 2y agoSeconded. My use case might be running my own Ruby scripts faster, but they don't need to run any faster so it's not worth the trouble of setting up a development environment, especially one that requires a compilation step. It would be a totally different thing if it could run Rails, which is one of the things I do in my job. The speedup would probably justify using it, especially when running tests on my laptop. My customers are perfectly happy with the performance of Rails on their servers. Same with the ones using Python with Django.
- compumike 2y ago> It would be a totally different thing if it could run Rails, which is one of the things I do in my job. At https://heiioncall.com/ https://heiioncall.com/ we combine the two and use Rails for the human-facing parts, and Crystal for the machine-facing parts (inbound API server calls, and outbound HTTP probes). For us, the number of machine-facing requests per second is so many orders of magnitude higher than human-facing requests that Crystal's performance and lower footprint is valuable there. While on the other hand, the conveniences of Ruby on Rails are still great for a conventional human-facing web and mobile application.
- Trasmatta 2y agoYeah,I've found that for the vast majority of Rails apps, the bottleneck is the database, not Ruby. Even a 100x speed up in the application code would have very little end user impact on performance in most cases.
- squarefoot 2y agoI would see plenty of use cases as a Micropython substitute if at least a subset of it was ported to microcontrollers.
- nobodywasishere 2y agoAs far as a REPL for Crystal goes, the interpreter is available via "crystal i"
- nobodywasishere 2y agoAlso for finding shards/libraries/documentation, I'd recommend checking out https://shards.info/ https://shards.info/, https://shardbox.org/ https://shardbox.org/, and https://crystaldoc.info/ https://crystaldoc.info/
- bingo-bongo 2y agoThe awesome crystal repo is also a good: https://github.com/veelenga/awesome-crystal https://github.com/veelenga/awesome-crystal
- sedatk 2y ago> Oops, looks like its tempfile lib is written in library code in the style of mktemp(3), instead of calling the OS’s mkstemp(3) or equivalent. So it just finds a filename that doesn’t exist and then opens it separately, letting an attacker create a new file which they can read at that location between those two steps. Uh, have fun with your temporary files leading to exploitable race conditions guys. Good catch. The API should be opening the tempfile atomically (the best option), or at least should use a CSPRNG for its random suffix. It doesn’t, currently.
- bhaney 2y agoI don't think that complaint is even true. A quick glance at the Crystal stdlib source[1] shows that the mktmp implementation uses the O_EXCL mode, which will cause the open call to error out if the file already exists, and the implementation here will handle that and try again with a different filename. I think the worst thing an attacker could do here if they could somehow predict the generated names before the file is created is DoS the mktmp call by racing ahead of it 100 times until it gives up. [1] https://github.com/crystal-lang/crystal/blob/release/1.13/src/crystal/system/file.cr#L53 https://github.com/crystal-lang/crystal/blob/release/1.13/sr...
- sedatk 2y agoAlso, good catch! :)
- hydrolox 2y agonothing against the language but it's a bit suspicious that the top sponsors include "buy Google reviews" and "buy Instagram followers" (if you navigate to the actual Crystal language site)
- graypegg 2y agoOh... and only for 750$ a month. The other sponsors pay more, but have to appear alongside those.
- bhaney 2y agoThat's an interesting form of guerrilla advertising. Find a project with decent website traffic that displays sponsors, and commit just enough to be displayed in the list of top contributors (which includes some delicious SEO-friendly backlinking juice too). $750/mo and it's already getting the companies mentioned in the currently-top comment of a thread on the front page of HN!
- mdasen 2y agoI'm not sure there's a lot of SEO-friendly back linking juice. They're marked as rel="nofollow sponsored"
- straight-shoota 2y agoI'm a core developer of Crystal. We've been wondering about why it's worth the money for these sponsors. The links ideally shouldn't have much effect because they're marked as `sponsored`. Anybody got any ideas? Either way, it's money that helps the project so we're fine with accepting it.
- Lerc 2y agoThat's a bit of an indictment on how poorly projects are supported. If those sponsors feel like the eyeballs they get from supporting this project is worth it, probably at least some of the owners of those eyeballs are getting enough value from the project to overbid. Is it an artifact of the Individual/Organisation split of OpenCollective? I see the top 10 individuals that have put in $1000 or more.
- flats 2y agoI work with Ruby every day at work & have always been curious about Crystal. This was a very helpful dive into some overarching concepts & small details. Thanks for sharing!
- joelignaatius 2y ago[flagged]
- Lerc 2y agoWhat's the performance like? The comment at the end OO language with a static type system that feels as low-friction as a dynamic one Sounds nice, but does the performance act like a dynamic language or is it super speedy? I'd be happy with half the speed of C. I'm still on the lookout for a language that feels as low-friction as a dynamic language but could do some form of iterable.map in parallel on all cores transparently. There's a blog post on paralellism in Crystal https://crystal-lang.org/2019/09/06/parallelism-in-crystal/ https://crystal-lang.org/2019/09/06/parallelism-in-crystal/ but that's a fair few years ago now.
- transfire 2y agoSuper speedy. It is in fact about half the speed of C. It has a GC so that’s probably the main slow down, but hey, no memory errors or raking your brain over a barrow checker.
- anotherhue 2y agoThe GC in particular is excessively slow. It's not a production implementation. I don't need JVM level GC engineering everywhere but it really is painful. https://en.m.wikipedia.org/wiki/Boehm_garbage_collector https://en.m.wikipedia.org/wiki/Boehm_garbage_collector
- viraptor 2y agoWhat use cases do you see that in? And do you mean throughput or latency is slow?
- anotherhue 2y agoWeb server with DB backend. Pause times can exceed 2000ms. No doubt some code changes could help but the fact remains the GC is not an optimised one.
- straight-shoota 2y ago
- steve_adams_86 2y agoI got to work on a crystal project years ago and loved it. I was exposed to a bunch of people who were really into crystal and it made me come to the completely incorrect conclusion that crystal was up and coming and I’d be able to find more work using it. I was wrong. I’ve never encountered it again. It was a great language, super productive. I helped some people develop a pet insurance service. So weird. I’d work with crystal again for sure.
- jaynetics 2y agoDid you have to work around some rough edges of the language in that job? To me it feels like it never quite reached maturity, although in a better world it would have gotten more support and conquered all territory that is now ruled by the Gophers. I guess to make a successful programming language, you either need corporate backing or extend an existing one nowadays, otherwise it's really hard to build a stdlib of modern proportions.
- steve_adams_86 2y agoAs I recall the rough edges were a lack of tutorials and docs at the time. There are some “weird” things about the language which are totally fine if you know what they are, but it’s brutal if you don’t have a quick reference to make sense of things. It has been a while so I can’t recall very clearly, and I have a feeling the community and docs around it are all much better now.
- zem 2y ago> it's really hard to build a stdlib of modern proportions. it would surprise me if the stdlib was the issue. you can clone the ruby, python, or go standard libraries, and that would get you into a good state pretty fast; those languages have already done the work to assemble a standard library that people seem satisfied with by and large, and translating code into a new language is far easier than coming up with what should go into a library and then designing and implementing it from scratch. still takes time and effort but there's a clear roadmap if you go that route. I suspect the real weakness is the thousands of third party packages for stuff that is too specialised or niche to be in the stdlib, but when you need it you really need it. (e.g. I have recently felt the need for an implementation of the blossom algorithm for graph matching in elixir; in python or ruby it's already done for me and available as a package)
- atemerev 2y ago“Oh it’s nice but it’s not Rust, why everything can’t be just like Rust” The audacity of Rustaceans is so thick.
- adnan45 2y ago[flagged]
- giovannibonetti 2y ago> I still think the world really needs a solid, immutable-first and functional-first scripting/glue language, and Crystal isn’t that. The author might be interested in Roc [1] by Richard Feldman et al, which seems to check all the boxes, including scripting with type inference and glue with good FFI to other languages. It is still in early stage, though, since it is a very ambitious project. [1] https://www.roc-lang.org/ https://www.roc-lang.org/
- bandauo 2y agoIt seems that Clojure ticks all this boxes
- lolinder 2y agoThe JVM is a pretty heavy dependency for scripting/glue, if just for startup times.
- vfclists 2y agoBabashka it when its done if it depends on libraries not in Babashka?
- deleted 2y ago[deleted]
- tomtheelder 2y agoJVM is a hard dealbreaker for a scripting/glue language.
- deleted 2y ago[deleted]
- tincholio 2y agoSee Babashka.
- rscho 2y ago
- ljm 2y agoThe bit about iterating over a directory struck me as a bit of a misunderstanding and therefore the rant following it seemed a bit misplaced. The code example makes no sense at all, so you can’t really make sense of the argument being made. Without that, it just looks like the author has an insatiable love affair with rust and their arguments lose credibility. Of course if they’re trying to smash Rust semantics into any other language they’re going to be disappointed.
- compumike 2y agoWe use Crystal extensively at https://heiioncall.com/ https://heiioncall.com/ for the high-performance stuff, and it’s terrific. :) As the author notes: the typing system is very pleasant. And the standard library is great! Generics work fine. The testing story is very good (almost as good as rspec). Have some experience with the Threads but won’t comment extensively as that’s about to get some big changes in the next minor release or two. Overall: highly productive for high-performance backend API servers and background task daemons. Way more fast and pleasant to write than Rust or Go (IMHO).
- nurettin 2y ago> As the author notes: the typing system is very pleasant. And the standard library is great! The author complained about the standard library, specifically the Dir module using inconsistent types and the implementation details of Temp files potentially leading to security flaws, leading me to think there are more warts he didn't cover.
- compumike 2y agoTemp file issue is debunked here: https://news.ycombinator.com/item?id=41684848 https://news.ycombinator.com/item?id=41684848 Dir module works fine, though maybe the docs could be improved. https://crystal-lang.org/api/1.13.3/Dir.html https://crystal-lang.org/api/1.13.3/Dir.html . The stdlib code is also highly readable: https://github.com/crystal-lang/crystal/blob/d14d04562/src/dir.cr#L111 https://github.com/crystal-lang/crystal/blob/d14d04562/src/d... shows that #each_child just calls #read and yields it to the block, so I don't think this is really a wart :)
- nurettin 2y agoI think the Dir issue wasn't that documentation was inadequate or stdlib code was hard to read, but he expected paths, and received strings instead.
- davidatbu 2y agoHow bad are the compile times? My understanding is that the compile times are currently bad, and they can't be improved without fundamental changes to the lang/type system.
- hi-v-rocknroll 2y agoI've tried Crystal half a dozen times but always ran into some inconsistency or bug that became a show-stopper. Sigh. I hope maybe the next time it will be reliable and usable.
- l0new0lf-G 2y agoI spent some time experimenting with the Crystal language, and it seemed very promising. But the absence of capabilities for parallel programming (as of 2022) made me abandon the attempts to learn better Crystal, as it felt pointless.