This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typealias UserAddressBuilderLambda = () -> UserAddress // 1 | |
class UserAddressBuilder( | |
addressInit: UserAddressBuilder.() -> Unit // 2 | |
) : UserAddressBuilderLambda { | |
init { | |
this.addressInit() // 3 | |
} | |
var userCity: String = "" | |
var userCountry: String = "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typealias UserAddressBuilderLambda = () -> UserAddress // 1 | |
class UserAddressBuilder( | |
addressInit: UserAddressBuilder.() -> Unit // 2 | |
) : () -> UserAddress { | |
init { | |
this.addressInit() // 3 | |
} | |
var userCity: String = "" | |
var userCountry: String = "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inline fun <reified T : User> UserBuilder.build(): T { | |
return when (T::class) { // 1 | |
UserImp::class -> { // 2 | |
UserImp( | |
address = userAddress, | |
id = userId, | |
name = userName | |
) as T | |
} | |
else -> object : User { // 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UserBuilder { | |
var userId: String = "" | |
var userName: String = "" | |
var userAddress: UserAddress = object : UserAddress { | |
override val city: String = "" | |
override val country: String = "" | |
} | |
fun userAddress(userAddressInit: UserAddressBuilder.() -> Unit) { | |
userAddress = UserAddressBuilder(userAddressInit).invoke() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inline fun <reified T : User> user(noinline creator: UserBuilder.() -> Unit): T { | |
return initClass(UserBuilder(), creator).build() | |
} | |
inline fun <T, R> initClass(tag: T, init: T.() -> R): T { | |
tag.init() | |
return tag | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface User { | |
val name: String | |
val id: String | |
val address: UserAddress | |
} | |
interface UserAddress { | |
val city: String | |
val country: String | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun <T> T.call(action: T.() -> Unit): T { | |
this.action() | |
return this | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SingleClass(val name: String) { | |
fun callSimpleFunction() = println("----> Single name is $name") | |
fun callFuncWithInput(input: String) = println("----> Single input = $input") | |
} | |
typealias SingleLiteral = SingleClass.(String) -> Unit | |
fun main(args: Array<String>) { | |
val singleLiteral: SingleLiteral = { inputString -> | |
this.callSimpleFunction() | |
this.callFuncWithInput(inputString) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AccountDelegate( | |
presenter: IAccountContract.IPresenter | |
) : BaseDelegate<IAccountContract.IView, IAccountContract.IPresenter>(presenter), | |
IAccountContract.IDelegate { | |
// 1 | |
override fun delegate() { | |
println("Account delegate is ready to work") | |
// 2 | |
with(view as AccountFragment) { | |
// 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainApplication : Application(), KodeinAware { | |
override val kodein: Kodein by Kodein.lazy { | |
bind<HomePresenter>() with singleton { HomePresenter() } | |
bind<AccountPresenter>() with singleton { AccountPresenter() } | |
bind<AccountDelegate>() with provider { AccountDelegate(instance()) } | |
bind<HomeDelegate>() with provider { HomeDelegate(instance()) } | |
} | |
} |