Gleam: Generated Erlang doesn't wrap blocks in begin/end with let expressions

Created on 29 Aug 2020  路  3Comments  路  Source: gleam-lang/gleam

The Erlang generated by this Gleam code is not correct, is assigned the wrong value to the variable.

let x = {
  1
  2
}
x // is 1, should be 2
X = 1,
2,
X
bug

All 3 comments

Oh dear, that's quite a bug. Thank you!

I just spent some time looking at a similar issue, not sure if it's the same one but seems related:

// gleam
pub fn fetch_bar(json: json.Json) -> Result(String, String) {
  let maybe_foo = {
      let data = dynamic.from(json)
      try foo = dynamic.field(data, "foo")
      dynamic.string(foo)
    }
  case maybe_foo {
    Ok(foo) -> Ok(foo)
    Error(_) -> Error("oh well")
  }
}
% erlang
fetch_bar(Json) ->
    MaybeFoo = Data = gleam@dynamic:from(Json),
    case gleam@dynamic:field(Data, <<"foo"/utf8>>) of
        {error, GleamTryError} -> {error, GleamTryError};
        {ok, Foo} ->
            gleam@dynamic:string(Foo)
    end,
    case MaybeFoo of
        {ok, Foo@1} ->
            {ok, Foo@1};

        {error, _} ->
            {error, <<"oh well"/utf8>>}
    end.

edit:

This version works just fine:

// gleam
pub fn fetch_bar(json: json.Json) -> Result(String, String) {
  let maybe_foo = {
      try foo = dynamic.field(dynamic.from(json), "foo")
      dynamic.string(foo)
    }
  case maybe_foo {
    Ok(foo) -> Ok(foo)
    Error(_) -> Error("oh well")
  }
}
% erlang
fetch_bar(Json) ->
    MaybeFoo = case gleam@dynamic:field(
        gleam@dynamic:from(Json),
        <<"foo"/utf8>>
    ) of
        {error, GleamTryError} -> {error, GleamTryError};
        {ok, Foo} ->
            gleam@dynamic:string(Foo)
    end,
    case MaybeFoo of
        {ok, Foo@1} ->
            {ok, Foo@1};

        {error, _} ->
            {error, <<"oh well"/utf8>>}
    end.

I think that's the same problem. I'll try and get this fixed as soon as possible.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

molnarmark picture molnarmark  路  6Comments

CrowdHailer picture CrowdHailer  路  6Comments

michaeljones picture michaeljones  路  6Comments

lpil picture lpil  路  6Comments

lpil picture lpil  路  6Comments