kqr
05/20/2022, 6:50 PMval x = if (y != null) y else {
<http://log.info|log.info>("bla")
z
}
vs
val x = y ?: run {
<http://log.info|log.info>("bla")
z
}
Tim
05/21/2022, 3:43 PMOkan Yıldırım
05/22/2022, 5:42 PMclass NotAuthenticatedError(
override val message: String,
) : Error(message = message)
I mean can’t I basically use the message immediately because it is extending?Okan Yıldırım
05/22/2022, 7:14 PM@PreAuthorize("hasRole('user')")
I would like to do
@UserOnly
ursus
05/23/2022, 2:43 PMtypealias BR = foo.bar.base.R
if I then use BR.
I don’t see string
, drawable
etc
I can only do
typeealias BR_string = foo.bar.base.R.string
typyealias BR_drawable = foo.bar.base.R.drawable
etc, which is stupidCanOfBees
05/23/2022, 5:26 PMval result = println("text")
println(result)
text
kotlin.Unit
One of the practice questions asks about the Magic Of println
and it implies that the value of result
is Unit
. While that's true, what's "text" doing in the output? Maybe this is something that I don't need to be worried about (and I'm not) but I'm curious why.
println is called twice, once for "text", and then a second time for println
itself, and the type value of the println
function is kotlin.Unit
? Is that true for all functions? Just println
?
Thanks for any clarity you're willing to share!Jordan Carlyon
05/24/2022, 3:41 PMvars
?Jonathan Ellis
05/24/2022, 7:04 PMJonathan Ellis
05/24/2022, 7:08 PMtipsy
05/24/2022, 7:40 PMAntonio Acuña Prieto
05/25/2022, 6:55 PMfun delete(id: UUID) = list.removeIf { it -> it.id == id }
And I want to:
• If the element was removed, don’t return nothing.
• If the element was not found, return exception
For now I have something like:
fun delete(id: UUID) = takeIf { list.removeIf { it -> it.id == id } } ?: throw NotFoundException(id)
But this still returns a true.zain
05/26/2022, 6:15 AM"Team's<>".contains("/[\"'\\/\\\\<>;|]/;")
CanOfBees
05/26/2022, 3:39 PMval scanner = Scanner(System.`in`)
val s1 = scanner.nextLine()
val s2 = scanner.nextLine()
val num = scanner.nextInt()
The answers are roughly:
a )
ABC DEF
123
b )
ABC
DEF 123
c)
10
ABC
3
d)
ABC DEF 123
Looking the behavior in kotlin file locally, the only answer that doesn't throw an error is a
, but not according to the course page. Is this a Kotlin version issue? A JVM issue? Apparently the correct answer is c
but the first Int is confusing me.
Any hints or clarity would be most appreciated! Thank you!Ankush
05/26/2022, 6:19 PMfor(a in As){
for(b in Bs){
for(c in Cs){
for(d in Ds){
}
}
}
}
Another train of thought I have is to use flatMap() to flatten the last loop and drop the loops from 4 to 3. But there too, am getting beaten up by Kotlin syntax.
Any help is appreciated! TIA 🙂
EDIT: Sturcture is of complex type in nature:
data class A(
val b: List<B>
)
data class B(
val c: List<C>
)
data class C (
val d: List<D>
)
data class D(
val string1: List<String>,
val string2: String
)
Karlo Lozovina
05/27/2022, 5:27 PMJoseph Burton
05/27/2022, 8:34 PMcontract
should prevent itsmit01
05/28/2022, 10:52 AMval invoke: suspend ()->Unit = fun(){}
Shows this error is it possible to declare suspending anonymous function with fun keyboard. I am also aware of suspending lambda that's not my question.
Thanks in advance.😇
error type mismatch: inferred type is () -> unit but suspend () -> unit was expected (line_27.kts1️⃣32)martmists
05/28/2022, 4:44 PMparens
rule if you check prop.name in getValue or provideDelegate, but is there a way the resolve() call could get access to this information somehow? I checked the stack trace and couldn't really find anything that could give this informationEllen Spertus
05/29/2022, 3:54 PMfun myfun: Boolean {
return foo?.stringProperty.isNullOrEmpty()
}
It seems to me that this would return null
, which is not Boolean
when foo
is null
. Why is the above code legal?Nick
05/29/2022, 7:19 PMstartDate
and an endDate
. I’d like to determine which events overlap so I can place events in a different column. Is there a way to apply some sort of groupBy
function to accomplish a way for me to group them?Mark
05/30/2022, 1:03 AM[0-9]
?
1️⃣ Int
2️⃣ UInt
3️⃣ Byte
4️⃣ Short
5️⃣ value class of Int
6️⃣ value class of UInt
7️⃣ value class of Byte
8️⃣ value class of Short
9️⃣ value class of Char
I suppose we already have the Kotlin answer, which is Char.digitToInt(): Int
Kirill Grouchnikov
05/30/2022, 1:07 AMMark
05/30/2022, 8:40 AMvar check1 = true
var check2 = false
val test = if (check1) {
null
} else if (check2) {
listOf("foo")
} else {
listOf("bar")
}.map { 2 }
println(test)
It seems we don’t need to use safe null on the map
call. However, if we remove the middle branch, then we do need it:
var check1 = true
var check2 = false
val test = if (check1) {
null
} else {
listOf("bar")
}?.map { 2 }
println(test)
In both cases (without the map
), the type of test
is List<String>?
Susmitha Gudapati
05/30/2022, 1:16 PM?
in states.
For example: My contract in ts is -
export interface A {
id: number,
name: string,
imageId?: number,
modified?: string,
}
In the interface A, the imageId and modified properties are optional. How do we denote the same functionality in kotlin’s classes?frogger
05/31/2022, 6:08 AMfun
in used in kotlin but issue no warning in java? We created a “bridge function” for java which should be avoided in kotlin code. I thought it would be nice to show a warning if used accidentally in kotlin code.Michael de Kaste
05/31/2022, 1:52 PMvalue class Example(val value: UInt){
init{
require(waarde <= 999_999_999u)
}
}
obviously this doesn't work, but a range check on numbers seems trivial when you actually call constructorselect
05/31/2022, 3:40 PMAyfri
06/01/2022, 10:51 AMAlejo
06/02/2022, 10:22 AMBoolean
variable from another one of type Boolean?
and if the value is null
then I will want to check if a value is present in another list and use that value for the initialization. Here is the code:
val statuses = listOf("pending", "on-hold", "auto-draft")
val status = "pending"
val isEditable: Boolean? = true
val result = isEditable ?: status in statuses
print(result)
But this initialization using the Elvis operator always returns false
, if I move the status in statuses
to a different variable or I wrap it in a run {status in statuses}
then the code works as expected.
Am I missing something here or this is a bug?
You can test the example here: https://pl.kotl.in/4QDgoNvYiEllen Spertus
06/02/2022, 8:55 PMEllen Spertus
06/02/2022, 8:55 PMRuckus
06/02/2022, 9:01 PMCameron Mallory
06/02/2022, 9:02 PMdata class Name(val first: String, val last: String){
fun getFullName(): String =
"$first $last"
}
If we run that in a Kotlin scratch file in Android Studio we get:Ruckus
06/02/2022, 9:03 PMEllen Spertus
06/02/2022, 9:04 PMCameron Mallory
06/02/2022, 9:05 PMEllen Spertus
06/02/2022, 9:11 PMCameron Mallory
06/02/2022, 9:13 PMEllen Spertus
06/02/2022, 9:15 PMCameron Mallory
06/02/2022, 9:18 PMFooExts.kt
file that all extensions can go into for easy discoverability IF we think they’d be useful “globally”. Otherwise private extensions in the same file where it’s used works for us.Matteo Mirk
07/04/2022, 3:30 PMRuckus
07/05/2022, 6:33 PMIf the extensions are defined in the same file, then what’s the point of using them?This article gives a good explanation as to the point.
Matteo Mirk
07/06/2022, 2:19 PM