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》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

技术元素

技术元素

[美] 凯文·凯利 / 张行舟、余倩、周峰、管策、金鑫、曾丹阳、李远、袁璐 / 译言·东西文库/电子工业出版社 / 2012-5 / 55.00元

我会将我不成熟的想法、笔记、内心争论、草稿以及对其他文章的回应都写在《技术元素》中,这样我就能知道自己到底在想些什么。——KK “技术元素”(technium)是凯文•凯利专门创造出来的词语。“技术元素不仅仅包括一些具象的技术(例如汽车、雷达和计算机等),它还包括文化、 法律、社会机构和所有的智能创造物。”简而言之,技术元素就是从人的意识中涌现出来的一切。KK把这种科技的延伸面看成一个能产生......一起来看看 《技术元素》 这本书的介绍吧!

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

Base64 编码/解码

SHA 加密
SHA 加密

SHA 加密工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具