Ashutosh Panda
11/05/2019, 2:39 AMjmfayard
11/05/2019, 10:43 AM.let { ... }
all the time and don't really understand the need for the others.
I know there are multiple articles on when to use
let, also, with, apply, run, takeIf, ...
But which one did you find the most useful?Exerosis
11/05/2019, 1:30 PMExerosis
11/05/2019, 1:33 PMinterface SomethingWithEvents {
fun Toggled.onSomething(listener: () -> (Unit))
}
To get called like this:
fun main(args: Array<String>) {
(args as Toggled).apply {
val something = args as SomethingWithEvents
something.onSomething {
println("Come on Kotlin for real m8")
}
}
}
which would ultimately make a java call like this:
something.onSomething(this, () -> {
});
Vishnu Haridas
11/05/2019, 2:24 PMlet
, also
, apply
etc. to execute some code when the receiver is null?
For example,
val response = getFromUrl(...)
response?.data?.user?.also {
... // welcome the user, save data, and lot more.
} ?: {
showError("Login failed")
} () // IIFE
Iaroslav Postovalov
11/05/2019, 5:13 PMRobert Jaros
11/05/2019, 7:17 PMKroppeb
11/05/2019, 11:39 PMr1
to r2
, but these are not equivalent. (The inferred type isn't even the same) Seems to be an issue if an extension function on a nullable type gives a generic type with the input type as one of the type parameters and as input a nullable type with a nullable parameterMani
11/06/2019, 10:06 AMval pickableState = try {
PickableState.valueOf(state)
} catch (ex: Exception){
null
}
is there a better way to check if a string is present in ENUM?jeggy
11/06/2019, 11:38 AMAndreyVanDenHaag
11/06/2019, 3:29 PMalex
11/06/2019, 11:07 PMClassCastException
and debugging shows that line no. 12
does nothing.
What might be the reason, that inline
version sometimes (for some calls) skips the cast and returns the same object (i.e. String
) instead of reified T
?
Classic approach with passing class ref works fine.Raymond Barlow
11/07/2019, 4:41 AMvineethraj49
11/07/2019, 6:08 AM!==
just translate to ! ==
which is !=
?Roberto Messina
11/07/2019, 9:23 AMOleh Ponomarenko
11/07/2019, 11:25 AMwhile ((inputLine = input.readLine()) != null) {
Log.d(TAG, inputLine);
}
Jakub Begera
11/07/2019, 11:47 AMFirst
be a Java class with a final function and Second
be a Kotlin class extending First
with a function of the same name like the final function in First
class, i. e.:
// Java class
class Firs {
final void foo() { }
}
// Kotlin class
class Second: First() {
fun foo() { }
}
Obviously, it’s wrong because the final function foo()
can not be overridden. However, compilation pass successfully and in run-time I get java.lang.LinkageError: Method void Second.foo() overrides final method in class First
.
Is this correct behavior of compiler? I supposed that there will be some validations for this case. Thank you! 🙂LeoColman
11/07/2019, 3:35 PMEllen Spertus
11/07/2019, 8:20 PMreportError()
to be called if the value preceding let
is `null`:
store.state.selectedTab?.content?.url.let {
doSomething(it)
} ?: run {
reportError("Unable to get URL to send")
}
Assume that both doSomething()
and reportError()
are void.Brian Norman
11/08/2019, 1:15 AMAntonio Aradaza
11/08/2019, 1:38 AMJohn
11/08/2019, 12:14 PMnimtiazm
11/08/2019, 8:41 PM""" …\n """.replace("\n", "")
but i’m looking for a better wayHullaballoonatic
11/09/2019, 5:01 AMByteArray
, IntArray
, CharArray
etc? why not just Array<Byte>
, Array<Int>
, Array<Char>
. I know that Java does treat primitive arrays differently from object arrays, but couldn't Kotlin do us a solid and smoosh them all into just Array<T>
?ventura
11/09/2019, 10:39 AMhellman
11/09/2019, 4:05 PMplugins {
id("org.jetbrains.kotlin.js") version "1.3.60-eap-76"
}
repositories {
mavenCentral()
mavenLocal()
maven(url = "<https://dl.bintray.com/kotlin/kotlin-eap>")
}
william
11/10/2019, 3:57 AMTravis Griggs
11/10/2019, 5:13 AMtypealias PlanOID = UInt
). Ideally, I’d like to bind a class/type/static function to PlanOID (e.g. PlanOID.nowOID()
), but I think this is not possible. I’m assuming that PlanOID is just what it says, a substitution alias, not a distinct type. And since UInt doesn’t have a Companion, I can’t extend that. Is this correct? Am I missing some other trick.
I get that I can just define a global free function, but I’d rather bind the behavior to the type if I could (avoids namespace clutter)Marcin Wisniowski
11/10/2019, 11:49 AMn
elements of a list are the same? Currently I have list.takeLast(n).toSet().size == 1
but I think this could be simpler.zceejkr
11/10/2019, 4:34 PMProjections are not allowed for immediate arguments of a supertype
.
I am new to generics so if anyone can point me in the right direction I would be thankful.zceejkr
11/10/2019, 4:34 PMProjections are not allowed for immediate arguments of a supertype
.
I am new to generics so if anyone can point me in the right direction I would be thankful.streetsofboston
11/10/2019, 4:38 PMT
(out on the declaration site): interface Bar<out T> { fun oneMethodThatReturnsT(): T }
?zceejkr
11/10/2019, 6:17 PMkarelpeeters
11/10/2019, 7:21 PM