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

can type inference be made to work for f-bounded type params? #4745

Open
scabug opened this issue Jun 28, 2011 · 5 comments
Open

can type inference be made to work for f-bounded type params? #4745

scabug opened this issue Jun 28, 2011 · 5 comments
Labels
enhancement f-bounds fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) infer
Milestone

Comments

@scabug
Copy link

scabug commented Jun 28, 2011

scala> object +: {
     |     def unapply[T, Coll <: LinearSeqLike[T, Coll]](t : Coll with LinearSeqLike[T, Coll]) : Option[(T, Coll)] =
     |       if (t.isEmpty) None
     |       else Some(t.head -> t.tail) // TODO - Try to remove cast.
     | }
defined module $plus$colon

scala>  +:.unapply(List(1,2,3,4))
res3: Option[(Int, List[Int])] = Some((1,List(2, 3, 4)))

scala>  List(1,2,3,4) match { case x +: xs => xs }
res4: List[Int] = List(2, 3, 4)

works fine, but if we remove the seemingly redundant second constituent in t : Coll with LinearSeqLike[T, Coll], type inference fails, because Coll is f-bounded (?)

can we generalise this pattern to make type inference work for f-bounds?

@scabug
Copy link
Author

scabug commented Jun 28, 2011

Imported From: https://issues.scala-lang.org/browse/SI-4745?orig=1
Reporter: @adriaanm

@scabug
Copy link
Author

scabug commented May 18, 2012

@paulp said:

object +: {
  import collection.LinearSeqLike
  def unapply[T, Coll <: LinearSeqLike[T, Coll]](t : Coll with LinearSeqLike[T, Coll]) : Option[(T, Coll)] = {
    if (t.isEmpty) None
    else Some(t.head -> t.tail) // TODO - Try to remove cast.
  }
}

A more pasteable version.

@scabug
Copy link
Author

scabug commented Mar 30, 2013

@retronym said (edited on Mar 30, 2013 10:18:12 PM UTC):
Here's how this technique can be applied to F-Bounded builders, e.g. JavaFX, as discussed in #6169.

qbin/scala -classpath /Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/jre/lib/jfxrt.jar
Welcome to Scala version 2.11.0-20130330-204812-2a6e83a85e (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_37).

scala> type CircleBuilderF = ({type CB <: CircleBuilder[CB]; type CB1 = CB with CircleBuilder[CB]})#CB1
defined type alias CircleBuilderF

scala> def newCircleBuilder: CircleBuilderF = CircleBuilder.create.asInstanceOf[CircleBuilderF]
newCircleBuilder: CircleBuilderF

scala> newCircleBuilder.centerX(0).centerX(0).build()
res28: javafx.scene.shape.Circle = Circle@38b9a306

Compare with:

scala> CircleBuilder.create.centerX(0).centerX(0).build()
<console>:14: error: value centerX is not a member of ?0
              CircleBuilder.create.centerX(0).centerX(0).build()
                                              ^

I was unable to a come up shorthand to express CircleBuilderF.

scala> trait FBounded[M[_]] { type F <: M[F]; type Apply = F with M[F] }
warning: there were 1 feature warning(s); re-run with -feature for details
defined trait FBounded

scala> CircleBuilder.create.asInstanceOf[FBounded[CircleBuilder]#Apply].centerX(0).centerX(0).build()
<console>:15: error: kinds of the type arguments (javafx.scene.shape.CircleBuilder) do not conform to the expected kinds of the type parameters (type M) in trait FBounded.
javafx.scene.shape.CircleBuilder's type parameters do not match type M's expected parameters:
type B's bounds <: javafx.scene.shape.CircleBuilder[B] are stricter than type _'s declared bounds >: Nothing <: Any
              CircleBuilder.create.asInstanceOf[FBounded[CircleBuilder]#Apply].centerX(0).centerX(0).build()
                                            ^

@scabug
Copy link
Author

scabug commented Mar 30, 2013

@retronym said (edited on Mar 31, 2013 10:38:38 AM UTC):
A similar, but simpler, formulation with existentials.

scala> implicit class RichFBoundBuilder[B[X <: B[X]]](val _builder: B[_]) {
  type B1 = X forSome { type X <: B[X] }
  def asScala: B1 = _builder.asInstanceOf[B1]
}
warning: there were 2 feature warning(s); re-run with -feature for details
defined class RichFBoundBuilder

scala> import javafx.scene.shape._
import javafx.scene.shape._

scala> CircleBuilder.create.asScala.centerX(0).centerX(0).build()
res0: javafx.scene.shape.Circle = Circle@6c60efe7 

@newca12
Copy link

newca12 commented Dec 21, 2018

Someone should add the "fixed in dotty" label for this issue

@NthPortal NthPortal added the fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) label Dec 25, 2018
@scala scala deleted a comment from scabug Jan 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement f-bounds fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) infer
Projects
None yet
Development

No branches or pull requests

4 participants