Hans Ellegard
10/08/2020, 2:03 PMfun foo(dummy: Int?) {}
fun foo(dummy: String?) {}
// This causes a compilation error.
// You get
// reified.kt: (8, 5): None of the following functions can be called with the arguments supplied:
// public fun foo(dummy: Int?): Unit defined in root package in file reified.kt
// public fun foo(dummy: String?): Unit defined in root package in file reified.kt
inline fun <reified T> bar() {
// As the function is inlined and the type parameter reified, I'm hoping for static dispatch,
// i.e. depending on T, one of the overloads for foo() should be chosen.
foo(null as? T)
}
// This function has no problems compiling, static dispatch works as expected.
fun baz() {
foo(null as Int?)
foo(null as String?)
}
Why do I get a compilation error from bar
calling foo
? I guess it has something to do with Kotlin instantiating the bar
function even though it's supposed to be inlined yet never called (with an actual type parameter), but I haven't found the reason yet (I read https://kotlinlang.org/spec/runtime-type-information.html#runtime-available-types but found no explanation).Andrea Giuliano
10/08/2020, 3:38 PMVera van Mondfrans
10/09/2020, 7:33 AMfun getDomain(obj: Any) {
when(obj) {
is KrsProject -> obj.someMemberFunctionOnKrsProject()
}
}
Why would this work? Doesn’t the type get erased?Marc Knaup
10/09/2020, 9:06 PMBig Chungus
10/09/2020, 10:21 PMitnoles
10/09/2020, 10:35 PMSlackbot
10/10/2020, 3:45 AMmbonnin
10/10/2020, 9:07 AMfun listOfArrays(): List<Array<Any>> {
return listOf(arrayOf("1", 0))
}
But not this?
fun listOfArrays(): List<Array<Any>> {
// Type mismatch: inferred type is List<Array<out {Comparable<*> & java.io.Serializable}>> but List<Array<Any>> was expected
return listOf(arrayOf("1", 0)).map { it }
}
Marc Knaup
10/10/2020, 10:13 PMT?
in a place where you need T
?
Unfortunately there’s no T!
😄
Alternatively in a function where I want a non-nullable T
for reified T: Any?
.
Basically T & Any
.akuleshov7
10/10/2020, 10:36 PMrkeazor
10/12/2020, 12:43 AMLaw Gimenez
10/12/2020, 2:19 PMhuehnerlady
10/12/2020, 3:48 PMuser
10/12/2020, 4:18 PMaddamsson
10/12/2020, 8:16 PMChannel
looks like this when I Ctrl + Click on it:
public interface Channel<E> : kotlinx.coroutines.channels.SendChannel<E>, kotlinx.coroutines.channels.ReceiveChannel<E> {
public companion object Factory {
public const final val BUFFERED: <http://kotlin.Int|kotlin.Int> /* compiled code */
internal final val CHANNEL_DEFAULT_CAPACITY: <http://kotlin.Int|kotlin.Int> /* compiled code */
public const final val CONFLATED: <http://kotlin.Int|kotlin.Int> /* compiled code */
public const final val DEFAULT_BUFFER_PROPERTY_NAME: kotlin.String /* compiled code */
internal const final val OPTIONAL_CHANNEL: <http://kotlin.Int|kotlin.Int> /* compiled code */
public const final val RENDEZVOUS: <http://kotlin.Int|kotlin.Int> /* compiled code */
public const final val UNLIMITED: <http://kotlin.Int|kotlin.Int> /* compiled code */
}
}
What can I do to fix this and see the source (IDEA says that this file is called Channel.kotlin_metadata
)?Akasha Peppermint
10/13/2020, 5:26 AMMichael de Kaste
10/13/2020, 1:43 PMdaphillips
10/13/2020, 3:22 PMWesley Acheson
10/13/2020, 4:26 PMSlackbot
10/13/2020, 5:46 PMDaniel
10/13/2020, 7:30 PMprivate val _state = MutableStateFlow<Foo>(Foo())
val state = StateFlow<Foo> get() = _state
and
private val _state = MutableStateFlow<Foo>(Foo())
val state = StateFlow<Foo> = _state
?
Jetbrains and Google use the first form in their documentation, but isn't a val
read-only regardless of whether it is specified as only having a getter or not?Daniel
10/13/2020, 8:18 PMsynchronize
, or how to understand how it works. Can anyone help me figure out what this actually does? (I figured out it was an internal fn from the warning I got from Android Studio)
// Annotates class to be a Room Database with a table (entity) of the Word class
@Database(entities = arrayOf(Word::class), version = 1, exportSchema = false)
public abstract class WordRoomDatabase : RoomDatabase() {
abstract fun wordDao(): WordDao
companion object {
// Singleton prevents multiple instances of database opening at the
// same time.
@Volatile
private var INSTANCE: WordRoomDatabase? = null
fun getDatabase(context: Context): WordRoomDatabase {
val tempInstance = INSTANCE
if (tempInstance != null) {
return tempInstance
}
synchronized(this) {
val instance = Room.databaseBuilder(
context.applicationContext,
WordRoomDatabase::class.java,
"word_database"
).build()
INSTANCE = instance
return instance
}
}
}
}
Is all this necessary? How does this differ from the more normal way I've seen people write something to cache a return value, which doesn't use @Volatile
or synchronized
? Something like
private var _cache: WordRoomDatabase? = null
fun getDatabase(context: Context): WordRoomDatabase {
val cached = _cache // also, why is this necessary actually?
if (cached != null) {
return cached
} else {
_cache = Room.databaseBuilder(
context.applicationContext,
WordRoomDatabase::class.java,
"word_database"
).build()
return cache
}
}
Corey Lanier
10/14/2020, 4:50 AMcopy
Rob Elliot
10/14/2020, 10:33 AMval x: Int? = null
val xs: Set<Int> = setOf(1, 2, 3)
xs.contains(x) // compiles, but why?
because the type of contains
is public operator fun <@kotlin.internal.OnlyInputTypes T> Iterable<T>.contains(element: T): Boolean
.
Does @kotlin.internal.OnlyInputTypes
mean that `T`is defined by the type of element
not of the receiver? I guess that would type check, as Int
is a subtype of Int?
- right?Gabriel
10/14/2020, 2:14 PMSlackbot
10/14/2020, 2:54 PMaddamsson
10/14/2020, 7:39 PMRyan Pierce
10/15/2020, 1:19 AMonItemClick
actually get inlined? I inlined it with the intention to improve performance, but I haven't seen inline
used in a constructor before.
class MyAdapter(private inline val onItemClick: (Item) -> Unit) {
//...
}
Mark Buikema
10/15/2020, 9:25 AMron
10/15/2020, 12:17 PMron
10/15/2020, 12:17 PMAlejandro Rios
10/15/2020, 12:19 PMron
10/15/2020, 2:39 PM