robinchew
10/25/2018, 7:25 PMfun <T>genattrs(vararg args: T): ArrayList<T> {
val arrays = ArrayList<T>()
arrays.addAll(args)
return arrays
}
val f = ::<Int>genattrs // invalid syntax
enleur
10/25/2018, 7:28 PMShawn
10/25/2018, 7:29 PMval f: (Array<out Int>) -> ArrayList<Int> = ::genattrs
robinchew
10/25/2018, 7:30 PMShawn
10/25/2018, 7:31 PMrobinchew
10/25/2018, 7:34 PMShawn
10/25/2018, 7:35 PMvararg params: T
is sugar for, Array<out T>
(vararg Int) -> ...
isn’t supportedrobinchew
10/25/2018, 7:36 PMShawn
10/25/2018, 7:37 PMrobinchew
10/25/2018, 7:37 PMShawn
10/25/2018, 7:37 PMrobinchew
10/25/2018, 7:38 PMShawn
10/25/2018, 7:38 PMrobinchew
10/25/2018, 7:42 PMShawn
10/25/2018, 7:42 PMrobinchew
10/25/2018, 7:43 PMval attrs: (Array<out Int>) -> ArrayList<Int> = ::genattrs
Is not the same as:
fun attrs(vararg args: Int): ArrayList<Int> {
val arrays = ArrayList<Int>()
arrays.addAll(args)
return arrays
}
attrs(1,2,3)
Shawn
10/25/2018, 7:46 PMVirtual...
to Int
you need a function likefun <T, R> genAttrs(vararg args: T): ArrayList<R>
robinchew
10/25/2018, 7:47 PMShawn
10/25/2018, 7:47 PMrobinchew
10/25/2018, 7:53 PMfun attrs(vararg args: Int): ArrayList<Int> {
val arrays = ArrayList<Int>()
arrays.addAll(args)
return arrays
}
apparently varargs args: Int
is a collection<Int>
type, which is not the same as ArrayList<Int>
.fun attrs(vararg args: Int): ArrayList<Int> {
val arrays = ArrayList<Int>()
arrays.addAll(args as ArrayList<Int>)
return arrays
}
Shawn
10/25/2018, 7:54 PMArray<Int>
, it’s IntArray
, which has a different inheritance hierarchyrobinchew
10/25/2018, 7:57 PM::genAttrs
problem. I guess this doesn't work?Shawn
10/25/2018, 7:58 PMrobinchew
10/25/2018, 8:00 PMarrayListOf(1,2,3)
but expecting 1 array argument instead, something like
NewArrayList(arrayListOf(1, 2, 3))
Shawn
10/25/2018, 8:02 PMrobinchew
10/25/2018, 8:03 PMarrayListOf
an alternate name, so I can just do a(1, 2, 3)
instead of arrayListOf(1,2,3)
Shawn
10/25/2018, 8:04 PMrobinchew
10/25/2018, 8:04 PMa = ::arrayListOf
dont workShawn
10/25/2018, 8:05 PMfun <T> a(vararg args: T): ArrayList<T> = arrayListOf(*args)
ArrayList
if you can help itList
if you can do with a read-only interface, MutableList
if you need those add/remove methodsrobinchew
10/25/2018, 8:08 PM*
wasn't supported, which is like ...
right?Shawn
10/25/2018, 8:08 PM...
means a couple different things depending on the language lol, but *
is definitely supported as the array spread operatorrobinchew
10/25/2018, 8:11 PM=
instead of {}
Shawn
10/25/2018, 8:12 PMrobinchew
10/25/2018, 8:13 PMShawn
10/25/2018, 8:15 PMrobinchew
10/25/2018, 8:17 PMShawn
10/25/2018, 8:17 PMrobinchew
10/25/2018, 8:18 PMdata class GlobalState(
val age: Int = 0,
val count: Int = 0,
val selectedIndex: String = "invalid-key")
When instantiating that I just want to do GS(1, 1, "key")
Shawn
10/25/2018, 8:20 PMtypealias
typealias GS = GlobalState
// elsewhere
val gs = GS(1, 1, "key")
robinchew
10/25/2018, 8:21 PMShawn
10/25/2018, 8:23 PMrobinchew
10/25/2018, 8:25 PMShawn
10/25/2018, 8:26 PMrobinchew
10/25/2018, 8:26 PMm(::LinearLayout,
attrs(
size(400, WRAP),
backgroundColorHex("#009999"),
elevation(1f),
margin(50)
),
children(
m(::TextView,
arrayListOf(
onCrup {movingView ->
value.animation.setView(movingView.parent as LinearLayout)
},
size(WRAP, MATCH),
weight(1f),
layoutGravity(CENTER_VERTICAL),
padding(5),
text("${value.name} ${selectedTop} ${value.checked}"),
backgroundColorHex("#000066"),
onTouch {v, motionEvent ->
val view = v.parent as LinearLayout
Shawn
10/25/2018, 10:06 PMrobinchew
10/26/2018, 11:16 AMShawn
10/26/2018, 1:49 PMm(LinearLayout) {
attrs {
size(400, WRAP)
backgroundColorHex ="#009999"
elevation = 1f
margin = 50
}
children {
+TextView {
onCrup { movingView ->
value.animation.setView(movingView.parent as LinearLayout)
}
size(WRAP, MATCH)
weight = 1f
layoutGravity = CENTER_VERTICAL
padding = 5
text = "${value.name} ${selectedTop} ${value.checked}"
backgroundColorHex = "#000066"
onTouch { v, motionEvent ->
view = v.parent as LinearLayout
}
}
}
}
robinchew
10/26/2018, 6:39 PM