Vladimir Vainer
01/12/2023, 8:46 AMimport kotlin.math.PI
import kotlin.system.measureTimeMillis
fun test1(a : FloatArray) : Float {
val angles = a.map { it.toDegrees() }
val yaw = angles[0]
val pitch = angles[1]
return yaw + pitch
}
fun test2(angles : FloatArray) : Float {
val yaw = angles[0].toDegrees()
val pitch = angles[1].toDegrees()
return yaw + pitch
}
fun Float.toDegrees() : Float {
return (this * 180 / PI).toFloat()
}
fun main(args: Array<String>) {
val a = floatArrayOf(0.5f, 0.7f)
val test2Time = measureTimeMillis {
var out = 0L
for (i in 0..1000000) {
out += test2(a).toLong()
}
}
val test1Time = measureTimeMillis {
var out = 0L
for (i in 0..1000000) {
out += test1(a).toLong()
}
}
println("Test1: $test1Time ms")
println("Test2: $test2Time ms")
}
Test1: 42 ms
Test2: 7 ms
Using map is pretty costy. Am i doing something wrong?Eric Womer
01/12/2023, 9:45 AMJohann Pardanaud
01/12/2023, 9:59 AMonClick
parameter for the Button
class.
My feeling here is there is no idiomatic way to handle events, am I wrong?Tmpod
01/12/2023, 4:14 PMsuspend context(Foo, Bar) () -> Unit
, however its .invoke
method is requiring two arguments, one for each receiver. The thing is: I explicitly have the receiver for one of them, but the other one is also a context receiver of the callee, so it isn't available as this
. Is there any way I can call this function?
this@Foo
and this@Bar
are not options, according to IntelliJ.
Thank you!Vladimir Vainer
01/13/2023, 1:42 AMPaul N
01/13/2023, 11:09 AMUnknown property 'someField' on class 'class rubbish.SomeKotlinClass'
Is there a way of making Kotlin POJO type classes 100% compatible with code like PropertyUtils that relies on Java reflection ?
// <https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils>
implementation("commons-beanutils:commons-beanutils:1.9.3")
package rubbish
import io.kotest.matchers.nulls.shouldNotBeNull
import io.kotest.matchers.shouldBe
import org.apache.commons.beanutils.PropertyUtils
import org.junit.jupiter.api.Test
data class SomeKotlinClass(
@JvmField
var someField: String? = null,
)
class SetTest {
@Test
fun kotlinTestData() {
val x = SomeKotlinClass::class.java.getDeclaredConstructor().newInstance()
x.shouldNotBeNull()
val fields = SomeKotlinClass::class.java.declaredFields
fields.forEach {
println("Field is $it")
println("trying to get field")
SomeKotlinClass::class.java.getField(it.name)
println("got field")
}
PropertyUtils.setProperty(x,"someField","hello")
x.someField shouldBe "hello"
}
}
Lisandro Di Meo
01/13/2023, 1:17 PMonSomething { (key,value) : Map<K,V> -> ... }
Matheus Santos
01/13/2023, 7:37 PMJebus_Chris
01/14/2023, 4:19 AMJohann Pardanaud
01/14/2023, 10:21 AMRyan Smith
01/14/2023, 2:20 PMKaj Koivunen
01/14/2023, 7:56 PMsrc
main
kotlin
*.kt
resources
file.txt
prior to this, just "file.txt" worked.Stylianos Gakis
01/16/2023, 11:15 AMinterface Binding<T> {
var value: T
}
And later I have a function which takes a Binding<String?>
like this fun foo(asd: Binding<String?>) {}
If on a function, I got an instance of Binding<String>
, I am unable to pass it since it sees Binding<String>
and Binding<String?>
as different types.
Changing my interface Binding
to take in out T
instead fixes this problem on the call site, but gives me an error on val value: T
as it tells me that it is delcared as out
but it occurs in invariant
position in type T. If it were a val value: T
it’d work, but it’s a var
now and I can’t change that.
Right now what I am doing, is that in call sites, if I got a Binding<T>
I convert it into a Binding<T?>
manually to pass it in. Is this the best I can do in this case?Sajal
01/16/2023, 2:29 PMsomeLambda
, where the lambda must take in a Contract.()
and return nothing (Unit
). But what does Contract.()
mean? Are we dot-accessing something on an interface?Michael de Kaste
01/17/2023, 10:40 AMdate1..null // Range.BoundedLeft<LocalDateTime>
null..date2 // Range.BoundedRight<LocalDateTime>
date1..date2 // Range.Closed<LocalDateTime>
For this we made an operator fun on the rangeTo operator for comparable classes. This works fine for things like dates and custom classes. But if I want to use the rangeTo on an Int, there is no way for me to set the precedence of my own utility function over that of the construction of an IntRange, is this fixable? I know I can import my custom rangeTo
as somethingElse
, but I'd like to be able to do the following:
val range: Range.Closed = 1..3
Dheerapat
01/17/2023, 11:13 AMfun main() {
val cup = 25
println(cup + " cup of water")
}
However this snippet of code face a compile error and the way to work around this is as follow:
fun main() {
val cup = 25
println("" + cup + " cup of water")
}
I dont't know that in Java/Kotlin world this is normal or not but I think it's a little weird and can be improved to make a developer experience better (I come from JS field)jeggy
01/17/2023, 11:50 AMAdam S
01/17/2023, 6:46 PM0u
? At the moment Kotlin doesn’t know which unsigned type to use, and errors with Overload resolution ambiguity
.vanshg
01/18/2023, 3:27 AMlateinit
var? My attempts at doing so result in an Initialization exception, and I can't find anything else about it on formus/SO. Is there some way to foce the compiler to allow this?liminal
01/18/2023, 4:37 AMMohammad Jahidul Islam
01/18/2023, 5:50 AMclass VeganFood: Food()
interface Seller<out T>
class FoodSeller: Seller<Food>
class VeganFoodSeller: Seller<VeganFood>
interface Consumer<in T>
class Person: Consumer<Food>
class Vegan: Consumer<VeganFood>
fun main() {
var foodSeller: Seller<Food>
foodSeller = FoodSeller()
foodSeller = VeganFoodSeller()
var veganFoodConsumer: Consumer<VeganFood>
veganFoodConsumer = Vegan()
veganFoodConsumer = Person()
}
Does anyone explain covariant and contravariant in Kotlin, please recommend any discussion on this topicy
01/18/2023, 1:52 PMthis
? or should I omit it?David Kubecka
01/18/2023, 2:22 PMclass Cat : Animal {
override var owner: CatOwner
override fun switchOwner(owner: AnimalOwner) {
owner = owner as CatOwner
}
}
The cast to CatOwner
is semantically redundant because it's quite clear what the resulting type should be, only the compiler can't/shouldn't guess it. Still, these casts soon seem like a boilerplate (especially with more complex generic types) so I devised the following helper function:
inline fun <reified TOut : T, T : Any> T.asTOut() = this as TOut
The usage would then simply be
override fun switchOwner(owner: AnimalOwner) {
owner = owner.asTOut()
}
This basically emulates what the compiler does when inferring generic parameter types. Of course, the difference in my example is that it's generally unsafe (that's why the compiler doesn't do it automatically).
What do you think about this? Neat or horrible?Kaj Koivunen
01/18/2023, 2:24 PMvar defaultValue = 4
fun foo(number: Int = defaultValue) { /*something*/ }
even when I change the var, it appears to call the function with the new value. Is there any pitfalls of doing this that I'm unaware of or is it perfectly fine?y
01/19/2023, 10:59 AMProcessor
that processes some data and I have a function process()
that instantiates the class and returns the result. I don’t want to have intermediate state, so I want to disallow creation of this class outside of the file it is declared in.
however, I do want to call the function as Processor.process()
.
currently I have
class Processor private constructor(...) {
//...
private fun finalResults(): Int
companion object {
fun process(...): Int {
return Processor(...).finalResults()
}
}
}
is there another way to do this?
EDIT: basically, I'm asking for a way to emulate namespaces here.Hamza GATTAL
01/19/2023, 2:42 PMval x: int? = null
val y: string? = if (x != null) x.toString() else null
mgrazianodecastro
01/19/2023, 6:21 PMBegum Turan
01/19/2023, 11:01 PMEdward Muller
01/20/2023, 12:11 AMAyfri
01/20/2023, 4:17 PMsealed interface Namespaced : Argument {
val name: String
val namespace: String
override fun asString() = "$namespace:$name"
}
interface Biome : Namespaced {
companion object {
operator fun invoke(biome: String, namespace: String = "minecraft") = object : Biome {
override val name = biome
override val namespace = namespace
}
}
}
interface MobEffect : Namespaced {
companion object {
operator fun invoke(name: String, namespace: String = "minecraft") = object : MobEffect {
override val name = name
override val namespace = namespace
}
}
}
// etc
So I can avoid having the same companion object again and again, some of the interfaces might change so I would prefer to have an interface the companion object
implements, but I can't figure out how to do it
I have to stick with interfaces because every one of them are implemented by an enum listing the native data of each in the game (for example there is a Biome
enum implementing the Biome
interface so you can use the raw values of the enum as an Argument
object).
It helps me for serialization also with the asString()
method which is custom in enums to include the name of the enum entry as lowercase.