Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unions - only as constraints (like go) or everywhere (like ts)? #130

Closed
emil14 opened this issue Dec 17, 2022 · 2 comments
Closed

Unions - only as constraints (like go) or everywhere (like ts)? #130

emil14 opened this issue Dec 17, 2022 · 2 comments
Labels

Comments

@emil14
Copy link
Collaborator

emil14 commented Dec 17, 2022

Consider this valid TS code:

function f(a, b number | string) {}
f("42", 42) 

It's impossible to do so in Go because

  1. union can only be used as a constraint:
  2. values of the same generic type must have same concrete type
type C interface { int | string }

func f[T C](a, b T) {}

// f("42", 42) // won't compile because compiler "can't infer type"
f(42, 42) // will compile, compiler infers `f[int]`

Let's look at that f("42", 42) line. It says compiler can't infer type because the type here would be f[int | string] and Go doesn't allow union to be used that way. You can't use union anywhere but inside interface that is used as a type constraint.

Go approach

Makes impossible situations like string + int. Go is a strong-typed language which means you must explicitly cast one type into another in such situations. Go does have operators that are builtin functions with predefined syntax. You won't find type declarations for them like you can for builtin functions like make or new but their behavior is described in the spec. Operator + e.g. works for both numeric types and strings but both arguments must be of the same type.

TS approach

On the other hand TypeScript is a superset of JS which is a weakly-typed language. You can sum numbers with strings and much more. That's why using unions everywhere makes sense. However, such implicit type casts works not everywhere. E.g. you can't - from string to number. This leads to situations like this:

function f(a, b number | string) {
  a - b // compile error
}
f("42", 42) 

You can probably do function f<T number | string>(a, b T) which shouldn't change anything here.

@emil14 emil14 changed the title Unions - only contraints (like go) or everywhere (like ts)? Unions - only as constraints (like go) or everywhere (like ts)? Dec 17, 2022
@emil14
Copy link
Collaborator Author

emil14 commented Dec 17, 2022

It's all about operators

At the lowest level message is passed to an operator. Operator can have type int|str. What will happen if we'll have messages of different types? Or with the same?

@emil14
Copy link
Collaborator Author

emil14 commented Dec 10, 2023

dupl of #407

@emil14 emil14 closed this as completed Dec 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant