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

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

内容简介:1 month and 10,910 lines of code later it is time for another Gleam release! Let’s see what’s new this time.Sometimes we want to use a certain fixed value in multiple places in our project. Until now we’ve had two options. The first option was to copy and

1 month and 10,910 lines of code later it is time for another Gleam release! Let’s see what’s new this time.

Module constants

Sometimes we want to use a certain fixed value in multiple places in our project. Until now we’ve had two options. The first option was to copy and paste the value into multiple places in our code.

pub fn is_before(year: Int) -> Bool {
  year < 2101
}

pub fn is_during(year: Int) -> Bool {
  2101 <= year && year <= 2111
}

Duplication of values like this is error prone, especially if we want to update the values later, as we’ll need to find every place it’s used in order to change it.

Another option is to wrap the value in a function.

pub fn start_year() -> Bool {
  2101
}

pub fn end_year() -> Bool {
  2111
}

pub fn is_before(year: Int) -> Bool {
  year < start_year()
}

pub fn is_during(year: Int) -> Bool {
  start_year() <= year && year <= end_year()
}

This is better than copying the value in our code, but isn’t yet ideal. A function can perform any amount of computation (with side effects!) when it is called, so we have to read the definition of the function to be sure what it does.

To make matters worse Gleam’s case clause guards don’t support calling of functions within them, so this code will be rejected by the compiler:

pub describe(year: Int) -> String {
  case year {
    year if year < start_year() -> "Before"
    year if year > end_year() -> "After"
    _ -> "During"
  }
}

To solve all these problems Gleam now has module constants. They are always inlined by the compiler, and can be used in case clause guards.

Using them the above code can be rewritten like so:

pub const start_year: Bool = 2101
pub const end_year: Bool = 2111

pub fn is_before(year: Int) -> Bool {
  year < start_year
}

pub fn is_during(year: Int) -> Bool {
  start_year <= year && year <= end_year
}

pub describe(year: Int) -> String {
  case year {
    year if year < start_year -> "Before"
    year if year > end_year -> "After"
    _ -> "During"
  }
}

Much better! Module constants are going to provide the basis of some exciting new features in future, so watch this space. :rocket:

Thanks to Ahmad Sattar for taking the lead in the implementation of this feature.

Bit string syntax

One of great things about Erlang is how easy it is to work with raw bits and bytes using the bit string literal syntax. Starting with this release Gleam supports this too!

Explaining all the things one can do with the bit string syntax would take longer than I have now, but in short they give a declarative way of constructing and parsing raw data of any format. What’s more the Erlang VM is highly optimised for this, so bit syntax is highly efficient too!

For example, if I wanted to create a 4 byte unsigned little endian integer with the value of 100 I could do so using bit syntax like this:

let my_integer = <<100:unsigned-little-int-size(4)>>

Or if I wanted to extract the title, artist, album, year, and comment from an MP3 music file’s ID3 tags I could pattern match on it like this:

pub fn get_metadata(id3: BitString) -> Result(Metadata, String) {
  case id3 {
    <<
      "TAG":utf8,
      title:utf8-size(30),
      artist:utf8-size(30),
      album:utf8-size(30),
      year:utf8-size(4),
      comment:utf8-size(30),
      _rest:binary,
    >> -> Ok(Metadata(title, artist, album, year, comment))

    _ -> Error("Not a valid ID3 tag")
  }
}

Thanks to Benjamin Tan’s blog post for this example.

For another example of bit syntax in action check out this very serious base64 encoding alternative which converts arbitrary data into emojis!

As part of this new feature we have introduced BitString and UtfCodepoint types into the prelude, and split the standard library Iodata type into StringBuilder and BitBuilder types, which are for efficiently constructing strings and bit strings respectively.

Thanks to Tom Whatmore for taking the lead in the implementation of this feature.

The rest

For information on the rest of additions, improvements, and bug fixes in this release please check out the Gleam changelog and the standard library changelog .

Supporting Gleam

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.

This release would not have been possible without the support of all the people who have sponsored and contributed to it, so a huge thank you to them.

Thanks for reading! Have fun! :purple_heart:


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

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

用户体验面面观

用户体验面面观

[美] 库涅夫斯基(Mike Kuniavsky) / 汤海 / 清华大学出版社 / 2010-5 / 69.00

这是一本专注于用户研究和用户体验的经典读物,同时也是一本容易上手的实战手册,从实践者的角度,着重讨论和阐述了用户研究的重要性、主要的用户研究方法和工具,同时借助于实例介绍了相关的应用。全书共3部分18章,深度剖析了何为优秀的用户设计,用户体验包括哪些研究方法和工具,如何 得出和分析用户体验调查结果等。一起来看看 《用户体验面面观》 这本书的介绍吧!

SHA 加密
SHA 加密

SHA 加密工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器