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:


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

查看所有标签

猜你喜欢:

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

司法的过程

司法的过程

(美)亨利·J.亚伯拉罕 / 泮伟江 宦盛奎 韩阳 / 北京大学出版社 / 2009-07-28 / 58.00元

本书是以比较研究的方法来分析司法哲学的经典文本之一。作者以敏锐的眼光透视了司法过程背后的理论、实践和参与其中的人。比较了美国、英国、法国的具体法院运作,审视了“司法能动主义”和“司法克制主义”之间的争辩。本书第七版的介绍吸收了美国、英国、法国和欧洲法院体系运作中的最新和重要的发展。 目前国内非常关注司法的运作过程、法官的裁判过程,此书的翻译对于这方面的研究很有助益,对于英国和法国法院的介绍填补了国......一起来看看 《司法的过程》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码