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

Nil coalescing operator ambiguity #2619

Closed
skreberem opened this issue Oct 28, 2022 · 6 comments · Fixed by #2650
Closed

Nil coalescing operator ambiguity #2619

skreberem opened this issue Oct 28, 2022 · 6 comments · Fixed by #2650
Assignees
Labels
codegen Issues related to or arising from code generation
Milestone

Comments

@skreberem
Copy link

Nil coalescing operator overload for GraphQLNullable results in ambiguity errors where type is not explicitly specified.

Example:

let width = rect?.width ?? 0

Error:

Ambiguous use of operator '??'

@gsapienza
Copy link

I am also experiencing this

@calvincestari
Copy link
Member

I don't know if there is any good way around this. I think it's caused by the fact that GraphQLNullable is simply a wrapper around many supported types. So the compiler is correct in telling you that it cannot infer which one it should use without conclusively knowing the property type.

It's resolved by specifying the type of width but I can see how that would be annoying if this syntax is used in many places of your existing codebase. Any suggestions?

@milowyner
Copy link

Same issue here, and the codebase I'm working on has a lot of places where nil coalescing is used, so I just had to comment out the operator overload in GraphQLNullable, which isn't a great solution.

@calvincestari
Copy link
Member

calvincestari commented Nov 8, 2022

Instead of commenting it out, try adding @_disfavoredOverload above the operator overload and see if it changes how the compiler treats the ambiguity.

@milowyner
Copy link

Ah thanks. That also works, and it feels like a better temporary solution. I was going to say they should add this to the SDK, but it seems that it's not recommended outside the compiler and standard library 😕 https://github.com/apple/swift/blob/main/docs/ReferenceGuides/UnderscoredAttributes.md

@calvincestari
Copy link
Member

I'm glad to hear it improves the issue, I'm going to go ahead with making that the fix and get it into 1.0.4. We already use a couple other @_ attributes in other places so we're comfortable using those for now. Thanks for trying it in your code, I appreciate the time.

@calvincestari calvincestari added codegen Issues related to or arising from code generation and removed awaiting response labels Nov 8, 2022
@calvincestari calvincestari self-assigned this Nov 8, 2022
@calvincestari calvincestari added this to the Release 1.0.4 milestone Nov 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
codegen Issues related to or arising from code generation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants