#Fields with reserved name

1 messages · Page 1 of 1 (latest)

wary shadow
#

Hello, I'm very new to Gleam, so I don't know what I'm doing yet. I'm trying to expose a type that is backed by an Elixir struct. The struct has a field named "type" and I can't seem to represent that in a Gleam custom type. the compiler complains:

pub type Event {
Event(
id: String,
type: String,
)
}

type: String,
^^^^ I was not expecting this.

I assume this is because I'm trying to use a reserved word. If I owned the type, I would likely rename it, is there anything I can do from the Gleam side?

Thanks

atomic palm
#

I think youre right that the error is because of a reserved word, but also I think that this wouldn't work anyway because elixir structs are built on top of maps, and gleam types are built on top of tuples, so they are not compatible like this

#

for example your Event type is describing a tuple like
{:event, "some id", "some type"}

grizzled bolt
#

Yeah, there are generally two strategies with structs: 1) use an external type so it's opaque in Gleam and you use getter/setter functions to interact with it, or 2) completely move it to a Gleam type and use it with defrecord in Elixir

#

Personally I've used type_ as an alias for type