grain
grain copied to clipboard
Type annotation shorthands?
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!
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.
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.
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 🤔
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.
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.
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.
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
V lang to seems have done this beautifully, https://github.com/vlang/v/blob/master/doc/docs.md#optionresult-types-and-error-handling
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.
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 totype foo = List<Number>type Foo = number[>]which would be equivalent totype 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.