Abstract Private Scala Fields

I was fortunate enough to discover that he did not allow to have abstract private fields in hell, i.e.

trait A1 {
    //private val a: Int         // Not allowed
    protected val b: Int         // OK
}

And, it seems, everything is right to do such a thing in an abstract class, if private fields are constructor parameters, i.e.

abstract class A2 (private val i: Int) // OK

So, I think that there are no constructor parameters in the dash, so there is no way to initialize them, so abstract private fields are not allowed.

If they are “protected,” then the subclass can initialize them using pre-initialized fields. This approach allows subclasses to see these fields.

What if I just want to initialize them and hide them later, as in the following example?

object holding {
    trait trick {
        protected val seed: Int                     // Can't be private
        final def magic: Int = seed + 123
    }

    trait new_trick extends trick {
        def new_magic: Int = magic + 456
        def the_seed: Int = seed                    // [1]
    }

    def play: new_trick = new { val seed = 1 } with new_trick 

    def show_seed(t: new_trick): Int = t.the_seed   // [2]
}

, - , [2] ( [1]) . ?


@Randall @pagoda_5b, . , , @Régis @axel22 .

+5
2

-, , , private, , . - , , . :

trait A {
  protected val foo: Bar
}

:

trait A {
  private val foo: Bar = initFoo 
  protected def initFoo: Bar
}

A val foo. foo initFoo, foo:

trait B extends A {
  protected def initFoo: Bar = ???
}

, initFoo - . , initFoo ( , factory), A, , - Bar ( , foo equals).

( , , seed fo type Int, , ), , - initFoo, ( -) . , , , . (. http://www.scala-lang.org/api/current/index.html#scala.concurrent.CanAwait).

trait A {
  // A "permit" to call fooInit. Only this instance can instantiate InitA
  abstract class InitA private[this]()
  // Unique "permit"
  private implicit def initA: InitA = null

  private def foo: Int = fooInit
  protected def fooInit( implicit init: InitA ): Int
}

trait B extends A {
  protected def fooInit( implicit init: InitA ): Int = 123
}

, B initFoo, , InitA ( A.initA A).

, , , axel22, , ( , - - , , ).

+8

, , - private[package_name]. , , .

+6

All Articles