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

Context bounds are not added as dedicated given clause #7127

Closed
SrTobi opened this issue Aug 29, 2019 · 5 comments
Closed

Context bounds are not added as dedicated given clause #7127

SrTobi opened this issue Aug 29, 2019 · 5 comments
Assignees

Comments

@SrTobi
Copy link
Contributor

SrTobi commented Aug 29, 2019

(I'm on 0.18.1-bin-20190827-2bbfd17-NIGHTLY)

According to https://dotty.epfl.ch/docs/reference/contextual/context-bounds.html, context bounds are added to the function signature as their own given clause. But Apparently they are prepended to the last given clause

minimized code

class Somethingable[T]
def fun[T: Somethingable](t: T) given Int given String = ()

fun(1) given 3 given "test" given Somethingable()

expectation

Code compiles

actual

[error] -- [E007] Type Mismatch Error: /[...]/src/main/scala/Main.scala:58:21 
[error] 58 |fun(1) given 3 given "test" given Somethingable()
[error]    |                     ^^^^^^
[error]    |                     Found:    String("test")
[error]    |                     Required: Somethingable[Int]
[error] one error found

note

Following works:

class Somethingable[T]

def fun[T: Somethingable](t: T) given Int given String = ()


fun(true) given (3) given (Somethingable(), "test")

As you can see, Somethingable() has to be supplied as first parameter of the last given clause.

@SrTobi
Copy link
Contributor Author

SrTobi commented Aug 29, 2019

Ok, I found https://dotty.epfl.ch/docs/reference/contextual/relationship-implicits.html#context-bounds but I still think if you are using given clauses the context bounds should be added as a new clause.

And if not that then at least appended to the end of the last given clause and not be prepended.

@SrTobi
Copy link
Contributor Author

SrTobi commented Aug 29, 2019

I think this is done here:
https://github.com/lampepfl/dotty/blob/c3233d2eed02a9777fac3f68c6bc15d7faa0d441/compiler/src/dotty/tools/dotc/ast/Desugar.scala#L362

I think that evidenceParams and vparams must be swapped. Or check if one of the present clauses is a given clause and just add a new one

@nicolasstucki
Copy link
Contributor

Updated syntax

object Test {
  class Somethingable[T]
  def fun[T: Somethingable](t: T)(given Int)(given String) = ()
  fun(1)(given 3)(given "test")(given Somethingable())
}

@nicolasstucki
Copy link
Contributor

Note that the current desugaring adds the contexts bounds at the start of the last given/implicit parameter list. Desugaring to the previous example to

def fun[T](t: T)(given Int)(given Somethingable[T], String) = ()

This seems to be necessary for Scala2 compat.

@odersky
Copy link
Contributor

odersky commented Apr 5, 2022

Yes, it's necessary for Scala-2 compat.

@odersky odersky closed this as completed Apr 5, 2022
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

3 participants