Davide Giuseppe Farella
10/04/2020, 6:33 AMwithTimeout
is more important than the frustration in learning to use threads in JavaPau
10/05/2020, 11:43 AMval cardStats = mutableMapOf<String, Int>()
Once the card is created in other map for example: "France" to "Paris", this is automatically created:
cardStats = mutableMapOf("France" to 0)
In the guessing function, if they miss it adds up fail attempts into the card:
cardStats[ansDef] += 1
But I get this:
Operator call corresponds to a dot-qualified call 'cardStats[ansDef].plusAssign(1)' which is not allowed on a nullable receiver 'cardStats[ansDef]'.
Kenneth
10/06/2020, 10:39 AMfind {}.let {}
give me an Object and not Object? inside the let?Pau
10/06/2020, 11:51 AMclass Rectangle {
val width: Int
val length: Int
constructor(_width: Int, _length: Int) {
width = _width
length = _length
}
}
val rectangle = Rectangle()
B
class Rectangle {
val width: Int
val length: Int
constructor(width: Int, length: Int) {
width = width
length = length
}
}
C
class Rectangle {
val width: Int
val length: Int
constructor(_width: Int, _length: Int) {
width = _width
length = _length
}
constructor(_sizeA: Int, _sizeB: Int) {
width = _sizeA
length = _sizeB
}
}
D
class Rectangle {
val width: Int
val length: Int
constructor(width: Int, length: Int) {
}
}
E
class Rectangle {
val width: Int = 1
val length: Int = 1
constructor(width: Int, length: Int) {
this.width = width
this.length = length
}
}
F
class Rectangle() {
var width: Int = 1
var length: Int = 1
constructor(width: Int, length: Int) {
this.width = width
this.length = length
}
}
I am a bit confused 🧐
1. Signature uniqueness violation
2. Primary constructor call is absent
3. Reassigning previously
4. Uninitialized properties
5. No values passed for constructor pararmeters
6. Reassigning val because of name collisionMax Beyer
10/07/2020, 4:44 PMdata class CarDTO(
val make: String,
val model: String,
val year: Int
)
data class Car(
val make: String,
val model: String,
val year: Int
) {
constructor(carDTO: CarDTO) : this(
make = carDTO.make,
model = carDTO.model,
year = carDTO.year
)
}
Example via extension function:
data class CarDTO(
val make: String,
val model: String,
val year: Int
) {
fun toCar(): Car {
return Car(
make = this.make,
model = this.model,
year = this.year
)
}
}
data class Car(
val make: String,
val model: String,
val year: Int
)
Is there an advantage to using one of these over the other? I’ve been doing some research, but can’t find any arguments either way.Alex
10/08/2020, 12:17 PM@PostMapping("/")
fun addAnnouncement(@RequestBody dto: AnnouncementDTO) {
println(dto);
manager.addAnnouncement(dto.toAnnouncement())
}
This is my body:
{
"text": "Hello",
"occupant": "35A",
"date": "2020-10-09"
}
It works.
Second question: Why?
I'm assuming this might not be even a kotlin-specific question, rather Spring, but as I said, I'm quite new at this so excuse some confusion. 🙂Alex
10/08/2020, 12:22 PMgradle/*
directory into VCS?Valentin Metz
10/08/2020, 3:34 PMValentin Metz
10/08/2020, 4:52 PMBonya
10/08/2020, 8:41 PMJuan B
10/09/2020, 1:54 AMval employeeRunData = EmployeeRunData(annualSalary = BigDecimal(120000))
Instead, I would like to do something like: <note there is not call to _BigDecimal(120000)_>
val employeeRunData = EmployeeRunData(annualSalary = 120000))
I would highlight that the annoyance is while writing unit test. Thank you all!Andrea Giuliano
10/10/2020, 5:46 PMclass ControlledMap(type: Type, index: Int) {
private val table = mapOf(type to index)
fun get(type: Type): Int =
table.getOrElse(type) { throw IllegalArgumentException("Trying to get a non existing content from ControlledMap") }
operator fun plus(other: ControlledMap): ControlledMap {
if (table.keys.any { other.table.containsKey(it) }) {
throw IllegalArgumentException("Cannot merge ControlledMaps with clashing keys")
}
// want to return a new ControlledMap where table is the union of the 2 tables (this.table + other.table)
}
enum class Type {
A,
B,
C,
D
}
}
Daniele B
10/11/2020, 12:17 AMval numbers = listOf(1,2,4,8,11,14)
I would like to create a new list with the differences between a number and the one that is 2 indexes before it. In this case the results would be:
val differencesTwoIndexesApart = listOf(3,6,7,6)
is there an easy way to do that with Kotlin high-order functions?
or is an imperative for loop the best way?Brutus5000
10/12/2020, 8:54 AM@Test
fun reproducibleNPE() {
ZoneId.of("Europe/Berlin").rules.getTransition(LocalDateTime.MIN).isOverlap
}
Looks like I can't trust Java platform calls. But what are the rules? When can I trust it?ildar.i [Android]
10/12/2020, 1:35 PMtseisel
10/12/2020, 2:09 PMfun <T> listDiff(original: List<T>, revised: List<T>): List<DiffOp<T>>
sealed class DiffOp<T> {
class Add<T>(val element: T, val index: Int) : DiffOp<T>()
class Remove<T>(val element: T, val index: Int) : DiffOp<T>()
}
I find myself needing this kind of feature quite often. I've searched the web like crazy, but it seems that Myer's Diff Algorithm has only been implemented for diffing text, not collections.
Swift's standard library implements it as CollectionDifference. Maybe this could be a nice library to have for Kotlin Multiplatform ?Slackbot
10/12/2020, 4:29 PMAndrea Giuliano
10/13/2020, 4:10 PMval myMap = sortedMapOf<String, Collection<Int>>()
Is there a way to put into the map and overflow in the collection in case the key already exist in the map?Andrea Giuliano
10/14/2020, 8:31 AMfirst.intersect(second.toList()).toIntArray()
but this is not really O(n). It takes more time than just using standard comparison one by one (using the property that the 2 IntArrays are ordered). I wonder if there is something elegant to solve this in Kotlin? An ugly way to do this fast would be with something like
private fun intersection(a: IntArray, b: IntArray): IntArray {
val c = IntArray(min(a.size, b.size))
if (c.isEmpty()) {
return c
}
var i = 0
var j = 0
var k = 0
while (i < a.size && j < b.size) {
val aa = a[i]
val bb = b[j]
val comparison = aa - bb
if (comparison == 0) {
c[k++] = aa
i++
j++
} else if (comparison < 0) {
i++
} else {
j++
}
}
return c.copyOf(k)
}
Jgafner
10/14/2020, 8:21 PMfun aFun() : ObjectA?{
val (side,line) = when(someData){
is TypeA -> thread.position?.let { diffData.linesMapper.findFileLocation(it) } ?: return null
is TypeB -> ... ?: return null
}
return ObjectA(side,line)
when ObjectA constructor is expecting null safe type of side and line.
in case of is TypeA happen and it return null what will be the values in side and line ?
tnxJuan B
10/14/2020, 10:44 PMFady Emad
10/15/2020, 12:26 AMJonexume
10/15/2020, 5:23 AMDaniele B
10/15/2020, 9:53 PMdata class City (
val name : String = "",
val country : String = "",
val population : Int = 0,
)
val cities = { listOf(
City("a", "1", 1305770),
City("b", "1", 556934),
City("c", "2", 1924701),
City("d", "3", 5785861),
City("e", "3", 4467118),
) }
Daniele B
10/16/2020, 1:06 AMmylist.zip(mylist.drop(1)) { a, b -> myfunction(a,b) }
is there a way to zip 3 lists instead of 2?
something equivalent to this?
zip(mylist, mylist.drop(1), mylist.drop(2)) { a, b, c -> myfunction(a,b,c) }
I would like to create a new list of values, which are calculated based on the latest 3 valuesSebastian Galkin
10/19/2020, 4:00 AMList
constructors, what are the performance characteristics of List.plus
etc.Son
10/19/2020, 4:29 PMfun fn1(x: Int): Boolean = x % 2 == 0
fun fn2(x: Int): Boolean { return x % 2 == 0 }
fun main () {
val listNum = listOf(1, 2, 3, 4, 5, 6)
val fn3: (Int) -> Boolean = { it % 2 == 0 }
println(listNum.filter(::fn1)) // compiles and outputs [2, 4, 6]
println(listNum.filter(::fn2)) // compiles and outputs [2, 4, 6]
println(listNum.filter(fn3)) // compiles and outputs [2, 4, 6]
// e: org.jetbrains.kotlin.util.KotlinFrontEndException: Exception while analyzing expression at (17,13)
println(listNum.filter(::fn3)) // Error
}
Jacob
10/19/2020, 9:00 PM.not()
and when I should use good old !
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/not.htmlhuehnerlady
10/20/2020, 9:53 AMdata class Foo(val id: Int? = null, val someStringProperty: String? = null)
I would like to write something like
createFoo(propertyName: String, propertyValue: Any): Foo
which I can then use like:
val foo1 = createFoo("id", 5) // Foo(id = 5, someStringProperty = null)
val foo2 = createFoo("someStringProperty", "someString") // Foo(id = null, someStringProperty= "someString")
Any idea how I can achieve that?
It might look very hacky… but it would be really useful for the tests we have (and to convinde colleagues who love grrovy a bit more 😉 )Wavecycle
10/20/2020, 12:36 PMWavecycle
10/20/2020, 12:36 PMdeactivateduser
10/20/2020, 12:37 PMWavecycle
10/20/2020, 12:41 PMdeactivateduser
10/20/2020, 12:44 PMWavecycle
10/20/2020, 12:52 PMdeactivateduser
10/20/2020, 12:53 PM