Chris Cordero
04/12/2020, 10:36 PMJehandad Kamal
04/13/2020, 6:41 AMUberunix
04/13/2020, 5:05 PMval myNewObject = NewObject(x,y,z)
Method 2:
val myNewObject: NewOject = NewObject(x,y,z)
Is there a practical reason that he declared the type of "myNewObject" in certain cases? It seems unnecessary.Enock Kalya
04/14/2020, 6:00 AMSanket Mehta
04/14/2020, 10:26 AMfun testWhenSyntaxDataProvider(): Array<Array<Any?>> {
return arrayOf(
arrayOf(null, null, 10),
arrayOf(1, null, 10),
arrayOf(null, 2, 20),
arrayOf(10, 20, 30)
)
}
The above function is throwing below exception:
Kotlin: Type inference failed. Expected type mismatch: inferred type is Array<Array<out Any?>> but Array<Array<Any?>> was expected
How can I typecast Int to Any?iseki
04/15/2020, 12:29 AMcrummy
04/15/2020, 10:53 AMPedro González
04/15/2020, 8:48 PMKeith
04/16/2020, 8:06 PMval alsoEmpty = listOf<String>()
alsoEmpty.add("funny face")
error: unresolved reference: add
alsoEmpty.add("funny face")
Ananiya
04/17/2020, 4:25 PMSlawomir Ziolkowski
04/18/2020, 11:18 AMfun main(args: Array<String>) {
val firstList = listOf<User>(
User(name="P-0"), User(name="P-0"), User(name="P-0"),
User(name="P-2"), User(name="P-2"), User(name="P-2"), User(name="P-2")
)
val secondList = listOf<User>(User(name="P-2"))
print(firstList.minus(secondList))
}
data class User(val name: String)
I expect that only one P-2 element will be not in the result, but the result looks like this:
[User(name=P-0), User(name=P-0), User(name=P-0)]Raymond Gao
04/18/2020, 5:44 PMval foo : Foo = Foo()
var jobj : JSONObject = serialize(foo)
var foo2 : Foo = construct(jobj, Foo::class)
The methods serialize and construct will inspect the object using reflection,
find any fields that match a set of constraints and add their values to a json object
or send them into the appropriate constructor of Foo
to create a new object
It works until I run into Generics
class Foo(val map : Map<String, Int>)
{
...
}
I'm having some difficulty getting the type parameters passed into Map
at runtime.
My friend has told me about the concept of type erasure. It seems that kotlin throws away
these arguments at runtime. I have resorted to annotating the field with the appropriate types
for now, but it causes some ugly duplication of information already typed down.
class Foo(@param:GenericsHint([String::class, Int::class]) val map : Map<String, Int>)
Does anyone have any ideas on how I can get around this problem?
Thanks 🙂fmcauley
04/19/2020, 4:55 PMChris Cordero
04/20/2020, 1:15 PMMadalin Valceleanu
04/21/2020, 7:53 AMiseki
04/21/2020, 4:14 PM(T)->Unit
`<T: Any`> but allow null when T is nullable type... How to do that? Thank youDaniel Lukic
04/22/2020, 12:01 PMAlexander Finn
04/22/2020, 4:29 PMinline fun <reified TDocument : Any> getCollection(
collectionName: String = KMongoUtil.defaultCollectionName(TDocument::class)
): CoroutineCollection<TDocument>
In my code I want to create an interface with a default method that would return a collection for specified type:
interface MongoDBRepository<T: Any> {
val client: CoroutineClient
fun collection(): CoroutineCollection<T> {
return client.getDatabase(accountId).getCollection<T>()
}
}
However, the compiler complains that 'T' can not be used as a reified type parameter here. And defining a reified type parameter would require me to change collection() to an inline function which is something I can not do in an interface. Any thoughts on how to change the code to make an interface functional? Or am I doing something completely wrong?Maciek
04/22/2020, 4:52 PMKenneth
04/23/2020, 8:52 AMAnaniya
04/23/2020, 11:57 AMclass SettingMyValue{ var setLength = 9 }
and from external kt file i put this code SettingMyValue().setLength = 7
to reassign the value of setLength
externally but it return 9 but all i want was to print the reassigned value (7) and if there is no external change returns 9Kenneth
04/23/2020, 2:28 PM!!
something that should be avoided?Gilberto Diaz
04/23/2020, 4:35 PMErik Dreyer
04/24/2020, 8:29 PMArray<Int?>
to Array<Int>
Gilberto Diaz
04/25/2020, 2:29 AMmapOf(…)
or mutableMapOf(…)
, the order in which elements are inserted are preserved. Is that a correct statement?Joshua
04/25/2020, 1:30 PMlayout
to androidx.drawerlayout.widget.DrawerLayout
and now I get the following error... AAPT: error: 'ferm.jonny.willow.models.ToolbarViewConfig' is incompatible with attribute type (attr) enum [linear=0, radial=1, sweep=2].
Any clue what this is about?keishi kubo
04/25/2020, 6:14 PMPK
04/26/2020, 5:37 AMChetan Sachdeva
04/27/2020, 1:13 PMAlex Wilson
04/28/2020, 4:08 PMfun compose(f: (Int) -> Int, g: (Int) -> Int): (Int) -> Int = { f(g(it)) }
I understand this example.
The next step was to make the compose function polymorphic by using type parameters.
I'm having a hard time making sense of this.
Since they are all the same type of Int, I thought it would be just this:
fun <T> compose23Example(f: (T) -> T, g: (T) -> T): (T) -> T = { f(g(it)) }
The solution however is this:
fun <T, U, V> compose(f: (U) -> V, g: (T) -> U): (T) -> V = { f(g(it)) }
I've been looking at it and am having difficulty understanding exactly what I'm looking at. Can anyone help?