grain icon indicating copy to clipboard operation
grain copied to clipboard

Type annotation shorthands?

Open ospencer opened this issue 5 years ago • 10 comments

Type annotations can get unwieldy pretty fast, with types like List<Option<Number>>. I think it would be pretty cool to have shorthands for common types, like [] for lists, maybe ? for options, etc.

With these shorthands, this type could be written as Number?[].

I'm not hard set on these particular shorthands, but I'm curious to hear what people think!

ospencer avatar Oct 15 '20 00:10 ospencer

Would type aliases help here? Since they wouldn't be exported, you could do something like:

type MaybeNumber = Option<Number>;

export type NumberList = List<MaybeNumber>;

It's definitely more verbose, but I don't like things to become "character soup", as I feel most functional languages start to become.

phated avatar Oct 15 '20 00:10 phated

Type aliases definitely help. You'd have to also export MaybeNumber here though since it'd be an opaque type otherwise.

I definitely feel where you're coming from with the character soup. I feel like as long as it's fairly limited it can be okay, though. I really like how writing int[] feels in languages like C, TypeScript, and Go.

ospencer avatar Oct 15 '20 00:10 ospencer

Oh, something else that might get confusing is if we add ? as an "unwrap-or-fail" operator. Then, using ? in type signatures as the Option type would be weird.

I'm also now thinking that using ? for option doesn't really fit well with Result being an oft-used return type too. Hmm 🤔

phated avatar Oct 15 '20 00:10 phated

Yeah, I tried thinking about something short for Result, but it almost feels impossible since you've always got to give it two type params.

ospencer avatar Oct 15 '20 01:10 ospencer

I actually don’t love ? for Option in TS or Swift or C♯. It tends to lead people down the Dark Path™ of sprinkling it everywhere instead of actually thinking about the right model for their data. I find [] for array to be a lot less of a nuisance that way.

chriskrycho avatar Oct 15 '20 03:10 chriskrycho

I was spinning on this a lot last night and if ? represents optional data, I'd want it to both be the type annotation AND an alias for Some(val), like maybeString: () -> ?string and maybeString = () => ?"hello world". So prefix would be the convenience for wrapping and postfix would be the unwrap-or-throw helper.

phated avatar Oct 15 '20 21:10 phated

I feel using shorthand for common operation definitely helps.it makes the code more readable.I think typescript has done this really well. We could use ? for optional values, ! To force unwrap. ?. To unwrap only if values is not null would be helpful

sanathusk avatar Jul 16 '22 02:07 sanathusk

V lang to seems have done this beautifully, https://github.com/vlang/v/blob/master/doc/docs.md#optionresult-types-and-error-handling

sanathusk avatar Jul 16 '22 05:07 sanathusk

I really like the or { "default" } syntax from VLang but I still think the ? makes for "character soup". Not sure how to reconcile my thoughts around that.

phated avatar Jul 16 '22 18:07 phated

I feel like ? and ! while common syntax definitely leads to character soup, just imagine someone doing something like number????. On the other hand, I think we are way overdue on having a shorthand for list and array. I think the only shorthands we should support are:

  • type Foo = number[] which would be equivalent to type foo = List<Number>
  • type Foo = number[>] which would be equivalent to type foo = Array<Number> The only downside of this I think is it makes searching through the codebase a little harder, a step in the right direction would be having the formatter convert to the shorthand.

spotandjake avatar Feb 14 '24 07:02 spotandjake