tschuchort
05/16/2018, 7:59 PMAndreas Sinz
05/16/2018, 8:01 PMtschuchort
05/16/2018, 8:03 PM(T?) -> T
it wouldn't work, but I'm pretty sure that type is uninhabited.Seri
05/16/2018, 8:29 PMkarelpeeters
05/16/2018, 8:31 PMkarelpeeters
05/16/2018, 8:31 PMSeri
05/16/2018, 8:31 PMkarelpeeters
05/16/2018, 9:05 PMfun <T: Any> replaceNullWithDefault(value: T?, default: T):T = value ?: default
elect
05/17/2018, 8:24 AMprivate val <R> KProperty1<out Bufferizable, R>.size: Int
get() = when (returnType) {
Int::class.defaultType -> Int.BYTES // Ok
Array<Int>::class.defaultType -> 4 // Dummy, but it doent work
Array<Any?>::class.defaultType -> 5 // Neither, same with `out`
else -> throw Error(toString()) // Always throws
}
satejs
05/17/2018, 10:07 AMsatejs
05/17/2018, 10:07 AMiex
05/17/2018, 3:19 PMiex
05/17/2018, 3:21 PMenum MyEnum {
FOO, BAR
constructor(string): MyEnum? { when (string) "x" -> FOO, "y" -> BAR, else -> null }
}
iex
05/17/2018, 3:21 PMiex
05/17/2018, 3:21 PMdiesieben07
05/17/2018, 3:21 PMcompanion object
with a operator fun invoke
.iex
05/17/2018, 3:22 PMiex
05/17/2018, 3:24 PMenum MyEnum: String {
case foo = "foo"
case bar = "bar"
}
so the mapping is out of the box. Was wondering if there's a trick or something in Kotlin to get a similar behaviordiesieben07
05/17/2018, 3:25 PMenum class MyEnum(val id: String) {
foo("x"),
bar("y");
companion object {
val byId = values().associateBy { it.id }
}
}
val enum = MyEnum.byId["x"] // returns foo
diesieben07
05/17/2018, 3:26 PMid
and use it.name
if the name of the constant is enough (in this case "foo" and "bar")iex
05/17/2018, 4:14 PMiex
05/17/2018, 4:14 PMiex
05/17/2018, 8:02 PM@JvmOverloads
?iex
05/17/2018, 8:03 PMiex
05/17/2018, 8:03 PMinit
methodAndreas Sinz
05/17/2018, 8:03 PMiex
05/17/2018, 8:06 PMclass CustomView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
}
iex
05/17/2018, 8:06 PMinit { }
nowPaul Woitaschek
05/17/2018, 8:16 PMPaul Woitaschek
05/17/2018, 8:17 PM