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 lets you create implicit by-name parameters, no way to actually use them #2688

Closed
scabug opened this issue Nov 23, 2009 · 4 comments
Assignees

Comments

@scabug
Copy link

scabug commented Nov 23, 2009

I created a function like this:

def doit(implicit f: => Int) = i * 2

Of course in my actual program "doit" actually does something and only evaluates f occasionally.

The f parameter is implicit because I want callers to be able to use an implicit default value when they know that f will never actually be evaluated:

implicit val default = 0

Unfortunately there seems to be no way to create a val, var, or def in such a way that Scala will choose it as the implicit argument to doit. I tried the following:

implicit def default = 0
implicit val default = { () => 0 }
implicit val default: => Int = 0

Of course the third one doesn't even compile.

If the compiler allows the creation of an implicit by-name parameter, then it should be willing to select an implicit argument for it. If not, then it should be illegal to create the implicit by-name parameter in the first place.

@scabug
Copy link
Author

scabug commented Nov 23, 2009

Imported From: https://issues.scala-lang.org/browse/SI-2688?orig=1
Reporter: Willis Blackburn (willisblackburn)

@scabug
Copy link
Author

scabug commented Nov 24, 2009

@adriaanm said:

Welcome to Scala version 2.8.0.r0-b20091123212008 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_15).
Type in expressions to have them evaluated.
Type :help for more information.

scala> def doit(implicit f: => Int) = f * 2
doit: (implicit f: => Int)Int

scala> implicit val x = 42
x: Int = 42

scala> doit
<console>:7: error: could not find implicit value for parameter f: => Int
       doit
       ^

scala> implicit val x: () => Int = () => 42
x: () => Int = <function0>

scala> doit                                
<console>:9: error: could not find implicit value for parameter f: => Int
       doit
       ^

@scabug
Copy link
Author

scabug commented Mar 16, 2010

@odersky said:
(In r21196) Closes #2688 by disallowing call-by-name implicit parameters. No review.

@scabug
Copy link
Author

scabug commented Mar 16, 2010

@odersky said:
see also r21197 which contains the change in specification.

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

No branches or pull requests

2 participants