gleam icon indicating copy to clipboard operation
gleam copied to clipboard

Cannot make a const list of other constants

Open lauritzsh opened this issue 2 years ago • 2 comments

Trying to write a constant list of other constants results in an error, please see https://johndoneth.github.io/gleam-playground/?s=MYewdgzgLgBAhjAvDARHFAoUlYCMmq6bbQzAErDHily7nIDacANDLm8ALpA%3D for an example.

error: Syntax error
  ┌─ src/main.gleam:4:14
  │
4 │ const abc = [a, b, c]
  │              ^ This type is not allowed in module constants.

See: https://gleam.run/book/tour/constants

lauritzsh avatar Dec 25 '21 20:12 lauritzsh

@lpil mentioned in Discord earlier that this limitation is unintended, and should be fixed.

The issue at least starts in the parser, in the parse_const_value function: https://github.com/gleam-lang/gleam/blob/a0d3cf1a0fa2a4bf821360470b8da7d1a18a55cc/compiler-core/src/parse.rs#L1931-L2027

The TL;DR is that this functions expects constant values to be literals. The only use of Token::Name (i.e. a lowercase string token) that's allowed is when it is being used to refer to a remote type constructor, like uri in uri.URI(scheme: ...).

Updating the match arm for Token::Name would be a start, but there may be further updates may need to be made in the code generation layers.

I'll have a go at fixing this tomorrow.

Update: I realize now that this will mean introducing a new Constant::Var AST node, which implies that it may be a lot of work :sweat_smile:

J3RN avatar Dec 26 '21 01:12 J3RN

Yes, let's fix this one. Happy to answer any questions RE implementation.

lpil avatar Dec 26 '21 11:12 lpil

This issue doesn't seem to be present in the current version. (0.30.3)

PastMoments avatar Jul 30 '23 11:07 PastMoments

Thank you

lpil avatar Jul 30 '23 13:07 lpil