top | item 32087110

(no title)

rrevi | 3 years ago

Thanks for sharing compumike! Very cool!!

If you don’t mind me asking, why did you choose the Crystal programming language for this project?

discuss

order

compumike|3 years ago

Crystal https://crystal-lang.org/ is beautiful, highly performant, concise, powerful, well documented. It has a solid standard library and plenty of "shards" (packages) too. It's really the elegance of writing it that I appreciate the most -- perhaps even more than I get that elegant feeling from Ruby. The code for the standard library is highly readable and well documented, which is awesome.

In working on this project, I discovered two "superpowers" of Crystal:

The first was macros https://crystal-lang.org/reference/1.5/syntax_and_semantics/... which I used sparingly, but to great effect, when an ordinary method call wouldn't do.

The second was monkeypatching: specifically, it's super easy to reopen existing standard library classes and add a field I need, for example:

    # BEGIN MONKEYPATCH
    class HTTP::Server::Context
      property response_body_cache_key : String? = nil
    end
    # END MONKEYPATCH
This adds a new field the standard library HTTP::Server::Context https://github.com/crystal-lang/crystal/blob/1.5.0/src/http/... / https://crystal-lang.org/api/1.5.0/HTTP/Server/Context.html so I can specify a cache key near the beginning of a request handler, and later my response middleware picks it up to store the response body.