Marko Novakovic
12/22/2021, 1:30 PMkotlin-test
broke 🤔 can’t fine import kotlin.test.Test
annotation. is there something going on with kotlin test that am not aware of?Marko Novakovic
12/22/2021, 3:23 PMcreate()
, method that is explicitly named or use invoke
operator function on a companion object
?leysont
12/22/2021, 4:39 PMAnkit Shah
12/22/2021, 5:15 PM(10/3).toInt()
it returns 3 but what I want is the upper value i.e. 4. How can I achieve it?Filipe Duarte
12/23/2021, 5:58 PMJosh Horwitz
12/24/2021, 1:44 AMGlen
12/24/2021, 1:46 PMChetan Tuteja
12/27/2021, 7:19 AMin
check for that.
Is there any way to do the same check while ignoring case check?
I know I can do the any
check, anything besides that?
val abc = "abc"
val random = arrayOf("ABC", "egf")
val isThere = abc in random // Prints false
Mendess
12/27/2021, 1:07 PMGrian
12/28/2021, 5:55 AMmyDsl {
1,
2,
3,
}
and end up with like a list of 1, 2, 3
somewhere in the DSL's codeasavio
12/28/2021, 8:59 AMSequence
or one with a really a large count of objects lazily without it being too time consuming like the factory methods?hfhbd
12/28/2021, 4:05 PMclass Config {
lateinit var s: String
}
class Consumer(val config: Config) {
init {
require(config::s.isInitialized)
}
}
My current workaround is using an internal member function: fun isInitialized() = ::s.isInitialized
and call this function in require
.JonasDaWi
12/28/2021, 5:07 PMEivind
12/29/2021, 9:03 AMError
that I want to use to represents errors occuring in my api (not exceptions, but state different than what the user expected when they made their request). When I use it now Kotlin defaults to the java.lang.Error.
Can I change this default, or should I pick a different name?David W
12/29/2021, 8:38 PMList<Lock>
and an anonymous function. It needs to lock all of the locks, execute the function, and then unlock all locks.
The anonymous function should also allow non-local returns. I've been banging my head against this for hours, anyone have a solution?
inline fun <T> write(locks: List<ReentrantReadWriteLock>, action: () -> T): T
mcpiroman
12/31/2021, 3:18 PMabstract class Base<T : Any>(val clazz: KClass<T>)
class Foo; class Bar;
object A : Base<Foo>(Foo::class) { ... }
object B : Base<Bar>(Bar::class) { ... }
Can I somehow 'reifine' the `T`paramater so I only have to write the parameter class once, e.g. `Base<Foo>()`or Base(Foo::class)
?
Edit: OK, it's fine that it's possible but I actually meant to ask 'how' to do that. The 2 ways I suggested above do not compile, nor can I use `reified`modifier.MisileLab
01/01/2022, 7:48 AMJames Whitehead
01/01/2022, 7:36 PM<T>
and the second parameter is "an instance or implementation of T"?
• Also kind of related question: When should one use <T>
vs <T : Any>
, I'm not really sure what the difference is?
Thanks in advance 🙂Ayfri
01/02/2022, 5:11 AMprivate inline fun execute(block: () -> Unit) = block()
fun main() {
execute { println("test") }
}
But when I see the Bytecode, it creates an execute
static method on the MainKt class but never use it, is there a way to say to the compiler to not generate this method and just inline ?hfhbd
01/02/2022, 10:54 AMTestingOnly
, or do you really create a new module containing only 1 function and publish it?Rob Elliot
01/02/2022, 11:42 AMorg.apache.commons.text.StringEscapeUtils
?Pablo
01/02/2022, 12:59 PMsealed class OrganizationExceptions : Exception() {
object OrganizationNotFound : OrganizationExceptions()
object ListNotAvailable : OrganizationExceptions()
}
This is an example, but it won't be always "OrganizationExceptions" I'm creating a method generic, also I don't know if it's better to create a sealed class or create
class OrganizationNotFoundException : Throwable() //or Exception()
class ListNotAvailable : Throwable() //or Exception()
Any recomendations?
the method signature is :
fun apiCallWithStatusCode(codes : HashMap<Int, Throwable>, apiCall : suspend () -> Response<T>,){...}
sorianog
01/03/2022, 2:50 PMglenkpeterson
01/03/2022, 10:43 PMval dotOne: Double = "0.1".toDouble()
assertSame(dotOne, dotOne)
expected: java.lang.Double@76774d21<0.1> but was: java.lang.Double@68be905c<0.1>
I understand that assertSame
uses referential equality (checks that the parameters have the same virtual memory address) and should function like the ===
operator. 0.1 !== 0.1
because they are primitives and could be boxed to different objects before being compared for referential equality. But, when I declare a variable, I would expect it to have the same virtual memory address as itself. Be referentially (and structurally) equivalent to itself.
Can I declare my variable somehow to not be compiled away to two different primitives that happen to have different virtual memory addresses?dave08
01/04/2022, 12:20 PM@JvmInline
value class MethodParams1<A1>(val value: Array<Any?>) {
operator fun component1() = value[0] as A1
var arg1: A1
get() = value[0] as A1
set(value) { this.value[0] = value }
}
Colton Idle
01/05/2022, 5:10 AMprivate fun List<NetworkFoo>.mapToDomain(): List<DomainFoo> {
return this.map { //stuff }
}
and
private fun List<NetworkBar>.mapToDomain(): List<DomainBar> {
return this.map { //stuff }
}
Error
Platform declaration clash: The following declarations have the same JVM signatureI suppose the error message is helpful in giving me a hint that these two methods are identical in JVM land? If I use the auto-correct option I get
@JvmName("mapToDomaincom.network.model.NetworkBar")
but the annotation gives me an error of:
Illegal JVM name
Simon Lin
01/05/2022, 6:58 AMval list = mutableListOf<String>()
list.add(0, "a")
list.add(0, "b")
list.toString() // except [b], actual [b, a]
Jason5lee
01/05/2022, 12:25 PM"something${somelogic}something${somelogic}"
. But it ends up being too long for a line. Any tips to rewrite to make the line shorter but still being conscious of how the string is built?Jasin Colegrove
01/05/2022, 12:39 PMLiudvikas Sablauskas
01/05/2022, 1:10 PM/**
* Task: Make line `DisNonNullable.nonNull()` not compile
* Clarification: `DisNonNullable.nonNull()` will always have a non-null value, so it makes sense to not allow that method
* Clarification: Everything else has to compile just as before, only `DisNonNullable.nonNull()` should fail
* Clarification: The non-generic parameter type String is chosen only as an example. This hardcoded type is not the focus of the task
* Restrictions: Mustn't modify main() method, DisNullable and DisNotNullable classes
*/
fun main(args: Array<String>) {
DisNullable.nonNull()
DisNonNullable.nonNull()
}
abstract class GenericMapper<T> {
abstract fun map(string: String): T
fun nonNull(): GenericMapper<T> {
val delegate = this
return object : GenericMapper<T>() {
override fun map(string: String): T {
return delegate.map(string)
}
}
}
}
object DisNullable : GenericMapper<String?>() {
override fun map(string: String) = string
}
object DisNonNullable : GenericMapper<String>() {
override fun map(string: String) = string
}
Liudvikas Sablauskas
01/05/2022, 1:10 PM/**
* Task: Make line `DisNonNullable.nonNull()` not compile
* Clarification: `DisNonNullable.nonNull()` will always have a non-null value, so it makes sense to not allow that method
* Clarification: Everything else has to compile just as before, only `DisNonNullable.nonNull()` should fail
* Clarification: The non-generic parameter type String is chosen only as an example. This hardcoded type is not the focus of the task
* Restrictions: Mustn't modify main() method, DisNullable and DisNotNullable classes
*/
fun main(args: Array<String>) {
DisNullable.nonNull()
DisNonNullable.nonNull()
}
abstract class GenericMapper<T> {
abstract fun map(string: String): T
fun nonNull(): GenericMapper<T> {
val delegate = this
return object : GenericMapper<T>() {
override fun map(string: String): T {
return delegate.map(string)
}
}
}
}
object DisNullable : GenericMapper<String?>() {
override fun map(string: String) = string
}
object DisNonNullable : GenericMapper<String>() {
override fun map(string: String) = string
}
Rob Elliot
01/05/2022, 1:23 PMfun main(args: Array<String>) {
DisNullable.nonNull()
DisNonNullable.nonNull()
}
// Internal so that there are only two immediate subtypes, but it isn't sealed, so other subtypes of
// those immediate subtypes can be defined in other packages.
abstract class GenericMapper<T> internal constructor() {
abstract fun map(string: String): T
}
abstract class NullableGenericMapper<T> : GenericMapper<T>() {
fun nonNull(): GenericMapper<T> {
val delegate = this
return object : GenericMapper<T>() {
override fun map(string: String): T {
return delegate.map(string)
}
}
}
}
abstract class NonNullableGenericMapper<T : Any> : GenericMapper<T>()
object DisNullable : NullableGenericMapper<String?>() {
override fun map(string: String) = string
}
object DisNonNullable : NonNullableGenericMapper<String>() {
override fun map(string: String) = string
}
fun nonNull()
when I wrote that. Guess in my version it should return NonNullableGenericMapper
.Liudvikas Sablauskas
01/05/2022, 1:39 PMRob Elliot
01/05/2022, 1:41 PMLiudvikas Sablauskas
01/05/2022, 1:42 PMobject DisNullable : GenericMapper<String?>() {
override fun map(string: String) = string
}
object DisNonNullable : GenericMapper<String>() {
override fun map(string: String) = string
}
This should not change at allRob Elliot
01/05/2022, 1:42 PMLiudvikas Sablauskas
01/05/2022, 1:43 PMRob Elliot
01/05/2022, 1:44 PMLiudvikas Sablauskas
01/05/2022, 1:46 PM"The constraints are arbitrary and it may be impossible to meet themNo, I just said that
But I definitely remember that there was a solution, exactly according to these rules
Rob Elliot
01/05/2022, 1:47 PMabstract class NullableGenericMapper<E : Any> : GenericMapper<E?>() {
fun nonNull(): NonNullableGenericMapper<E> = TODO()
}
object DisNullable : NullableGenericMapper<String>() {
override fun map(string: String): String? = null
}
Jason5lee
01/05/2022, 2:34 PMClarification: `DisNonNullable.nonNull()` will always have a non-null value, so it makes sense to not allow that method
It does not make sense to me.Rob Elliot
01/05/2022, 2:46 PMnonNull
is to transform a GenericMapper<String?>
into a GenericMapper<String>
so that when you call map
on it you get back a String
not a String?
.
So calling GenericMapper<String>.nonNull()
is pointless - it would effectively return itself - and the idea is to make it not compile so that people don't accidentally call it.Liudvikas Sablauskas
01/05/2022, 2:49 PMRob Elliot
01/05/2022, 3:07 PMfun main(args: Array<String>) {
DisNullable.nonNull()
DisNonNullable.nonNull()
}
abstract class GenericMapper<T> {
abstract fun map(string: String): T
}
fun <T> GenericMapper<T?>.nonNull(): GenericMapper<T> {
val delegate = this
return object : GenericMapper<T>() {
override fun map(string: String): T {
return delegate.map(string) ?: TODO("handle null")
}
}
}
object DisNullable : GenericMapper<String?>() {
override fun map(string: String) = string
}
object DisNonNullable : GenericMapper<String>() {
override fun map(string: String) = string
}
Liudvikas Sablauskas
01/05/2022, 3:25 PM