jeudi 31 janvier 2019

Call a factory methods using Kotlin DSL

I want to be able to write Kotlin DSL that calls a factory method to create an instance of a object instead of directly calling the constructor.

I have a factory named, PersonFactory that implements a method called getPresonTypeOne(name: String, age:Int, address: String). PersonTypeOne has three member variables named name, age, and address. I want to be able to write Kotlin DSL that calls getPresonTypeOne to create an instance of PersonTypeOne instead of directly calling the PersonTypeOne constructor.

I ideally what something that looks like this

class PersonTypeOne(
      val name: String, 
      val age: Int, 
      val address: String) {
    ...
}


personTypeOne {
    withName {
       ...
    }
    withAge {
       ...
    }
    withAddress {
       ...
    } 
}

I would like that DSL to get effectively result in a call that looks like this : personFactory.getPresonTypeOne(name, age, address)

I have looked around quite a bit, but I have only found examples of me being able to do this by directly calling the PersonTypeOne constructor.

Aucun commentaire:

Enregistrer un commentaire