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

Proc with Void return type #3655

Closed
chenkovsky opened this issue Dec 8, 2016 · 7 comments · Fixed by #7527
Closed

Proc with Void return type #3655

chenkovsky opened this issue Dec 8, 2016 · 7 comments · Fixed by #7527
Labels
help wanted This issue is generally accepted and needs someone to pick it up kind:feature topic:compiler

Comments

@chenkovsky
Copy link

class A
  @@a : String -> Void
  @@a = ->(y : String){
    	1
  }
end

compiler complains Proc type doesn't match.

@ysbaddaden
Copy link
Contributor

Because your block returns 1, thus is a Proc(String, Int32) not a Proc(String, Nil).

@chenkovsky
Copy link
Author

@ysbaddaden but the following code can be compiled.

def foo (&block : Int32 -> Void)
  yield 1
end

foo do
  1
end

@asterite
Copy link
Member

asterite commented Dec 8, 2016

Right now the compiler, in many places, to determine the type of something, just takes the type on the left-hand side, the type on the right-hand side, combines them, and tries to assign the result to the left-hand side. Here a union is created and it doesn't match the original type.

However, since we introduced explicit types in instance variables this rule can be changed or relaxed. In those cases we should try to see if the right-hand side can be implicitly converted to the left-hand side.

In the case of a block, it's simpler: if the block specification returns Void (or Nil, please use Nil instead of Void), then the block's type is inferred to be Nil.

I'll mark this as an enhancement.

@asterite
Copy link
Member

asterite commented Dec 8, 2016

A simple workaround is of course:

class A
  @@a : String -> Nil
  @@a = ->(y : String){
    	1
        nil
  }
end

@paulcsmith
Copy link
Contributor

Could this be bumped? Me and a few others have run into this. It is a bit strange since return types on regular methods set to Nil will be coerced to nil, but not the same for Procs. This has led to a fair bit of confusion so I wonder if it could be prioritized?

Also just for clarity to anyone reading this that isn't familiar:

def my_method : Nil
  "a string"
end

Will still compile. This is basically Void return type in other languages. But doing it for a Proc currently requires an explicit nil return

@straight-shoota
Copy link
Member

PR's are welcome 👍

@straight-shoota straight-shoota added the help wanted This issue is generally accepted and needs someone to pick it up label Mar 8, 2019
@asterite
Copy link
Member

asterite commented Mar 8, 2019

I can try to see if I can merge procs of same argument types and Nil and T return types into Nil return type. Maybe that'll work and it makes sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted This issue is generally accepted and needs someone to pick it up kind:feature topic:compiler
Projects
None yet
5 participants