suggestions icon indicating copy to clipboard operation
suggestions copied to clipboard

Simpler single constructor type definition

Open edman opened this issue 4 years ago • 5 comments

How about accepting:

pub type Cat(name: String, cuteness: Int)

As the equivalent to:

pub type Cat {
  Cat(name: String, cuteness: Int)
}

edman avatar Dec 07 '20 23:12 edman

Gleam aims to not have multiple ways of writing the same thing, so this is something I'm not eager to add. However if this proves popular we can explore a concise sugar. Let’s see what people think

lpil avatar Dec 07 '20 23:12 lpil

I would like this

sporto avatar Dec 15 '20 09:12 sporto

I agree that it's a bit verbose, but the shorthand would be ambiguous with type parameters:

pub type Box(a) {
  Box(x: a)
}

// ???
pub type Box(a)(x: a)

A C-like struct maybe? This would be very confusing though and you'd have to peek for the colon to know it's not a union...

pub type Cat {
  name: String,
  cuteness: Int
}

pub type Box(a) {
  x: a
}

edongashi avatar Sep 20 '21 16:09 edongashi

Gleam aims to not have multiple ways of writing the same thing, so this is something I'm not eager to add.

I wholeheartedly agree with this principle, though for this particular scenario I don't see a reason to ever use the longer form for a single-case union if the short version existed. This could even be an auto-fix for the formatter...

Having an easy (and single-line) way to define records would encourage their use for smaller things which helps with code clarity.

pub type Cat { Cat(name: String, cuteness: Int) }

I'd sooner return a tuple than type out the above record for some internal function result.

After more thought I think type Cat(name: String, cuteness: Int) would be perfect (if the parameter type thing can be dealt with...)

edongashi avatar Sep 20 '21 16:09 edongashi

I'm not dead against having a concise syntax for this! Let's collect ideas for what it could be and at a later date we can see where we are.

lpil avatar Sep 20 '21 16:09 lpil