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

Find join of types, improve inference #61

Closed
6 tasks
turbolent opened this issue May 5, 2020 · 3 comments · Fixed by #1027
Closed
6 tasks

Find join of types, improve inference #61

turbolent opened this issue May 5, 2020 · 3 comments · Fixed by #1027
Assignees
Labels

Comments

@turbolent
Copy link
Member

Context

Implement a function to return the least common supertype.

This improves type checking for:

  • Conditionals: instead of just returning the left type (type of true branch) and requiring the right type (else branch) to be the same
  • Array literals
  • Dictionary literals

See "Types and Programming Languages, 16.3 Joins and Meets"

In the following example y should be inferred to String?:

let x: String? = ""
let y = true ? nil : x 

Definition of Done

  • Sema:
    • Add function to find join of two or more types
    • Use join function for conditionals
    • Use join function for array literals
    • Use join function for dictionary literals
  • Tests
  • Documentation
@turbolent turbolent self-assigned this May 5, 2020
@turbolent turbolent changed the title Find join of types, improve inferrence Find join of types, improve inference May 5, 2020
@turbolent turbolent added the Good First Issue Good for newcomers label May 7, 2020
@turbolent turbolent removed their assignment Jan 15, 2021
@bluesign
Copy link
Contributor

bluesign commented Jun 21, 2021

Sorry if this is not related but something I observed and curious if this fixes that:

   var  myDictionary = {
           1: true,
           2: false   
       }

  myDictionary = myDictionary as! {Int:AnyStruct};

Currently, this gives a conflicting error and warning:

Error:
mismatched types. expected {Int:Bool}, got {Int:AnyStruct}

Warning:
force cast ('as!') from to {Int:AnyStruct} always succeeds

@SupunS
Copy link
Member

SupunS commented Jun 21, 2021

These errors are actually for two places.

The first error mismatched types. expected {Int:Bool}, got {Int:AnyStruct} is for the assignment:
myDictionary = ..., where rhs-expr is forced to have {Int:AnyStruct}. But the variable is {Int:Bool}. Assigning a broader-type to a narrower-type is not allowed.

The second warning is for the cast: myDictionary as! {Int:AnyStruct};. because the upcasting/boxing is always guaranteed to succeed.
i.e: Force-casting is not required.

So both the error and the warning are valid in this case. Here they have overlapped at the same place. If you break the second statement into two, the error/warning would look much more clear/sensible:. e.g:

  var temp = myDictionary as! {Int:AnyStruct}    // warning goes here

  myDictionary = temp;   // error goes here

The supertype calculation wouldn't change this behavior either. Supertype inferring only happens at the expression level. It wouldn't consider multiple statements for type infering. The least common supertype for {1: true, 2: false} (therefore, also for myDictionary) would still be {Int:Bool}.

@bluesign
Copy link
Contributor

Thanks @SupunS, actually it makes sense when separating into two.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants