RFC: variable declaration syntax using `:=`

Long soliloquy incoming, I apologise.

they can be quite burdensome for newcomers

As someone who started using crystal relatively extensively as a hobby coder last year, with very little prior coding experience, I think I’m uniquely qualified to chime in on this :DD

As you pointed out, having dozens of different notations, keywords etc is really challenging for people starting out, particularly with little coding experience, because it increases the complexity significantly.

With that being said, the Crystal team has done a phenomenal job with the language. At no point did I feel like syntax sugar was introduced just for the sake of “being cool”. There was always a clear intent and reasonable thought process behind it. The language has been a joy to learn and this intuitive syntax sugar is what makes it so fun for me.

In that same vein, := follows a very logical derivation of the underlying syntax.

# Define a variable with inferred, open Type
x = 5 # x is Int32, but can change.

# Define a variable with fixed, explicit Type
x : Int32 = 5 # x is Int32 and cannot change.

# Based on this, any of the following are logical,
# intuitive steps for the inferred, yet fix Type
x : _ = 5
x : = 5
x := 5

None of the above are are pulled out of thin air, and anyone starting out with the language can see how it’s derived with relative ease.

TL;DR: I think Crystal is the best language precisely because it has a lot of syntax sugar, but they’re all very intuitive, easy to learn and convenient to use.

7 Likes