Search code examples
scalasilhouette

why can't I pass companion object to a function


I am working on Silhouette framework and am creating an variable of type Environment. It's companion object has signature

def apply[E <: Env](
  identityServiceImpl: IdentityService[E#I],
  authenticatorServiceImpl: AuthenticatorService[E#A],
  requestProvidersImpl: Seq[RequestProvider],
  eventBusImpl: EventBus

For the authenticatorServiceImpl parameter, I thought that I could pass the companion object SessionAuthenticatorService (as defined in https://github.com/mohiva/play-silhouette/blob/master/silhouette/app/com/mohiva/play/silhouette/impl/authenticators/SessionAuthenticator.scala) but when I tried:

val sessionEnv = com.mohiva.play.silhouette.api.Environment[SessionEnv](new UserService(userRepository),SessionAuthenticatorService() ,CredentialsProvider(),EventBus())

I get the error

com.mohiva.play.silhouette.impl.authenticators.SessionAuthenticatorService.typ does not take parameters.

I removed the () but got another error:

found : SessionAuthenticatorService.type [error] required: AuthenticatorService[components.SessionEnv#A]

It seems my concepts are not sound. Why I cannot pass SessionAuthenticatorService?


Solution

  • A companion object is not an instance of the companion class. It is actually a different type.

    class ABC(val arg: Int)
    object ABC
    
    val abc: ABC      = new ABC(9)
    val xyz: ABC.type = ABC
    
    abc.arg  //res0: Int = 9
    xyz.arg  //Error: value arg is not a member of object A$A1661.this.ABC