gleam: Constant's type is determined only once which causes strange errors

This code:

import gleam/io
import gleam/result

pub fn with_smth(f: fn() -> a) -> a {
  f()
}

pub const with_smth2 = with_smth

pub fn main() {
  use <- with_smth2()
  Ok(Nil)
}

pub fn another() {
  use <- with_smth2()
  Nil
}

produces this error:

error: Type mismatch
   ┌─ /tmp/const_forward/src/const_forward.gleam:17:3
   │
17 │   Nil
   │   ^^^

Expected type:

    fn() -> Result(Nil, a)

Found type:

    fn() -> Nil

The problem is that when this constant is public and used in multiple files, these files fail in random order depending on slight code changes, and the error doesn’t give enough context.

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 18 (18 by maintainers)

Commits related to this issue

Most upvoted comments

I knew there was one I was missing 😂 I picked up a bunch of issues and then got crazy busy, yes I actually have time today to 😁

I’d be down to take a look at this 😃

Type systems are hard! Lots of base info to learn. No worries 💜

Yes sorry!

This is a bug in the type checker, this code should be accepted. I think we are failing to generalise constants. Thank you