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

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

内容简介: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:


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

查看所有标签

猜你喜欢:

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

软件测试的艺术

软件测试的艺术

梅尔斯 / 机械工业出版社 / 2006年01月 / 22.0

《软件测试的艺术》(原书第2版)成功、有效地进行软件测试的实用策略和技术:    基本的测试原理和策略      验收测试    程序检查和走查         安装测试    代码检查            模块(单元)测试    错误列表            测试规划与控制    同行评分            独立测试机构    黑盒、白盒测试    ......一起来看看 《软件测试的艺术》 这本书的介绍吧!

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

RGB HEX 互转工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具