Skip to content

Let traits with given clauses be implemented indirectly #7613

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
travisbrown opened this issue Jul 20, 2019 · 2 comments · Fixed by #11830
Closed

Let traits with given clauses be implemented indirectly #7613

travisbrown opened this issue Jul 20, 2019 · 2 comments · Fixed by #11830
Assignees
Milestone

Comments

@travisbrown
Copy link
Contributor

Suppose you have a hierarchy like this:

trait Foo[A]
trait Bar[A] extends Foo[A]
trait Baz[A] extends Bar[A]

trait FooLaws[A] given Foo[A]
trait BarLaws[A] given Bar[A] extends FooLaws[A]
trait BazLaws[A] given Baz[A] extends BarLaws[A]

It'd be nice to be able to write something like this:

def instance[A] given Baz[A]: BazLaws[A] = new BazLaws[A] {}

Instead of:

def instance[A] given Baz[A]: BazLaws[A] =
  new BazLaws[A] with BarLaws[A] with FooLaws[A] {}

The former fails on the current nightly with:

1 |def instance[A] given Baz[A]: BazLaws[A] = new BazLaws[A] {}
  |                                           ^
  |          parameterized trait FooLaws is indirectly implemented,
  |          needs to be implemented directly so that arguments can be passed
@smarter
Copy link
Member

smarter commented Jul 21, 2019

SIP-25 says "Only classes can pass arguments to parent traits. Traits themselves can pass arguments to neither classes nor traits." and this is pretty central to making trait constructors work I think, so BarLaws can't pass its Bar[A] to FooLaws. However what we could maybe do is: when we see that there's an indirectly implemented trait whose constructor parameters haven't been filled and these parameters are all implicit, perform an implicit search before failing with the error you're seeing.

@soronpo
Copy link
Contributor

soronpo commented Sep 8, 2019

I also proposed something similar on the SIP's contributor's thread https://contributors.scala-lang.org/t/proposal-to-add-trait-parameters-to-the-language/2356

@smarter smarter transferred this issue from lampepfl/dotty-feature-requests Nov 25, 2019
@smarter smarter added this to the 0.21 Tech Preview milestone Nov 25, 2019
@anatoliykmetyuk anatoliykmetyuk removed this from the 0.21 Tech Preview milestone Dec 16, 2019
odersky added a commit to dotty-staging/dotty that referenced this issue Mar 21, 2021
Namer will augment the parent types of a class with possibly other types.
Currently the only such change is a possibly leading class type, but with
scala#7613 we wll also generate trait parents that take context parameters.
The new method `parentTrees` will reflect any such changes in the parent
trees in Typer.
@Kordyjan Kordyjan added this to the 3.0.0 milestone Aug 2, 2023
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

Successfully merging a pull request may close this issue.

5 participants