Kotlin multiple constructor error: declaration have same java signature

540 Views Asked by At

I have two constructors in FirebaseUserAuth class and it is giving an Error: declaration have same java signature. The parameter of constructors are different. Why is it giving same signature error? how to fix it?.

class FirebaseUserAuth(private val auth: FirebaseAuth,
                       private val loginCallBack: ((LoginAuthData) -> Unit)?,
                       private val registerVM_CallBack: ((RegisterAuthData) -> Unit)?)
{

  constructor(auth: FirebaseAuth, loginCallBack: (LoginAuthData) -> Unit): this(auth, loginCallBack, null)
  constructor(auth: FirebaseAuth, registerCallBack: (RegisterAuthData) -> Unit): this(auth, null, registerCallBack)
}

Error

> Platform declaration clash:The following declarations have the same JVM
> signature (Lcom/google/fireBase/auth/FirebaseAuth;Lkotlin/jvm/
> functions/Function1;)V):
> 
>  • public constructor FirebaseUserAuth(auth: FirebaseAuth,
> loginCallBack: (LoginAuthData) -> Unit) defined.
> com.examgle.data.FirebaseUserAuth 
> 
>  • public constructor FirebaseUserAuth(auth: FirebaseAuth,
> registerCallBack: (RegisterAuthData) -> Unit) defined.
> com.examgle.data.FirebaseUserAuth

1

There are 1 best solutions below

3
On
constructor(auth: FirebaseAuth, loginCallBack: (LoginAuthData) -> Unit)
constructor(auth: FirebaseAuth, registerCallBack: (RegisterAuthData) -> Unit)

Both constructor are same as the type of the function parameter in JAVA is Lkotlin/jvm/functions/Function1.

Check this through:

  1. Menu > Tools > Kotlin > Show Kotlin Bytecode
  2. Click on the Decompile button
  3. Check the java code

My suggestion is to use default parameter rather than secondary constructor if possible:

class FirebaseUserAuth(private val auth: FirebaseAuth,
                       private val loginCallBack: ((LoginAuthData) -> Unit)? = null,
                       private val registerVM_CallBack: ((RegisterAuthData) -> Unit)? = null)
{
    ...
}