https://kotlinlang.org logo
Title
r

robinchew

10/25/2018, 9:13 AM
Hello I have an array of functions that accepts a View argument:
val manyFunctions: ArrayList<(View) -> Unit> = arrayListOf(
    {layout: TextView -> Unit}
)
But that don't work cos
TextView
is not exactly
View
though it subclasses it.
d

Dominaezzz

10/25/2018, 9:20 AM
Change
View
to
out View
or
in View
. I don't remember which will work but one should.
d

diesieben07

10/25/2018, 9:21 AM
Yes, your function does not accept
View
, it only accepts
TextView
. How do you intend to even call these functions? You can't. @Dominaezzz That does not work in a function type (it's not really useful, the parameters are always
in
, the return type is always
out
).
d

Dominaezzz

10/25/2018, 9:23 AM
Oh true.
r

robinchew

10/25/2018, 9:27 AM
Why can't you call them? Functions are meant to be called.
d

Dominaezzz

10/25/2018, 9:27 AM
What argument would you pass?
r

robinchew

10/25/2018, 9:28 AM
An instance of a View type
d

diesieben07

10/25/2018, 9:28 AM
If you had a (theoretical)
{ foo: out Thing -> Unit }
there is no way to call it, since you cannot know what it's actual parameter is.
r

robinchew

10/25/2018, 9:29 AM
Ok I think I'm back to the same discussion I had with Shawn yesterday
d

Dominaezzz

10/25/2018, 9:29 AM
If you pass on anything that isn't a
TextView
, you'll get a ClassCastException or similar. Given it compiles.
d

diesieben07

10/25/2018, 9:30 AM
It will only compile with an "unchecked cast" warning
r

robinchew

10/25/2018, 9:31 AM
Thanks guys for the clues, I think I know where to go.
I hit a wall again, so instead of doing:
ArrayList<(View) -> Unit>
I tried to do:
ArrayList<((Context) -> View) -> Unit>
But that's a syntax error
I basically substituted
View
for
(Context) -> View
.
oh wait, it works? must be bracket problems
nevermind it doesn't make sense anyway