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

Compiler warning if two types have any field names in common #775

Closed
cician opened this issue Jul 10, 2021 · 5 comments
Closed

Compiler warning if two types have any field names in common #775

cician opened this issue Jul 10, 2021 · 5 comments

Comments

@cician
Copy link
Contributor

cician commented Jul 10, 2021

The compiler emits a warning for the following program. Unless there's something fundamental I don't understand about Grain's type system, I think the two record types should coexist peacefully and stay independent of each other if I use explicit type annotations.

record A {
  a: Number
}

record B {
  a: Number,
  b: Number
}

let a = {
  a: 1
} : A

let b = {
  a: 1,
  b: 2
} : B

let c = b.a // Warning: a belongs to several types: B A The first one was selected. Please disambiguate if this is wrong.

Same happens in this case:

let f = (x: B) => {
  let c = b.a
}
@alex-snezhko
Copy link
Member

To follow up on this, is there any way to get this warning to go away? I ran into this same situation and have tried disambiguating in various ways but the warning still shows

@spotandjake
Copy link
Member

spotandjake commented Aug 13, 2022

To follow up on this, is there any way to get this warning to go away? I ran into this same situation and have tried disambiguating in various ways but the warning still shows

you can do

{ recordInfo } : Type

which should prevent the warning.

@SteveSandersonMS
Copy link

This project looks really interesting!

you can do { recordInfo } : Type which should prevent the warning

Could you clarify further? Considering the following code:

record Person { id: Number }
record Company { id: Number }

let person = { id: 123 }: Person
let company = { id: 123 }: Company

print(person.id)
print(company.id) // For some reason this gives a warning even though the preceding line is considered fine

I'm guessing it's a compiler bug that person.id is allowed whereas company.id is not. Is that right?

And assuming so it's possible to work around by disambiguating manually, what's the correct syntax for the final line in this sample?

@spotandjake spotandjake changed the title Compiler warning if two records have any field names in common Compiler warning if two types have any field names in common May 27, 2023
@spotandjake
Copy link
Member

spotandjake commented May 27, 2023

The name of this issue has been changed because this isnt exclusive to records.

Record Issue

The initial issue references Records which can be handled just by adding a : TypeName after the record definition.

as @SteveSandersonMS referenced above with print(company.id) this scenario there does not seem to be a way to silence the error.

Enums

Enums have the same issue take the example program here:

module Test

enum NodeA {
  Same, // Same
  DiffB,
}
enum NodeB {
  Same, // Same
  DiffA,
}
let a = Same: NodeB
match (a) {
  DiffA => void,
  Same => void,
}

as can be seen in the sample program we are able to dissambiguate at the deffinition the same way we do for records Same: NodeB but we are not able to dissambiguate at the match statement leaving a warning on the line Same => void.

A note is lets say you had a program like:

enum Status {
  Success,
  Failure,
}
let x = Success
match (x) {
  Sucess => void,
  Failure => void,
}

you would get an error on the line Failure => void because of the exception Failure in pervasives.

Work Arrounds

for now in the case of records with the same fields and defining enums you can just append : TypeName after initializaton as stated and displayed above. As per pattern matching there is not a work arround so if you want the error to go away unfortunately you will have to change the name of your variant until this is fixed.

@ospencer
Copy link
Member

ospencer commented Apr 5, 2024

Fixed in #2072

@ospencer ospencer closed this as completed Apr 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants