lpil / snag

⛵ A boilerplate-free ad-hoc error type
Apache License 2.0
49 stars 2 forks source link

Can not using imported `Snag` as type #4

Closed xhh closed 8 months ago

xhh commented 8 months ago

This might not relate to snag, but to Gleam.

Code:

import gleam/io
import gleam/string.{inspect}
import snag.{Snag}

pub fn main() {
  let r: Snag = snag.new("hello") // Error on this line
  io.println(inspect(r))
}

Error:

Unknown type

Did you mean `Int`?

The type `Snag` is not defined or imported in this module.

It's OK if change that line to (but import snag.{Snag} becomes an "Unused imported item" warning):

  let r: snag.Snag = snag.new("hello")

and this also works:

  let r: snag.Snag = Snag("hello", [])
xhh commented 8 months ago
import gleam/io
import gleam/string.{inspect}
import snag.{type Snag, Snag}

pub fn main() {
  let r: Snag = snag.Snag("hello", [])
  // or:
  //   let r: snag.Snag = Snag("hello", [])
  io.println(inspect(r))
}

In the above code, the latter Snag import shoud be a warning of Unused imported item, but there's none.

lpil commented 8 months ago

Hi there! If you want to import a type the syntax is import module.{type TypeName}. If you want to import a value the syntax is import module.{Value}. If you want to import a type and a value with the same name you'll need to import them both.

The dead code detection feature of the compiler is due to be rewritten in 2024, it does not detect all dead code today.