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

import.{given ?} doesn't work, but probably should #8696

Closed
neko-kai opened this issue Apr 8, 2020 · 1 comment
Closed

import.{given ?} doesn't work, but probably should #8696

neko-kai opened this issue Apr 8, 2020 · 1 comment

Comments

@neko-kai
Copy link
Contributor

neko-kai commented Apr 8, 2020

Minimized code

@main def main(): Unit = {
  final case class Item(a: Int)
  object Item {
    extension on (i: Int) {
      def squashWith(l: Iterable[Item]): Int = (List(Item(i))) ++ l).map(_.a).sum
    }
  }
  import Item.{given ?}
  println(5 squashWith List(Item(1), Item(2)))
}

Output

Error:(35, 13) value squashWith is not a member of Int/T, but could be made available as an extension method.
The following import might fix the problem:
  import Item.extension_squashWith_Int.squashWith
         
  println(5 squashWith List(Item(1), Item(2)))

Expectation

Should work. Moreover, if the wildcard is hidden by a type-alias it compiles and works:

  type Xa[A] = A
  import Item.{given Xa[?]} // works now

Motivation

Given import imports givens based on their compatibility with the given type, naturally all types are compatible with the wildcard type, as evidenced by Xa[?] working correctly - it may even be more natural to write than given _ if you look at it from the point of view of syntactical consistency – although I do not advocate for replacement of given _ with given ?.

@odersky
Copy link
Contributor

odersky commented Apr 10, 2020

Wildcard types are syntactically only allowed as type arguments to other types. So this will not work.

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

2 participants