Unexpected tokens use ';' to separate expressions on the same line - error while creating an object in Kotlin

698 views Asked by At

I am new to kotlin. I was working on some design patterns used in kotlin. I came across abstract factory design and I keep getting this error while creating an object for the class. I tried the other solutions on stack overflow such as curly braces missing, but in vain. I've attached the entire code to resolve. Can someone help me how to resolve this? Thanks in advance.

The error in console:

Unexpected tokens (use ';' to separate expressions on the same line)

The main.kt code:

object AAbstractFactoryDesignPattern{

internal interface IAndroid{
    fun GetModelDetails(): String
}

internal interface IiOS{
    fun GetModelDetails(): String
}

internal interface IMobile{
    fun GetAndroidPhone() : IAndroid
    fun GetiOsPhone() : IiOS
}

internal class SamsungGalaxy : IAndroid{
    override fun GetModelDetails(): String {
        return "Model: Samsung Galaxy - RAM: 2GB - Camera: 13MP"
    }
}

internal class IphoneFour: IiOS{
    override fun GetModelDetails(): String {
        return "Model: Iphone 4 - RAM: 1GB - Camera: 12MP"
    }
}

internal class Samsung : IMobile{
    override fun GetAndroidPhone(): IAndroid {
        return  SamsungGalaxy()
    }

    override fun GetiOsPhone(): IiOS {
        return IphoneFour()
    }
}


internal class MobileClient(factory: IMobile){
    var androidPhone: IAndroid
    var iOSPhone: IiOS
    fun GetAndroidPhoneDetails(): String{
        return  androidPhone.GetModelDetails()
    }
    fun GetIOSPhoneDetails(): String{
        return  iOSPhone.GetModelDetails()
    }
    init {
        androidPhone = factory.GetAndroidPhone()
        iOSPhone = factory.GetiOsPhone()
    }

}


@JvmStatic
fun main(args : Array<String>){
    val samsungMobilePhone: Samsung() //error line
    val samsungClient: MobileClient(samsungMobilePhone) //error line
    println(samsungClient.GetAndroidPhoneDetails())
    println(samsungClient.GetIOSPhoneDetails())
}

}

1

There are 1 answers

1
dnault On

When declaring a variable, a colon (:) is for explicitly specify the variable's type. It looks like the colons on the error line should be equals (=) instead.

Try changing your "error lines" to this:

val samsungMobilePhone = Samsung() //error line
val samsungClient = MobileClient(samsungMobilePhone) //error line

Alternatively, if you want to be explicit about the type:

val samsungMobilePhone: IMobile = Samsung() //error line
val samsungClient: MobileClient = MobileClient(samsungMobilePhone) //error lines