y
04/15/2024, 6:18 AMinterface Foo {
/* ... */
}
interface Bar {
val thing: Thing
/* ... */
}
fun getFooByName(s: String): Foo? = /* ... */
// no getBarByName
// ... and a bunch of classes that implement some combination of Foo and Bar
would you add a fun asBar(): Bar?
or even fun getThing(): Thing?
for Foo
, or is that silly from a coupling/API design point of view?CLOVIS
04/15/2024, 8:01 AMGamar Mustafa
04/15/2024, 12:45 PMdave08
04/15/2024, 12:56 PMabstract class KomapperBaseRepository<ENTITY : Any, ID : Any, META : EntityMetamodel<ENTITY, ID, META>>(
private val db: R2dbcDatabase,
private val entityMetamodel: META,
) {
suspend fun create(entity: ENTITY): ENTITY = db.runQuery(QueryDsl.insert(entityMetamodel).single(entity))
}
how do I avoid having to provide all three type params when deriving from this? Doesn't the compiler infer them when I provide the entityMetamodel in the constructor?Elizeu Silva dos Santos
04/15/2024, 4:03 PMMahvi
04/16/2024, 6:52 AMDavid Kubecka
04/16/2024, 11:46 AMfun List<String>.sort(order: String) {
val sortFun = if (order == "asc") List<String>::sortedBy else List<String>::sortedByDescending
}
I can't figure out the right typing for sortFun
Ray Rahke
04/17/2024, 2:41 AMwhen piece is {
Pawn -> {}
Bishop -> {}
...
}
as opposed to this
if (piece is Pawn) {}
else if (piece is Bishop) {}
Bernhard
04/17/2024, 10:25 AMinternal data class EnumAttr(
override var key: String,
override var label: String,
): AttributePlainEnumValue
is there no support for getter/setter to data class property mapping?Bernhard
04/17/2024, 2:16 PMMaksym M.
04/17/2024, 4:52 PMMap<Int, String?>
, I have a check in place that verifies all map values are not null, map.all { it.value != null }
and only if this check is true I pass this map down into a function.
The problem is my function accepts only non nullable maps of type Map<Int, String>, I don't like unchecked casts or !! bangs because I know for a fact this code supposed to work without them.
I decided to fix this using Kotlin Contracts but it seems like it's not possible to make a contract like
contract {
returns(true) implies(values is Map<Int, String>)
}
due to type erasure.
Is there anything I have overlooked? What's the idiomatic way to solve this?Joel Denke
04/17/2024, 6:11 PMTom Huynh
04/17/2024, 7:38 PMZyle Moore
04/17/2024, 9:28 PMvalue class NonEmptyString(private val raw: String) {
init {
require(raw.isNotEmpty())
}
}
value class Digits(private val raw: NonEmptyString) {
init {
require(raw.all(Char::isDigit))
}
}
val nes = NonEmptyString("asdf")
val digits = Digits("123")// doesn't work, but I want it to
val nesDigits = Digits(NonEmptyString("123"))// works, but feels clunky
Ray Rahke
04/18/2024, 6:40 AMgradle init
. All good, except I dislike the default directory structure. I don't want src/main/kotlin/ whatever. I just want src/main.kt.
I've forgotten how to do that. And I don't know how to quickly find the answer.
This seems like a real problem with gradle, that it is so dependent on memory.
I really wish kotlin had it's own build tool. like swift.André Martins
04/18/2024, 1:28 PMval startTime = System.currentTimeMillis()
try {
<some code>
} finally {
val duration = Duration.ofMillis(System.currentTimeMillis() - startTime)
}
Thanks in advance ✌️Travis Griggs
04/18/2024, 8:24 PMvar insideBox: SpanBoxInfo? = null
...
for (spanBoxInfo in spanBoxes(valveSpanList, yAxis, size)) {
val (_, _, spanRect ) = spanBoxInfo
if (spanRect.contains(offset)) {
insideBox = spanBoxInfo
} else if ...
}
return if (insideBox != null) {
"inside" to insideBox
} else if ...
I have a preference for .forEach
and wanted to apply that here. But when I do, the smart null pointer casting doesn't work at the bottom anymore. Why is that?Bruno Santos
04/19/2024, 12:39 PMMark
04/20/2024, 5:23 AMNothing?
override val foo: Nothing? get() = null
Ray Rahke
04/21/2024, 1:50 PMvar x: Number = 2
var y: Number = 3
var c = x + y
it is so annoying that this is not accepted in kotlinscott
04/21/2024, 7:58 PMobject TimespanTable : Table("timespan"), BaseUniverseTable<Timespan, UByte> {
override val universeId: Column<UInt> = universeIdCol()
override val id: Column<UByte> = ubyte("id")
val timespanId = ubyte("timespan_id")
val name = varchar("name", 100)
override val primaryKey = PrimaryKey(universeId, timespanId)
override fun rowToData(row: ResultRow) = Timespan(
id = row[timespanId],
name = row[name],
)
//This works, but not when I extract to extension function
// suspend fun selectAllForUniverseId(universeIdz: UInt) : List<Timespan> =
// DatabaseSingleton.dbQuery {
// selectAll().where { universeId eq universeIdz }.map { rowToData(it) }
// }
}
suspend fun < T, D> T.selectAllForUniverseId(universeIdz: UInt) : List<D> where T: Table, T: BaseUniverseTable<D,Any> =
DatabaseSingleton.dbQuery {
selectAll().where { universeId eq universeIdz }.map { rowToData(it) }
}
val a : List<Timespan> = runBlocking { TimespanTable.selectAllForUniverseId<TimespanTable, Timespan>(1u) }
val b : List<Timespan> = TimespanTable.selectAllForUniverseId(1u)
Zyle Moore
04/21/2024, 10:20 PMoperator fun MatchResult.getValue(parent: Nothing?, property: KProperty<*>): String =
groups[property.name]?.value ?: ""
I wanted a nicer way to deal with missing values during serialization, so ended up using this for my deserialization, but I'd love to know what the caveats are
@Serializable(with = PidSerializer::class)
data class Pid(
val first: Digits,
val second: Digits? = null,
)
...
override fun deserialize(decoder: Decoder): Pid =
decoder.decodeString().toOption()
.flatMap { pidPattern.matchEntire(it).toOption() }
.map {
val first by it
val second by it
Pid(first, second.ifBlank { null })
}.getOrNull() ?: throw MissingFieldException("first", "PID")
v79
04/22/2024, 7:35 PMKotln
shown here...Phillip Ross
04/23/2024, 1:03 AMManuel Dossinger
04/23/2024, 9:37 AMChronic.parse('tomorrow')
which returns 2024-04-24 (for today ;))Hildebrandt Tobias
04/23/2024, 11:50 AMDog
class with some members. And I want to create a new one in code.
I'd like something that creates this with the press of a button:
Dog(
name = TODO(),
breed = TODO(),
owner = TODO(),
)
Paulo Cereda
04/23/2024, 1:53 PMfuzzy-matcher
) and some cool projects like lucilla
, but we thought of something "less powerful". The archives here suggest kt-fuzzy
but it seems it's not published anymore (although source is fully available). We could implement the algorithms ourselves, just wondering if there's something that went under our radar. Thank you!Jon Bailey
04/23/2024, 7:37 PMobject Foo {
object Bar {
val value = ""
}
}
val t = Foo.Bar.value
But if I put an object in a companion object then it doesn't work as smoothly:
class Foo {
companion object {
object Bar {
val value = ""
}
val fooValue = ""
}
}
val t1 = Foo.fooValue
val t2 = Foo.Companion.Bar.value
val t3 = Foo.Bar.value // This doesn't work, unlike t1 and t2
y
04/23/2024, 9:04 PMfun <T> identity(t: T): T = t
fun <T, E> Iterable<T>.allSameValue(f : (T) -> E = ::identity) : E? { /* ... */ }
I'm getting Required: T, Found E
for the default value on the parameter. why? what's wrong with E = T
?Nick Isaacs
04/24/2024, 8:51 PM