kassim
01/03/2018, 1:32 PMval example = listOf()
won’t compile, I must write val example = listOf<Any>()
louiscad
01/03/2018, 1:33 PMkassim
01/03/2018, 1:35 PMkirillrakhman
01/03/2018, 1:35 PMdiesieben07
01/03/2018, 1:35 PMval list = listOf()
what type should the list have? It is not specified.diesieben07
01/03/2018, 1:35 PMkassim
01/03/2018, 1:36 PMAny
?diesieben07
01/03/2018, 1:36 PMString
or Foo
or Any?
? There is no reason for it to be any of these.diesieben07
01/03/2018, 1:37 PMlouiscad
01/03/2018, 1:38 PMNothing
? or Nothing?
? And what would happen when you would add something in the vararg? The type would magically change, breaking code?kassim
01/03/2018, 1:38 PMkassim
01/03/2018, 1:39 PMNothing?
but Any?
is something that can be assumeddiesieben07
01/03/2018, 1:39 PMkassim
01/03/2018, 1:40 PMkassim
01/03/2018, 1:40 PMkassim
01/03/2018, 1:41 PMdiesieben07
01/03/2018, 1:41 PMkassim
01/03/2018, 1:41 PMdiesieben07
01/03/2018, 1:41 PMlouiscad
01/03/2018, 1:41 PMlistOf()
vararg. It works in Java because Java allows raw generics, but Kotlin doesn't, so you need to specify the generic typelouiscad
01/03/2018, 1:42 PMgildor
01/03/2018, 1:42 PMdiesieben07
01/03/2018, 1:42 PMlouiscad
01/03/2018, 1:44 PMAny()
as argument of listOf(...)
for example, the type will be able to be inferred though.kassim
01/03/2018, 1:48 PMprotected <A extends BaseActivity> A getBaseActivity() {
return (A) getActivity();
}
I want to call this from my Kotlin code and call getBaseActivity().someMethodInBaseActivityClass()
but I have to write getBaseActivity<BaseActivity>().someMethodInBaseActivityClass()
I don’t care what the actual class is, the method is contained within the BaseActivity
class - but I’m still having to specify that, in my opinion, unnecessarilydiesieben07
01/03/2018, 1:48 PMdiesieben07
01/03/2018, 1:49 PMkassim
01/03/2018, 1:51 PMdiesieben07
01/03/2018, 1:55 PMkassim
01/03/2018, 2:01 PMdiesieben07
01/03/2018, 2:03 PMkassim
01/03/2018, 2:03 PMlouiscad
01/03/2018, 2:04 PMdiesieben07
01/03/2018, 2:04 PMBaseActivity
that will be returned.louiscad
01/03/2018, 2:05 PMBaseActivity
? I don't see such thing in this thread's context...diesieben07
01/03/2018, 2:05 PMlouiscad
01/03/2018, 2:06 PMlouiscad
01/03/2018, 2:08 PMkassim
01/03/2018, 2:09 PMlouiscad
01/03/2018, 2:14 PMlistOf()
defaulting to Any
[why not Any?
or Nothing
?]) would be made possible, which could introduce confusion and mistakes over the course of codebases lives.kassim
01/03/2018, 2:15 PMkassim
01/03/2018, 2:15 PMlistOf
examplekassim
01/03/2018, 2:18 PMpublic final <T extends View> T findViewById(@IdRes int id) {
if (id == NO_ID) {
return null;
}
return findViewTraversal(id);
}
if a user just wanted to disable a view, findViewById(ID).enabled = false
but findViewById(ID)<View>.enabled = false
is necessary
you can argue if you want that that’s “hidden magic”
anyways, thanks for responding, I guess the actual answer is “it hasn’t been addressed yet”louiscad
01/03/2018, 2:20 PMval
before, to do multiple operations (even if not touching generic part)? What would be the type of the val
? Yeah, type with a raw typed generic parameter, which is intentionally not allowed in Kotlin. I think it'd be theorically possible to support such use cases, but I personally don't think it's trivial to implement, and it could lead to increased compiler complexity (let alone IDE plugin) as well as hard to grasp edge cases such as the one I told youlouiscad
01/03/2018, 2:23 PMfindViewById
anymore, I use a homemade Anko-like View DSL that doesn't need to lookup the View hierarchy: https://github.com/Beepiz/BleGattCoroutines/blob/master/app/src/main/java/com/beepiz/blegattcoroutines/sample/MainActivity.kt#L24
Also, kotlin android extensions cache the views (more efficient than calling the lookup incurring findViewById
each time) and don't need you to specify the typekassim
01/03/2018, 2:27 PMlouiscad
01/03/2018, 2:27 PMkingsley
01/03/2018, 2:56 PMlouiscad
01/03/2018, 3:24 PMkassim
01/03/2018, 3:42 PM