raulraja
10/24/2017, 8:07 PMReified
instance. For example:
package foo
interface Reified<A> {
val selfClass: KClass<A>
}
package bar
object ReifiedString: Reified<String> {
val selfClass: KClass<String> = String::class
}
import bar.ReifiedString
fooTC<String>() // compiles and returns String::class
fooTC<Int>() // Does not compile because the compiler can't find evidence of `Reified<Int>` imported in scope
ilya.gorbunov
10/24/2017, 8:24 PMraulraja
10/24/2017, 8:29 PMReified<Any>
if it wanted to get generic class info or any other shared behavior for any type potentially.Marc Knaup
10/24/2017, 8:30 PMclass Foo<A>
there is just a generic parameter A: Any?
. There’s no way the compiler can know what instance of Reified<A>
to pass to fooTC<A>()
since the compiler cannot know the argument for A
here.
@ilya.gorbunov I guess the various instances of Reified<A>
would be declared by the compiler automatically should it replace inline … reified
.raulraja
10/24/2017, 8:40 PMraulraja
10/24/2017, 8:40 PMA
means then it can lookup in the imported scope instances that satisfy the constrainMarc Knaup
10/24/2017, 8:44 PMFoo
from Java then the Kotlin compiler cannot know what A
means in `Foo`s constructor.
Same when you use reflection to create the instance:
ArrayList::class.java.newInstance()
.
No E
here.raulraja
10/24/2017, 8:49 PMclass Foo<A> given Reified<A> {
val aClass: KClass<A> = A.selfClass
}
import ReifiedString
Foo<Int>() //does not compile because no `Reified<Int>` is in scope
Foo<String>() //compiles because `Reified<String>` is in scope
raulraja
10/24/2017, 8:50 PMraulraja
10/24/2017, 8:52 PMMarc Knaup
10/24/2017, 9:04 PMReified
constraint.
Why do you import or declare ReifiedString
all the time? Isn’t the goal of Reified
to have that one declared implicitly by the compiler? 🙂raulraja
10/24/2017, 9:09 PMraulraja
10/24/2017, 9:10 PMReified
type class would be user defined unless the Kotlin team decided to put it in the stdlib, my point was just that if you are trying to get generic info for types you do know already then there is no need to have inline or reified modifiers should type classes get accepted in the lang.Marc Knaup
10/24/2017, 9:16 PMinline … reified
and passing the KClass
manually as a function argument.
Because I only have to declare a SomethingReified : Reified<Something>
only once it’s simpler than passing a KClass
on every function invocation.raulraja
10/24/2017, 9:20 PMraulraja
10/24/2017, 9:22 PMAsync
and provide instances for Deferred
, CompletableFuture
or whatever it can support it's behavior. then in your code you can code everything to Async
and apply at the edge the impl you want to useraulraja
10/24/2017, 9:24 PMraulraja
10/24/2017, 9:25 PMraulraja
10/24/2017, 9:27 PMraulraja
10/24/2017, 9:28 PMRepository
and inject at compile time in tests an instance that does not hit the DB whereas in prod you provide one that does.Marc Knaup
10/24/2017, 9:32 PMMarc Knaup
10/24/2017, 9:32 PMinterface JSONDecoderCodec<out Value : Any, in Context : JSONCoderContext> : JSONCodecProvider<Context> {
Working on that one right now.raulraja
10/24/2017, 9:41 PMraulraja
10/24/2017, 9:42 PMraulraja
10/24/2017, 9:42 PMfun <A> A.json(): String given JsonCodec<A> = A.asJSON()
raulraja
10/24/2017, 9:43 PMperson.json()
raulraja
10/24/2017, 9:44 PMJsonCodec<Person>
importedraulraja
10/24/2017, 9:45 PMMarc Knaup
10/24/2017, 9:49 PMperson.json()
is exactly what I do not want - having any kind of JSON encoding/decoding logic being part of the model (be it directly or through type classes). Better is decoder.decode<Person>()
to have a clear abstraction/separation.
But that’s just a minor change I guess.
Implementing these codecs is more interesting as they should be able to recursively encode/decode objects without knowing the actual codec being used. The codecs being used don’t even need to be public/known which is good because implementations should be hidden whenever possible.
This is already possible without reflection (except for isSubclassOf
and isSuperclassOf
) - just not for generic classes.
I’d have to spend some time to figure out if type classes give any benefit here.raulraja
10/24/2017, 9:57 PMfun <A> decode(a: A): String given JsonCodec<A> = a.json()
The actual codecs don't need to be known either, they can be imported:
import production.runtime.*
Marc Knaup
10/24/2017, 10:04 PMKClass
-style approaches.
At compile-time the consumer of a JSON encoder/decoder cannot know which codecs have been registered and which ones not (see GSON for example or MongoDB’s codec registry). So compile-time safety is only guaranteed if the consumer wires all the JSON codecs, isn’t it?raulraja
10/24/2017, 10:07 PMList<A>
it can't encode it unless there is also a codec for <A> and the compiler can verify that.raulraja
10/24/2017, 10:10 PMraulraja
10/24/2017, 10:10 PMMarc Knaup
10/24/2017, 10:11 PMJSONParser
instance through DI.
How can the compiler know which codecs are registered and which are not?Marc Knaup
10/24/2017, 10:12 PMraulraja
10/24/2017, 10:12 PMraulraja
10/24/2017, 10:13 PMraulraja
10/24/2017, 10:13 PMimport PersonCodec
is equivalent to register(PersonCodec)
Marc Knaup
10/24/2017, 10:13 PMMarc Knaup
10/24/2017, 10:14 PMMarc Knaup
10/24/2017, 10:14 PMraulraja
10/24/2017, 10:14 PMraulraja
10/24/2017, 10:16 PMMarc Knaup
10/24/2017, 10:17 PMinternal
codecs which they’ve registered.Marc Knaup
10/24/2017, 10:17 PMKClass
then yes, it’s needed.raulraja
10/24/2017, 10:18 PMraulraja
10/24/2017, 10:19 PMPerson -> Json
and you are referring to a Person it can traverse its dependency graph in the same way it does today with inline
and resolve all the constrainsraulraja
10/24/2017, 10:19 PMMarc Knaup
10/24/2017, 10:20 PMraulraja
10/24/2017, 10:21 PMgiven
as in the proposal in the KEEPraulraja
10/24/2017, 10:23 PMMarc Knaup
10/24/2017, 10:24 PMKClass
to use type classes and see if things work as intended or not.
It’s difficult to discuss that feature in theory 😮raulraja
10/24/2017, 10:26 PMraulraja
10/24/2017, 10:27 PMMarc Knaup
10/24/2017, 10:28 PMraulraja
10/24/2017, 10:34 PMraulraja
10/24/2017, 10:35 PMraulraja
10/24/2017, 10:36 PMMarc Knaup
10/24/2017, 10:42 PMraulraja
10/24/2017, 10:46 PMWildCardType
etc.. then you need to resolve before doing anything useful with those. I'm not proud of the code, I want to 🔥 it, therefore the KEEPraulraja
10/24/2017, 10:47 PMMarc Knaup
10/24/2017, 10:49 PM