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

Allow specifying a multi type's type to help with type widening ambiguities #643

Open
CohenArthur opened this issue Jan 6, 2024 · 4 comments
Labels
design Issue or concerns regarding jinko's design generics Issue with generic types typechecker Issue related to typechecking in jinko

Comments

@CohenArthur
Copy link
Member

e.g. with the following result type:

type Result[T, E] = T | E;

func ambiguous() -> Result[int, int] {
    15 // Ok or Err?
}

which we could instead write as:

type Result[T, E] = T | E;

func ambiguous() -> Result[int, int] {
    Result.T(15) // Ok
}

and with more descriptive generic names

type Result[Ok, Err] = Ok | Err;

func ambiguous() -> Result[int, int] {
    Result.Ok(15)
}
@CohenArthur CohenArthur added design Issue or concerns regarding jinko's design generics Issue with generic types typechecker Issue related to typechecking in jinko labels Jan 6, 2024
@CohenArthur
Copy link
Member Author

this means that flattening ambiguities in the typechecker would now be allowed, which I'm not a big fan of. so this is more of a design question than an actual "let's do this" issue. does it even make sense for a multi type/sum type to have duplicated types?

@CohenArthur
Copy link
Member Author

I think overall it would be better to have helps around designing sum types. the above problem can be avoided by creating a sum type instead of an union type, like so:

type Ok[T](T);
type Err[E](E);

type Result[T, E] = Ok[T] | Err[E];

which then allows Result[int, int] and requires disambiguation when returning the value:

func no_longer_ambiguous() -> Result[int, int] {
    Ok(15) // or Err(15)
}

@CohenArthur
Copy link
Member Author

so having a shorthand for creating these types in the same scope would work well:

type Result[T, E] = type Ok[T](T) | type Err[E](E); // maybe? very verbose

we can have the kind system extend it and make it easier but it's even harder to read IMO:

type Result[T, E] = newtype["Ok", T] | newtype["Err", E];

@CohenArthur
Copy link
Member Author

so probably it would be better to have Maybe be defined as a "sum type" as well -

type Just[T](T);
type Nothing;

type Maybe[T] = Just[T] | Nothing;

which could be different from something like Nullable[T]:

type Null;

type Nullable[T] = T | Null;

which would not allow a Nullable[Null] to exist

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
design Issue or concerns regarding jinko's design generics Issue with generic types typechecker Issue related to typechecking in jinko
Projects
None yet
Development

No branches or pull requests

1 participant