Sylvain Patenaude
12/20/2019, 9:15 PMShawn
12/20/2019, 9:20 PMShawn
12/20/2019, 9:21 PMSylvain Patenaude
12/20/2019, 9:24 PMShawn
12/20/2019, 9:27 PMwhen
to smart-cast) but then you lose compile-time safety and kick a new can down to the consumer of the APISylvain Patenaude
12/20/2019, 9:28 PMDominaezzz
12/20/2019, 9:30 PMSylvain Patenaude
12/20/2019, 9:30 PMSylvain Patenaude
12/20/2019, 9:31 PMpt
12/20/2019, 9:33 PMwhen (param) {
is Int -> ...
is Float -> ...
}
Shawn
12/20/2019, 9:33 PMShawn
12/20/2019, 9:33 PMShawn
12/20/2019, 9:34 PMT : Number
, but the moment someone passes in a Double
by accident, this construct will kerplodept
12/20/2019, 9:35 PMpt
12/20/2019, 9:36 PMSylvain Patenaude
12/20/2019, 9:36 PMShawn
12/20/2019, 9:36 PMSylvain Patenaude
12/20/2019, 9:38 PMDico
12/20/2019, 9:43 PMSylvain Patenaude
12/20/2019, 9:46 PMobject
, which by default is sealed, isn't it?Shawn
12/20/2019, 9:47 PMobject
s are not sealed
by default, and cannot be inherited from ’cause they’re singletonsShawn
12/20/2019, 9:48 PMShawn
12/20/2019, 9:48 PMSylvain Patenaude
12/20/2019, 9:49 PMobject
?Shawn
12/20/2019, 9:50 PMShawn
12/20/2019, 9:51 PMSylvain Patenaude
12/20/2019, 9:51 PMShawn
12/20/2019, 9:51 PMShawn
12/20/2019, 9:52 PMSylvain Patenaude
12/20/2019, 9:52 PMZachary Grafton
12/20/2019, 9:54 PMZachary Grafton
12/20/2019, 9:55 PMSylvain Patenaude
12/20/2019, 9:56 PMZachary Grafton
12/20/2019, 9:58 PMf1
with a type other than Int or Float?Sylvain Patenaude
12/20/2019, 10:01 PMSylvain Patenaude
12/20/2019, 10:03 PMZachary Grafton
12/20/2019, 10:05 PMsealed class Value
class IntegerValue(val value: Int): Value()
class FloatValue(val value: Float): Value()
fun f1(param: Value) {
when(param) {
is IntegerValue -> { handleInteger(param) }
is FloatValue -> { handleFloat(param) }
}
}
pt
12/20/2019, 10:05 PMfun f(param: Int)
and fun f(param: Float)
, which call a private fun f2(param: Number)
and do runtime checking there. still far from perfect, but it prevents the consumer from using the API incorrectly. then the only person who has to understand type funkiness is the maintainer of those functionsZachary Grafton
12/20/2019, 10:05 PMpt
12/20/2019, 10:05 PMZachary Grafton
12/20/2019, 10:07 PMf1
like f1(IntegerValue(10))
or f1(FloatValue(10.5f))
and the compiler will throw an error if called any other way.Sylvain Patenaude
12/20/2019, 10:08 PMSylvain Patenaude
12/20/2019, 10:09 PMpt
12/20/2019, 10:09 PMZachary Grafton
12/20/2019, 10:09 PMSylvain Patenaude
12/20/2019, 10:11 PMZachary Grafton
12/20/2019, 10:11 PMZachary Grafton
12/20/2019, 10:13 PMSylvain Patenaude
12/20/2019, 10:16 PMSylvain Patenaude
12/20/2019, 10:19 PMDico
12/20/2019, 11:48 PMSylvain Patenaude
01/07/2020, 4:21 PM