robinchew
10/24/2018, 8:55 PMTextView
instance with something like:
render(TextView::class, hashOf("text", "abc123"))
Shawn
10/24/2018, 8:56 PMrobinchew
10/24/2018, 8:57 PMrender
function look like so that the class from the first argument can be instantiated?Shawn
10/24/2018, 8:59 PMfun <reified T : Any> render(params: Map<String, String>): T? {
return T::class.primaryConstructor?.call(params)
}
// called as such
val view = render<TextView>(mapOf("text" to "abc123"))
// or with type inferencing
val view: TextView? = render(mapOf("text" to "abc123"))
robinchew
10/24/2018, 9:00 PMShawn
10/24/2018, 9:00 PMrobinchew
10/24/2018, 9:02 PMShawn
10/24/2018, 9:03 PMShawn
10/24/2018, 9:03 PMKClass<T>
is a member of kotlin.reflect
robinchew
10/24/2018, 9:05 PMdata class Virtual(val cls: KClass<out View>, val attr: Map<String, Any>)
fun realiseView(activity: Context, parent: ViewGroup, virtual: Virtual): View {
val view = virtual.cls(activity)
parent.addView(view)
return view
}
realiseView(activity, vg, Virtual(TextView::class, hashOf("text" to "abc")));
I know you advised against KClassrobinchew
10/24/2018, 9:08 PMrobinchew
10/24/2018, 9:08 PMShawn
10/24/2018, 9:09 PMKClass
instances around, the problem is with disparate constructor signaturesShawn
10/24/2018, 9:09 PMView
objects have the exact same constructor in which case I guess it’s nbdrobinchew
10/24/2018, 9:10 PMShawn
10/24/2018, 9:10 PMVirtual
data class, but maybe you’re using it elsewhereShawn
10/24/2018, 9:10 PMrobinchew
10/24/2018, 9:10 PMShawn
10/24/2018, 9:11 PMShawn
10/24/2018, 9:12 PMrobinchew
10/24/2018, 9:12 PMvirtual.cls.primaryConstructor?
Shawn
10/24/2018, 9:12 PMrobinchew
10/24/2018, 9:13 PMvirtual.cls
then?robinchew
10/24/2018, 9:13 PMShawn
10/24/2018, 9:14 PMvirtual.cls.primaryConstructor
, which will be of type KFunction<{class type}>?
Shawn
10/24/2018, 9:15 PMKFunction<T>
type defines invoke
, and instead provides .call(Any)
robinchew
10/24/2018, 9:15 PM.primaryConstructor
don't existrobinchew
10/24/2018, 9:16 PM.constructors
though, but I gotta loop itrobinchew
10/24/2018, 9:16 PMKClass
, then TextView:class
will complainShawn
10/24/2018, 9:17 PMjava.lang.List
and now my function that needs List
is complaining”Shawn
10/24/2018, 9:18 PMKClass
itself, I’m just trying to warn against this kind of arbitrary factory methodShawn
10/24/2018, 9:18 PMShawn
10/24/2018, 9:18 PMShawn
10/24/2018, 9:18 PMShawn
10/24/2018, 9:18 PMrobinchew
10/24/2018, 9:19 PMrobinchew
10/24/2018, 9:19 PM.primaryConstructor
though. Don't existShawn
10/24/2018, 9:20 PM.primaryConstructor
looks like an extension val, check to see if you have import kotlin.reflect.full.primaryConstructor
at the top of the fileShawn
10/24/2018, 9:20 PMrobinchew
10/24/2018, 9:21 PMunresolved reference full
Shawn
10/24/2018, 9:22 PMShawn
10/24/2018, 9:22 PMrobinchew
10/24/2018, 9:23 PMShawn
10/24/2018, 9:25 PMrobinchew
10/24/2018, 9:25 PMShawn
10/24/2018, 9:27 PMShawn
10/24/2018, 9:27 PMTextView(params)
robinchew
10/24/2018, 9:27 PMTextView
is kept inside the Virtual
data classrobinchew
10/24/2018, 9:28 PMvirtual.cls(params)
or equivalent to workShawn
10/24/2018, 9:28 PMmyFunc
Shawn
10/24/2018, 9:28 PMVirtual
data class anyhowrobinchew
10/24/2018, 9:28 PMShawn
10/24/2018, 9:29 PMShawn
10/24/2018, 9:29 PMShawn
10/24/2018, 9:30 PMrobinchew
10/24/2018, 9:30 PMrobinchew
10/24/2018, 9:30 PMShawn
10/24/2018, 9:31 PMShawn
10/24/2018, 9:31 PMShawn
10/24/2018, 9:31 PMval myBuilder = { MyClass(params) }
robinchew
10/24/2018, 9:32 PMShawn
10/24/2018, 9:32 PMShawn
10/24/2018, 9:32 PMval view = myBuilder()
robinchew
10/24/2018, 9:35 PMval boo = TextView
boo(activity);
Shawn
10/24/2018, 9:35 PMval foo = TextView
in Kotlin doesn’t get you a type-like reference to a given classrobinchew
10/24/2018, 9:36 PMval boo = TextView::class;
boo(activity);
Which complains about not having invoke methodShawn
10/24/2018, 9:36 PMTextView.Companion
or result in a compile error if there is no companion object defined on TextView (which there probably isn’t)robinchew
10/24/2018, 9:37 PMrobinchew
10/24/2018, 9:37 PMShawn
10/24/2018, 9:37 PMTextView::class
doesn’t let you use the reference like you would like if you were just calling a constructor by handShawn
10/24/2018, 9:37 PMval foo = ::TextView
Shawn
10/24/2018, 9:38 PMBar != KClass<Bar>
robinchew
10/24/2018, 9:39 PMval boo = ::TextView
boo(activity)
robinchew
10/24/2018, 9:39 PMShawn
10/24/2018, 9:39 PMShawn
10/24/2018, 9:40 PMShawn
10/24/2018, 9:40 PMShawn
10/24/2018, 9:40 PMrobinchew
10/24/2018, 9:41 PMShawn
10/24/2018, 9:41 PMrobinchew
10/24/2018, 9:42 PMrobinchew
10/24/2018, 9:42 PMTextView(activity)
robinchew
10/24/2018, 9:42 PMShawn
10/24/2018, 9:42 PMShawn
10/24/2018, 9:43 PMShawn
10/24/2018, 9:43 PMval foo = ::TextView
Shawn
10/24/2018, 9:43 PMrobinchew
10/24/2018, 9:43 PMrobinchew
10/24/2018, 9:43 PMShawn
10/24/2018, 9:44 PMrobinchew
10/24/2018, 9:44 PMrobinchew
10/24/2018, 9:44 PMShawn
10/24/2018, 9:44 PMFoo(params)
will resolve which constructor you need at compile-timeShawn
10/24/2018, 9:44 PMShawn
10/24/2018, 9:45 PMval ctor: (Context) -> TextView = ::TextView
robinchew
10/24/2018, 9:46 PMrobinchew
10/24/2018, 9:48 PMShawn
10/24/2018, 9:48 PMrobinchew
10/24/2018, 9:48 PMShawn
10/24/2018, 9:49 PMrobinchew
10/24/2018, 9:49 PM() ->
is reserved for functionsrobinchew
10/24/2018, 9:49 PMrobinchew
10/24/2018, 9:49 PMShawn
10/24/2018, 9:49 PMT
is a function that returns T
, no?robinchew
10/24/2018, 9:50 PM::TextView
is the constructor and not the class.Shawn
10/24/2018, 9:50 PMrobinchew
10/24/2018, 9:51 PMrobinchew
10/24/2018, 9:51 PMShawn
10/24/2018, 9:52 PMShawn
10/24/2018, 9:52 PMShawn
10/24/2018, 9:53 PMrobinchew
10/24/2018, 9:54 PM