Love for Expression Languages

2023-12-26

I love expression languages. In Rust I can do this:

let last_integer = {
    if last_number_which_list == 0 {
        word_to_integer(&last_number_in_words)
    } else {
        last_number_in_words.parse::<i32>()
    }
};

While in Go, to achieve the same result I would have to do something like this:

last_integer := 0

if last_number_which_list == 0 {
  last_integer = word_to_integer(last_number_in_words)
} else {
  last_integer = strconv.Atoi(last_number_in_words)
}

Or what feels clunkier but comes closer to an expression:

last_integer = func() int {
  if last_number_which_list == 0 {
    return word_to_integer(last_number_in_words)
  } else {
    return strconv.Atoi(last_number_in_words)
  }
}()

Note: Error handling has been elided in all code samples for the sake of brevity. The idiomatic naming conventions and casing styles have also been ignored in order to provide as similar code samples as possible.

I wonder what the costs are associated with having expression blocks in your programming language.


More posts like this

Rust Macros (while Crafting Interpreters)

2023-12-31 | #interpreters #macros #programming-languages #rust

I have never used Rust extensively. The few times I have tried, have ended up in me giving up due to unwinnable fights against the borrow-checker. By the recommendation of a friend I am giving this another shot. I’m making my way through Robert Nystrom’s Crafting Interpreters as well as doing 2023’s Advent of Code. I am late to the latter but I am enjoying myself regardless. I am using Rust as my language of choice for both endeavours.

Continue reading 