Nir
07/10/2019, 2:33 PMinline fun <reified T> foo() : Int {
return T.bar
}
Basically, a way to access values, functions, etc, from the type itself, instead of from an instance of that type.karelpeeters
07/10/2019, 2:34 PMNir
07/10/2019, 2:36 PMdalexander
07/10/2019, 2:36 PMstatic
in Java) make sure you read about companion objects, if you haven't already?Ruckus
07/10/2019, 2:37 PMNir
07/10/2019, 2:38 PMdalexander
07/10/2019, 2:38 PMkarelpeeters
07/10/2019, 2:38 PMinterface X { val x: Int }
class Foo {
companion object : X {
override val x = 5
}
}
fun X.print() = println(x)`
Use like this:
Foo.print()
Shawn
07/10/2019, 2:38 PMNir
07/10/2019, 2:39 PMx
🙂dalexander
07/10/2019, 2:39 PMkarelpeeters
07/10/2019, 2:39 PMdalexander
07/10/2019, 2:40 PMNir
07/10/2019, 2:40 PMtemplate <class T, class Period>
class Duration;
karelpeeters
07/10/2019, 2:40 PMNir
07/10/2019, 2:41 PMT t;
. If you want to get the duration in seconds, you would do t * Period::ratio;
inline fun <reified T> foo() : Int {
return T.bar
}
Is that the function can extract constants from a type without receiving an instance of that type. Since the type is reified, this is clearly possible in principle, the question is just whether Kotlin supports it currently.dalexander
07/10/2019, 2:45 PMNir
07/10/2019, 2:45 PMShawn
07/10/2019, 2:46 PMdalexander
07/10/2019, 2:46 PMTime
type alias of some variety that used a long for a backing value, then... hm. It should be relatively easy to store the ratios in associated objects as constants.Shawn
07/10/2019, 2:46 PMdalexander
07/10/2019, 2:46 PMinline class Seconds(val backing: Long) {
fun toMilliseconds(): Milliseconds = Milliseconds(backing * 1000)
fun toMinutes(): Seconds = Seconds(backing / 60)
...
}
inline class Seconds(val backing: Long) { ... }
Something like this should be possible I think? Inline classes are currently experimental however. That should get a nice interface to work with while also not introducing any performance issues.Marc Knaup
07/10/2019, 3:05 PMNir
07/10/2019, 3:08 PMHanno
07/10/2019, 3:09 PMNir
07/10/2019, 3:09 PMMarc Knaup
07/10/2019, 3:09 PMNir
07/10/2019, 3:09 PMHanno
07/10/2019, 3:10 PMNir
07/10/2019, 3:10 PMinline
is that unless you use it throughout it doesn't solve the problem.Marc Knaup
07/10/2019, 3:11 PMNir
07/10/2019, 3:12 PMMarc Knaup
07/10/2019, 3:12 PMNir
07/10/2019, 3:13 PMMarc Knaup
07/10/2019, 3:13 PMNir
07/10/2019, 3:13 PMMarc Knaup
07/10/2019, 3:14 PMNir
07/10/2019, 3:14 PMMarc Knaup
07/10/2019, 3:15 PMNir
07/10/2019, 3:16 PMMarc Knaup
07/10/2019, 3:16 PMNir
07/10/2019, 3:16 PMStores value as Long seconds + Int nanoseconds
Marc Knaup
07/10/2019, 3:18 PMHanno
07/10/2019, 3:19 PMShawn
07/10/2019, 3:21 PMreified
definitely changes things by getting reflection involvedMarc Knaup
07/10/2019, 3:21 PMShawn
07/10/2019, 3:22 PMMarc Knaup
07/10/2019, 3:22 PMShawn
07/10/2019, 3:23 PMMarc Knaup
07/10/2019, 3:24 PMNir
07/10/2019, 3:24 PMMarc Knaup
07/10/2019, 3:24 PMNir
07/10/2019, 3:25 PMMarc Knaup
07/10/2019, 3:25 PMNir
07/10/2019, 3:25 PMMarc Knaup
07/10/2019, 3:25 PMNir
07/10/2019, 3:26 PMMarc Knaup
07/10/2019, 3:26 PMNir
07/10/2019, 3:27 PMMarc Knaup
07/10/2019, 3:27 PMNir
07/10/2019, 3:30 PMMarc Knaup
07/10/2019, 3:30 PMNir
07/10/2019, 3:30 PMMarc Knaup
07/10/2019, 3:30 PM