0

Consider a class School. If we were to add a Pupil field to the school class is it possible to implicity pass a reference to the School object into the Pupil object.

So rather than doing

class School() {
   val pupil:Pupil = new Pupil(this)
}

We could do this

class School() {
   val pupil:Pupil = new Pupil()
}

And would still be able to access the school reference from the pupil object. I'm thinking scala implicits might help?

newlogic
  • 807
  • 8
  • 25
  • Perhaps you've scaled down the example so much that it is no longer representative, but are you sure this isn't a situation where explicit is better than implicit? – Joe Pallas Apr 12 '17 at 01:14

1 Answers1

1

For example,

object ImplicitConstructorParameter extends App {
  class Pupil(implicit val school: School)

  class School {
    implicit val school: School = this
    val pupil: Pupil = new Pupil
  }

  val school = new School

  println(school.pupil.school.eq(school))
}

prints true

Dedkov Vadim
  • 426
  • 5
  • 10