You should try Gleam

本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。

You should try Gleam

Introduction If you have read my recent rant about programming languages, you’ll know I’ve been quite interested in Gleam. Gleam is a functional language that targets BEAM, the Erlang VM, with a simple syntax and interoperability with the wider BEAM ecosystem (Erlang and Elixir). Interestingly, Gleam can also target JavaScript i.e., Gleam can be compiled into JavaScript and run through Node.js. I’ve been mostly using Rust for hobby projects, which at the moment is the only language that really makes coding enjoyable for me, and Gleam has managed to scratch that itch too. If you’re a Rust connaisseur, then Gleam will feel familiar to you, bar a few key differences. I feel like 80% of the code I write could be copy pasted to Rust (with a tinge of search-and-replace for syntax) and it’d compile. This post won’t give an exhaustive overview of the language, if you want to learn more, I recommend the excellent interactive tour. You’ll get a good feel for Gleam in under half an hour.

引言:如果你读过我最近关于编程语言的吐槽,你就会知道我对 Gleam 很感兴趣。Gleam 是一门函数式语言,目标平台是 Erlang 虚拟机 BEAM,它拥有简洁的语法,并能与更广泛的 BEAM 生态系统(Erlang 和 Elixir)互操作。有趣的是,Gleam 还可以针对 JavaScript 进行编译,即 Gleam 代码可以被编译成 JavaScript 并在 Node.js 中运行。我主要在业余项目中使用 Rust,目前它是唯一能让我真正享受编码乐趣的语言,而 Gleam 也成功地满足了我的这种需求。如果你是 Rust 的行家,那么除了几个关键区别外,你会觉得 Gleam 很亲切。我觉得我写的 80% 的代码都可以直接复制粘贴到 Rust 中(只需稍作语法上的替换),并且能够编译通过。这篇文章不会对该语言进行详尽的概述,如果你想了解更多,我推荐那个出色的交互式教程。你可以在半小时内对 Gleam 有一个很好的了解。

Gleam: the good On its homepage, Gleam is described as “a friendly language for building type-safe systems that scale!” (emphasis theirs) which does a good job of conveying what Gleam is all about. Gleam is friendly The syntax is extremely simple, most of the complex stuff is hidden by the Erlang runtime which you don’t really have to interact with unless you want to! Of course, Gleam is a functional language, so if you have lived in a C-shaped imperative world your whole life, it might be a bit daunting at first. But if you compare it to other functional languages (OCaml, Haskell, Erlang), you’ll quickly notice Gleam is much simpler. After using it a bit, it feels like all the functional parts of Rust, with a few missing ones, and none of the imperative parts.

Gleam:优点。在其主页上,Gleam 被描述为“一种用于构建可扩展、类型安全系统的友好语言!”(强调原文),这很好地传达了 Gleam 的核心理念。Gleam 很友好,语法极其简单,大部分复杂的东西都被 Erlang 运行时隐藏了,除非你想深入了解,否则你根本不需要与之交互!当然,Gleam 是一门函数式语言,所以如果你一生都生活在 C 风格的命令式世界中,起初可能会觉得有点令人生畏。但如果你把它与其他函数式语言(OCaml、Haskell、Erlang)进行比较,你会很快发现 Gleam 要简单得多。使用一段时间后,它感觉就像是 Rust 中所有函数式部分的集合,虽然缺少了一些功能,但完全没有命令式部分。

The use pattern If you’ve written a lot of JavaScript, you might be used to the indentation creep that arises when nesting many callbacks into one another. In Gleam, many functions expect a callback, for example because as a way to control access to a resource and clean up when the process is done (closing a database connection, releasing a file handle, etc.). To avoid this kind of tower of callbacks, Gleam provides some syntactic sugar: use param <- some_func(arg) is equivalent to some_func(arg, fn(param) { ... }). The callback must always be the last argument of the function. That is the opposite of |>, which fills the first one.

Use 模式:如果你写过很多 JavaScript,你可能已经习惯了在嵌套多个回调时出现的缩进地狱。在 Gleam 中,许多函数需要回调,例如作为控制资源访问并在进程完成后进行清理(关闭数据库连接、释放文件句柄等)的一种方式。为了避免这种回调塔,Gleam 提供了一些语法糖:use param <- some_func(arg) 等同于 some_func(arg, fn(param) { ... })。回调必须始终是函数的最后一个参数。这与 |> 正好相反,后者填充的是第一个参数。

Tail-call optimization This one might genuinely trip you up if you’re not familiar with it. The first thing you need to know is there are no loops in Gleam! Again, if you’ve written OCaml or Haskell before, you won’t be surprised by this. In 98% of cases, loops are simple operations over bounded iterations. Think for loops on a list or a set. Exactly the kind of thing which in Rust you always have two ways of doing: one imperative with the exact for loop, one more functional with a combination of operations on iterators. Well, if you look at the code example in the previous section, you’ll see Gleam uses the second kind and has a plethora of functions to iterate over lists, sets, tuples, strings, anything. But what if you need something more complicated? What about the 2%? This is where you would instead use recursion!

尾调用优化:如果你不熟悉这个概念,它可能会让你真正感到困惑。你需要知道的第一件事是 Gleam 中没有循环!同样,如果你之前写过 OCaml 或 Haskell,你对此不会感到惊讶。在 98% 的情况下,循环是对有界迭代的简单操作。想想列表或集合上的 for 循环。这正是你在 Rust 中总是有两种实现方式的那种事情:一种是使用 for 循环的命令式方式,另一种是结合迭代器操作的函数式方式。好吧,如果你看看上一节的代码示例,你会发现 Gleam 使用的是第二种方式,并且拥有大量用于遍历列表、集合、元组、字符串等的函数。但如果你需要更复杂的东西呢?那 2% 的情况怎么办?这就是你需要使用递归的地方!