v0.8 of Gleam, the statically typed language for the Erlang VM, is out

栏目: IT技术 · 发布时间: 4年前

内容简介:It’s been a really good couple months for Gleam. The community has been growing, we’ve been getting new features and improvements, and now it’s time for another release. Let’s take a look!Documentation has been a bit of sore point up until now. If you want

It’s been a really good couple months for Gleam. The community has been growing, we’ve been getting new features and improvements, and now it’s time for another release. Let’s take a look!

HTML Documentation

Documentation has been a bit of sore point up until now. If you wanted to write a program in Gleam there was no other way to see what types functions were available to use. More than likely you would have to have the source code for the standard library open in a window somewhere, which isn’t the nicest way to learn a language.

To solve this problem Gleam can now generate HTML documentation from your Gleam source code and publish to HexDocs, the home of documentation in the Erlang ecosystem.

Check out an example of the new documentation here .

Gleam format

Consistency matters in code! Having a consistent style makes code easier to read and easier to work with. On the other hand, maintaining a consistent style by hand is an expensive time sink, and it’s all too easy to waste time debating exactly where is the right place to put those curly braces in code reviews.

The solution to these problems is a code formatter. Write whatever code you want without spending any time on style, press save, and have your editor automatically fix the style before you’ve taken your fingers off the keyboard.

Starting with this release Gleam includes a formatter, and all new Gleam projects are preconfigured to have their format validated on CI. Enable the formatter in your editor and never waste time on fixing style again.

If you’re interested in learning more about formatters or how they work, a few years back I gave a talk on the subject. It is in the context of the Elixir language, but it all holds true to Gleam as well.

Fancy pipes

The pipe operator is a favourite feature of Gleam, it allows us to write chains of functions that read top-to-bottom and left-to-right.

It works by taking the value on the left hand side and using it as the argument to the function on the right hand side.

// Without pipe
print(eval(read(stdin)), stdout)

// With pipe
stdin |> read |> eval |> print(_, stdout)

In the example above the function “print” takes two arguments, so the function _ capture syntax has been used to tell Gleam where to insert the left hand side value. In practice most Gleam functions take multiple arguments, so the capture syntax is needed more often than not.

scores
|> list.map(_, double)
|> list.fold(_, 0, add)
|> should.equal(_, 42)

With Gleam v0.8’s fancy pipe this boilerplate is no longer needed!

When used with a function call on the right hand side the pipe will first check to see if the left hand value could be used as the first argument to the call, and if not it falls back on using it as the argument to the call.

scores
|> list.map(double)
|> list.fold(0, add)
|> should.equal(42)

Much better! And what’s more this is resolved at compile time using the type system so there is no performance or safety penalty to use this over nested function calls.

Accessor syntax

Pattern matching is great, but sometimes we just want to get a single value out of a record or tuple. To enable this Gleam now supports the .field syntax that’s common in other languages.

It works with records:

let student = Student(subject: "Theatre")
let mary = Person(name: "Mary", role: student)

mary.name         // => "Mary"
mary.role.subject // => "Theatre"

And it works with tuples:

let pair = tuple("Apples", "Oranges")

pair.0 // => "Apples"
pair.1 // => "Oranges"

Spread operator

Gleam’s pattern matching has been expanded with a new operator called the spread operator, which allows you to write a partial pattern for a record.

// Without spread
let Uri(
  scheme: scheme,
  port: port,
  path: _,
  query: _,
  fragment: _,
  authority: _,
  userinfo: _,
  host: _,
) = uri

// With spread
let Uri(scheme: scheme, port: port, ..) = uri

To be consistent with records the syntax for prepending to a list and for matching on the head of a list have been updated to also use the spread operator.

let new_list = [1, 2, 3, ..existing_list]

case list {
  [first, ..] -> first
  [] -> 0
}

The old [x | xs] list syntax is deprecated but is still accepted. If you run the formatter it will automatically upgrade your code for you, so do that first!

The rest

In addition to these changes there’s also support for numbers and numerical operators in case guards, an “assert” syntax for inexhaustive pattern matching, a “todo” syntax for type checking functions without a finished implementation, a handful of performance improvements, and a whole bunch of bug fixes!

Outside of the compiler we continue to grow the standard library, and we have made good progress in OTP support with a prototype for type safe supervision trees.

For more information check out the changelog on Github.

Thanks

The most exciting thing for me about this release is just how much the community has grown since the last release. We’ve more people writing Gleam, more people sharing ideas, and more people working on the compiler. We’ve even got the beginnings of a Gleam web server and web framework in the Midas project!

It would not have been possible to have a release this big without the support of all Gleam’s sponsors and contributors, so a huge thank you to them:

If you would like to help make strongly typed programming on the Erlang virtual machine a production-ready reality please consider sponsoring Gleam via the GitHub Sponsors program.

Thanks for reading! Have fun! :purple_heart:


以上所述就是小编给大家介绍的《v0.8 of Gleam, the statically typed language for the Erlang VM, is out》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

算法数论

算法数论

裴定一、祝跃飞 / 科学出版社 / 2002年09月 / 19.00

本书论述了算法数论的基本内容,其中包括:连分数、代数数域、椭圆曲线、素性检验、大整数因子分解算法、椭圆曲线上的离散对数、超椭圆曲线。本书的特点是内容涉及面广,在有限的篇幅内,包含了必要的预备知识和数学证明,尽可能形成一个完整的体系。并且本书的部分内容曾多次在中国科学院研究生院信息安全国家重点实验室和广州大学作为硕士研究生教材使用。 本书可作为信息安全、数论等专业的研究生教材及相关专业的研究人......一起来看看 《算法数论》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

随机密码生成器
随机密码生成器

多种字符组合密码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码