ursus
11/11/2020, 1:06 AMfun User(...) : User? {
// if invalid input return null
return User(..)
}
data class User(...)
andylamax
11/11/2020, 1:56 AMinterface Human
private class Person(val name:String):Human
fun Human(name: String):Human = Person(name)
2. How ever if I have a class and all I want to check is validity, I mainly use companion invoke operator functions
data class User internal constructor(
val name: String,
val phone: String,
val email: String
) {
companion object {
operator fun invoke(name: String,phone:String): User { . . .}
operator fun invoke(name: String,email: String):User { . . .}
operator fun invoke(name: String,email:String,phone:String): User {. . .}
}
}
3. If I am building UI components/composables, I use Capital letter functions. It makes the code easy to follow through
// compose
@Compose
fun UserView(u:User) = Column {
Text(content = u.name)
Text(content = u.phone)
Text(content = u.email)
}
// kotlin react
fun RBuilser.UserView(u: User) = Flex {
Text(content = u.name)
Text(content = u.phone)
Text(content = u.email)
}
asad.awadia
11/11/2020, 2:04 AMNir
11/11/2020, 3:05 AMNir
11/11/2020, 3:06 AMNir
11/11/2020, 3:12 AMNir
11/11/2020, 3:14 AMArkadii Ivanov
11/11/2020, 10:23 AMursus
11/12/2020, 12:12 AM