gleam-lang / suggestions

📙 A place for ideas and feedback
26 stars 2 forks source link

Simpler single constructor type definition #111

Open edman opened 3 years ago

edman commented 3 years ago

How about accepting:

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

As the equivalent to:

pub type Cat {
  Cat(name: String, cuteness: Int)
}
lpil commented 3 years ago

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

sporto commented 3 years ago

I would like this

edongashi commented 2 years ago

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 commented 2 years ago

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...)

lpil commented 2 years ago

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.