Search code examples
scalaself-typetype-alias

Type aliases for outer object as private scope parameter


I have this situation

object SuperHorribleLongName {
   trait X {
       private[SuperHorribleLongName] def internalGaga() : Unit
   }
}

and I'm trying to get something like this working:

object SuperHorribleLongName {
   private type Sup = SuperHorribleLongName.type
   trait X {
       private[Sup] def internalGaga() : Unit
   }
}

but that just gives me "error: Sup is not an enclosing class"... I also tried type Sup = this.type, but still it doesn't work.

Anyways to achieve a nice shortcut for my outer object when using as private scope parameter? I do want to keep the long name for the object, and I have lots of private methods, that's why it gets really in my way.


Solution

  • I'm know sure it suits your hierarchy, but what about putting all you private methods in a

    private trait Y {
    

    Otherwise, you can always mimic a namespace :

    object SuperHorribleLongName {
    object SHLN { //Dummy alias
      trait X {
         private[SHLN] def internalGaga() : Unit
      }
    }
    type X = SHLN.X  //Lift into main object
    }
    

    It's not satisfying, since SHLN is visible, and turning it private prevents lifting X. And it's messy.
    So, let turn the problem inside/out :

    private object SHLN {
       trait X {
         private[SHLN] def internalGaga() : Unit
      }
    }
    
    //Expose the instance under wanted name
    val SuperHorribleLongName = SHLN