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

options.flatMap doesn't work with sugar.=> #15880

Open
GULPF opened this issue Nov 7, 2020 · 4 comments
Open

options.flatMap doesn't work with sugar.=> #15880

GULPF opened this issue Nov 7, 2020 · 4 comments

Comments

@GULPF
Copy link
Member

GULPF commented Nov 7, 2020

Example

import options, sugar

var x = some(1)
echo x.map(proc(y: int): int = y + 1) # ok
echo x.map(y => y + 1) # ok
echo x.flatMap(proc(y: int): Option[int] = some(y + 1)) # ok
echo x.flatMap(y => some(y + 1)) # Error: cannot instantiate: 'B'

Additional Information

$ nim -v
Nim Compiler Version 1.4.0
@momeemt
Copy link

momeemt commented Jan 4, 2021

Hello.
It seems that the => macro generates a procedure with all argument and return value types set to auto type.
However, I tried and found that the current implementation of the auto type implicitly generates a generic routine (source) that automatically sets the Option[T] type to It was not possible to get the Option[T] type automatically.
The same error seems to occur in another example.
I suspect that it is not due to the implementation of the => macro, but due to the inability to get the auto type.

And I don't think this is a problem that can be solved by implementing the => macro.

@schneiderfelipe
Copy link

Hi @GULPF, a possible solution is to use the do notation:

import options

var x = some(1)
echo x.map(proc(y: int): int = y + 1) # ok
echo x.map do (y: auto) -> auto:
  y + 1  # ok
echo x.flatMap(proc(y: int): Option[int] = some(y + 1)) # ok
echo x.flatMap do (y: int) -> auto:
  some(y + 1)  # ok

Unfortunately, you have to specify at least y: int as explained by @momeemt. It would be awesome to have parameter types be inferred from the function, though!

@dom96
Copy link
Contributor

dom96 commented Jun 22, 2021

IMO if you're going to use the do notation you might as well just write out the full proc (the do notation should be removed from the language :)). Here is a better workaround:

echo x.flatMap((y: int) -> Option[int] => some(y + 1))

@schneiderfelipe
Copy link

IMO if you're going to use the do notation you might as well just write out the full proc (the do notation should be removed from the language :)). Here is a better workaround:

echo x.flatMap((y: int) -> Option[int] => some(y + 1))

I'm embarrassed, I didn't know I could use -> together with =>! 😆

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

5 participants