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

Fixed type inference for 'set` and 'tuple' #18827

Merged
merged 3 commits into from
Sep 11, 2021
Merged

Conversation

beef331
Copy link
Collaborator

@beef331 beef331 commented Sep 9, 2021

Closes: #18826
Also the following no longer crashes the compiler:

proc b(val: int or float): set = 
  when typeof(val) is int:
    {10u8, 3}
  else:
    {'a', 'b'}
assert typeof(b(10)) is set[uint8]
assert typeof(b(10.0)) is set[char]

@Araq
Copy link
Member

Araq commented Sep 10, 2021

The crash is bad of course but why should this code compile? Where's the merit in allowing partially specified return types? Either write the return type or use auto. Type classes are constraints for parameters, not for return types.

@ho0ber
Copy link

ho0ber commented Sep 10, 2021

Where's the merit in allowing partially specified return types? Either write the return type or use auto. Type classes are constraints for parameters, not for return types.

Yeah, my perspective as someone new to Nim is that there was nothing telling me that using a tuple return type was "wrong", especially because it had worked in every prior circumstance (where I was returning tuple variables). It was quite baffling to have the code work fine if I added a line and assigned my return value to a tuple, vs returning that value directly.

I think that either a fix like this PR OR a more expressive compiler error would have made this less difficult for me. I guess the alternative is to just disallow using tuple as a return type altogether so it doesn't seem to work fine in some circumstances if it's not idiomatic.

I don't think this is likely to trip up anyone but beginners, but this is the kind of error that makes beginners think a language isn't ready for serious use; it seemed odd that the same behaviors wouldn't apply to returns that apply to assignment.

@Araq Araq merged commit 66e53bd into nim-lang:devel Sep 11, 2021
PMunch pushed a commit to PMunch/Nim that referenced this pull request Mar 28, 2022
* improved built in typeclass inference

* Smarter logic to fit node

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

Successfully merging this pull request may close these issues.

Returning tuple literal in proc with tuple return type gives compiler error
3 participants