3

Code Example:

import java.util.UUID

interface InterfaceOne<AType> {
    var abcOne:AType
}

interface InterfaceTwo<AType> {
    var abcTwo:AType
}

class Example<AType>: InterfaceOne<AType>, InterfaceTwo<AType> {

    override var abcOne: AType // Looking for default value to not from constructor 
    set(value) {
        field = value
        //...
    }

    override var abcTwo: AType // Looking for default value to not from constructor 
    set(value) {
        field = value
            //...
    }

   fun test(uuid: AType) {
       abcTwo = uuid
       abcOne = default // I'm looking for C#'s default keyword equivalent in here
   }
}

fun main() {

    val uuid = UUID.randomUUID()
    val uuid2 = UUID.randomUUID()

    val interfaceOne = Example<UUID>()

    interfaceOne.test(uuid)
}

You can use my playground for your testing! Click here.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
bkulaksiz
  • 77
  • 1
  • 9

1 Answers1

3

I believe that Kotlin doesn't have an equivalent feature. There is no default literal in Kotlin. Kotlin doesn't initialize class fields to any default, not even primitives (unlike Java, where, for example, an int is initialized to 0).

You either have to initialize your fields in the primary constructor, or at declaration, or make them nullable and leave them uninitialized (null).

Kotlin allows you to specify defaults for parameter values in the constructor, but that needs to be an instance of a concrete class, so that cannot take the generic into account.

You could use a factory method to generate a default value instead:

class Example<AType>(val factory: () -> AType): InterfaceOne<AType>, InterfaceTwo<AType> {

    override var abcOne: AType = factory()

    override var abcTwo: AType = factory()

    fun test(uuid: AType) {
        abcTwo = uuid
        abcOne = factory()
        println("abcOne=$abcOne")
        println("abcTwo=$abcTwo")
    }
}

fun main() {

    val uuid = UUID.randomUUID()
    val uuidExample = Example<UUID>({UUID.randomUUID()})
    uuidExample.test(uuid)

    val stringExample = Example<String>({"default"})
    stringExample.test("two")
}
Dario Seidl
  • 4,140
  • 1
  • 39
  • 55
  • Hello Dario! Thank you for reply my question! I already thought the primary constructor but If AType consider the "class type" or "object" or "primitive type" how should I make these object as default initialization? What would you suggest? If you have a time, Would you like to show me in the code? If you do that, I'll glad to you. Thanks! – bkulaksiz Apr 11 '20 at 20:41
  • 1
    One thing you could do, is to pass a function to your class that returns a "detault" value. Look at this [answer](https://stackoverflow.com/a/27000619/401712). I'll also edit my answer with some code. – Dario Seidl Apr 12 '20 at 01:23
  • Thank you for your code! I have another question. This implementation can be implement as a "Annotation"? For Instance there is a annotation called "DefaultInitializer" and it's target to "AnnotationTarget.CLASS" type, "AnnotationTarget.PROPERTY", "AnnotationTarget.FIELD" and "AnnotationTarget.TYPEALIAS" etc..Does it make sense? What's your opinion? – bkulaksiz Apr 12 '20 at 09:24
  • 1
    You're welcome. I don't think you could put the factory in an annotation. You could maybe put it in another Interface: `interface DefaultValueFactory {val factory: () -> AType}`. – Dario Seidl Apr 13 '20 at 14:11