Skip to content

Implicit def not found if it occurs later in same file #1814

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

Closed
scabug opened this issue Mar 21, 2009 · 5 comments
Closed

Implicit def not found if it occurs later in same file #1814

scabug opened this issue Mar 21, 2009 · 5 comments
Assignees
Labels

Comments

@scabug
Copy link

scabug commented Mar 21, 2009

In the following code, whether an implicit conversion is found by the compiler depends on the order of modules in the file, and on whether the method is called explicitly in a seperate statement:

// code doesn't compile, no suitable implicit found for the last println
object Foo extends Application {
  import Utils.string2FooString;
  //println(string2FooString("%s = %s") % ("foo", "bar")); // this line causes next line to compile!
  println("str" % ("foo", "bar"));
}

// if this module is moved to the top of the file, the code compiles
object Utils {
  class FooString(s: String) {
    def % (arg1: String, arg2: String) = arg1+arg2;
  }
    
  implicit def string2FooString(s: String) = new FooString(s)
}
@scabug
Copy link
Author

scabug commented Mar 21, 2009

Imported From: https://issues.scala-lang.org/browse/SI-1814?orig=1
Reporter: David Greenspan (dgreensp)

@scabug
Copy link
Author

scabug commented Mar 21, 2009

David Greenspan (dgreensp) said:
Also compiles correctly if the implicit def is given a return type of FooString.

@scabug
Copy link
Author

scabug commented Mar 22, 2009

@DRMacIver said:
This is actually a feature. It was changed in 2.7.1 I think. I can't seem to find it in the SLS though.

@scabug
Copy link
Author

scabug commented May 5, 2009

@paulp said:
This is essentially a duplicate of #1209 and at least one other ticket I can't find right now. If you want your implicits to work everywhere, declare the return type.

  implicit def string2FooString(s: String): FooString = new FooString(s)

That works.

  //println(string2FooString("%s = %s") % ("foo", "bar")); // this line causes next line to compile!

That works because calling it explicitly causes the return type to be resolved.

// if this module is moved to the top of the file, the code compiles

That works because then the implicit's return type is resolved before it gets to the operation which needs it.

@scabug
Copy link
Author

scabug commented May 5, 2009

@paulp said:
Ah, there's the one I couldn't find: #801.

"I have implemented a slightly more permissive rule: An implicit conversion without explicit result type is visible only in the text following its own definition. That way, we avoid the cyclic reference errors. I close for now, to see how this works. If we still have issues we migth come back to this."

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

No branches or pull requests

2 participants