Lukas K-G
11/16/2022, 8:56 PMYogeshvu
11/17/2022, 12:16 AMXad Kile
11/17/2022, 3:35 AMval myVar by lazy{ something() }
, but does not cache the value, but re-evaluates the value every time myVar
it is access? myVar
is a local variabley
11/17/2022, 7:17 AMsealed class
where children share some common variables. how do I do this with the least amount of boilerplate?
what I was recommended is something like
sealed class Parent {
abstract val s: String
data class Derived1(override val s: String, ...) : Parent()
data class Derived2(override val s: String, ...) : Parent()
sealed class Derived3() : Parent() {
data class Derived4(override val s: String, ...) : Derived3()
}
}
which repeats the val s
declarationRob Elliot
11/17/2022, 2:26 PMvalue class
expose all its value's members at the top level is there?Justin Tullgren
11/17/2022, 5:37 PMcom.company.internal.feature
?LIRAN Y
11/17/2022, 7:25 PMimport com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.module.kotlin.readValue
data class InsideList(
@JsonProperty("price") val price: String,
@JsonProperty("product") val product: String
)
data class NList(
@JsonProperty("listexample") val listexample: List<InsideList>?
)
the response
is the json list
val jsonResponse = MapperUtility.jsonMapper.readValue<NList>(response)
println("Response : {${jsonResponse.listexample}}")
the print is:
NList(listexample=[InsideList(product=apple, price=ten)])
how can i print just the value of the product?
i tried to do it with forEach
but not works as expected 🙄Lukasz Kalnik
11/18/2022, 10:21 AMm
in the old Java naming convention:
mSomeProperty
.
Can I configure it somewhere that Kotlin refactoring doesn't use (obsolete) Java naming conventions?Joshua Hansen
11/18/2022, 2:53 PMAyfri
11/18/2022, 11:06 PMwith
of my instance of Test2.
class Test(var name: String = "test")
class Test2 {
fun Test.method() = name
}
fun builder(block: Test.() -> Unit) = Test().apply(block)
fun main() {
val a = Test2()
builder {
with(a) {
println(method()) // can access method()
}
println(a.method()) // cannot access a.method() for some reason ?
}
}
Georg Ekeberg
11/19/2022, 2:54 PMabstract class GameState(i: Int, i1: Int) {
abstract val pinsLeft: Int
abstract val triesLeft: Int
}
fun roll(pinsDown: Int,
pinsLeft: Int = 10,
triesLeft: Int = 2): GameState {
return mapOf(Pair("pinsLeft", pinsLeft - pinsDown), Pair("triesLeft", --triesLeft)
}
Georg Ekeberg
11/19/2022, 2:56 PM@Test
fun `Roll returns how many pins are left when rolling 0 twice`() {
val state = roll(0)
assertEquals(0, roll(state["pinsLeft"], state["triesLeft"])
}
Georg Ekeberg
11/19/2022, 2:57 PMGeorg Ekeberg
11/19/2022, 3:04 PMdata class GameState(val i: Int, val i1: Int) {
private val pinsLeft = i
private val triesLeft = i1
operator fun get(s: String): Int {
when (s) {
"pinsLeft" -> return pinsLeft
"triesLeft" -> return triesLeft
}
}
}
xun su
11/20/2022, 1:15 AMtype Fn = (...args:any[]) -> any
but I don't know how to do it in Kotlin.Gordon
11/20/2022, 12:34 PMpublic val Icons.Filled.Circle: ImageVector
get() {
if (_circle != null) {
return _circle!!
}
_circle = materialIcon(name = "Filled.Circle") {
materialPath {
moveTo(12.0f, 2.0f)
curveTo(6.47f, 2.0f, 2.0f, 6.47f, 2.0f, 12.0f)
reflectiveCurveToRelative(4.47f, 10.0f, 10.0f, 10.0f)
reflectiveCurveToRelative(10.0f, -4.47f, 10.0f, -10.0f)
reflectiveCurveTo(17.53f, 2.0f, 12.0f, 2.0f)
close()
}
}
return _circle!!
}
private var _circle: ImageVector? = null
And i wonder why doesn't it just use lazy initialization?
As in something like this:
public val Icons.Filled.Circle: ImageVector by lazy {
materialIcon(name = "Filled.Circle") {
materialPath {
moveTo(12.0f, 2.0f)
curveTo(6.47f, 2.0f, 2.0f, 6.47f, 2.0f, 12.0f)
reflectiveCurveToRelative(4.47f, 10.0f, 10.0f, 10.0f)
reflectiveCurveToRelative(10.0f, -4.47f, 10.0f, -10.0f)
reflectiveCurveTo(17.53f, 2.0f, 12.0f, 2.0f)
close()
}
}
}
Is it just a style issue? Or the second approach has hidden performance cost?Diego DeSouza
11/20/2022, 3:04 PMsqldelight {
database("AppDatabase") {
packageName = "com.jetbrains.handson.kmm.shared.cache"
}
}
while using Android studio, I’m not able to create a package like that, but directory instead, that looks like this.
sqldelight {
database("AppDatabase") {
packageName = "com.example.ktorSQLDelight.shared.commonMain.sqldelight.shared.cache"
// packageName = "com.example.shared.cache"
}
}
i called this project ktorSQLDelight, i tried the above, but my AppDatabase is never “found” by the rest of the app.Diego DeSouza
11/20/2022, 3:07 PMRob Elliot
11/20/2022, 7:17 PMStefan Oltmann
11/21/2022, 11:29 AMxun su
11/21/2022, 1:50 PMString
a primitive type ? if so , why I can use lateinit
on it but not on Int
? the doc(https://kotlinlang.org/docs/properties.html#late-initialized-properties-and-variables) says
The type of the property or variable must be non-null, and it must not be a primitive type.I got a error like this
Xad Kile
11/22/2022, 8:34 AMhfhbd
11/22/2022, 10:20 AMCaused by: java.util.ServiceConfigurationError: Factory: Impl Unable to get public no-arg constructor
and NoSuchMethodException: Impl.<init>()
Ian Lynagh
11/22/2022, 1:25 PM@Suppress("DEPRECATION")
anywhere
$ cat Test1.kt
@file:Suppress("DEPRECATION")
import foo.Bar
$ cat Test2.kt
package foo
@Deprecated(message = "Bar is deprecated")
interface Bar
$ kotlinc -Werror Test1.kt Test2.kt
Adrian Amiel Sanchez
11/22/2022, 4:53 PMSuch classes are useful for one-time use
.
interface Person {
val name: String
val age: Int
}
Option 1
---
fun Person(name: String, age: String): Person = object : Person {
override val name = name
override val age = age
}
Option 2
---
fun Person(name: String, age: String): Person = PersonImpl(name, age)
private class PersonImpl(
override val name: String,
override val age: String
) : Person
1. What’s the pros/cons between the two?
2. Which is more preferred?
3. What does one-time use
mean? Does it mean we can’t pass instances of anonymous classes around?y
11/23/2022, 5:39 PMSam Stone
11/24/2022, 3:32 AMtheapache64
11/24/2022, 10:25 AMRobert Jaros
11/24/2022, 6:33 PMSen Ichi
11/24/2022, 9:02 PM