Philipp Hanslovsky
03/20/2021, 3:08 PMorg.jetbrains.kotlin:kotlin-compiler-embeddable:jar:1.4.30:compile
, which introduces the JetBrains trove4j fork org.jetbrains.intellij.deps:trove4j:jar:1.0.20181211:runtime
as a transitive dependency. (entire dependency tree in thread). One of the usage scenarios installs the jars and transitive dependencies into a program that already has GNU trove4j
as a dependency, but in an incompatible version. What is the best way to avoid dependency clashes here? I tried shading all of kotlin-compiler-embeddable
and the jetbrains trove4j
into the jar that I generate for installation and it works. Is this the recommended way to solve this clash between jetbrains trove4j
fork and GNUE trove4j
? If anyone has examples, I would like to take a look at those to make sure, I only include the dependencies necessary and also don't leave out anything.therealbluepandabear
03/20/2021, 8:31 PMDominick
03/21/2021, 5:57 AMenum class PacketType(val id: Short, vararg aliases: String) {
CUSTOM_PAYLOAD(0), KEEP_ALIVE(1), LATENCY(2);
companion object {
private val PACKETS_BY_ID = mutableMapOf<Short, PacketType>()
private val PACKETS_BY_ALIAS = mutableMapOf<String, PacketType>()
fun fromId(id: Short): PacketType? = PACKETS_BY_ID[id]
fun fromName(id: String): PacketType? {
return try {
valueOf(id)
} catch (e: IllegalArgumentException) {
PACKETS_BY_ALIAS[id]
}
}
}
init {
PacketType.PACKETS_BY_ID[id] = this
for (alias in aliases) {
PacketType.PACKETS_BY_ALIAS[alias] = this
}
}
operator fun get(id: Short) = fromId(id)
operator fun get(id: String) = fromName(id)
}
For some reason, PACKETS_BY_ID and PACKETS_BY_ALIAS don't get initializedDanish Ansari
03/21/2021, 8:44 AMandroidx.compose.material.Colors
class which should be changed later that's why I have used var
var Colors.warning: Color
get() = Color(0xFFFFC107)
set(value) {
field = value
}
But I'm getting error in the setter Unresolved reference: field
Dunno how to handle this scenariofranztesca
03/21/2021, 11:35 AMimport kotlin.jvm.internal.Reflection
inline fun <reified T: Number>round(number: T): Int {
return when(T::class){
Int::class -> number as Int
Double::class -> (number as Double).toInt()
else -> throw IllegalArgumentException("Invalid type")
}
}
fun test(){
print(round(1))
print(round(2.0))
print(round(3f))
}
fun howShouldBeCompiled(){
print(1)
print((2.0 as Number).toInt())
throw IllegalArgumentException("Invalid type")
}
fun howItIsCompiled() {
val number = 1
val type = Reflection.getOrCreateKotlinClass(Int::class.java)
var result1: Int = when(type){
Int::class.java -> number
Double::class.java -> (number as Double).toInt()
else -> throw java.lang.IllegalArgumentException("Invalid type")
}
print(result1)
//... do the same for each case
}
Is there a way to inline reified generic functions at compile time? Isn't T::class known at compile time? The only alternative which I see is removing the generic and manually overload each possible accepted class (in this case it's just two, but I have more complex cases with like 8 input classes). I feel like having only one function declaration with a switch (when) is more elegant than 8 declarations with overload (for simple functions).christophsturm
03/21/2021, 1:30 PMdephinera
03/21/2021, 2:43 PMinterface Scope
class Service : Scope {
fun <T> scoped(block: Service.() -> T): T {
return block().also {
finalize()
}
}
fun Scope.foo(): String {
return "Heya!"
}
private fun finalize() {
// release resources and stuff
}
}
fun main() {
val service = Service()
val res = service.scoped {
foo()
}
service.foo() // ERROR
}
Basically I achieved what I wanted - to be able to call Service's methods only within a scope, so some finalization work can be done automatically, instead of relying on the consumer to do it. However I don't really understand why I can't call foo
outside the scope. Can anyone explain it, please?samuel
03/21/2021, 8:31 PMString
such as toInt
toLong
etc?. I can see the file and package they are contained in here but i am not sure how to actually access them, could anyone provide a hint?Michael S.
03/21/2021, 8:59 PMlet()
function it works fine even though the IDE is complaining about it. Is this intended behavior?Robert
03/21/2021, 9:11 PMTwoClocks
03/22/2021, 5:46 AMType mismatch: inferred type is MessageDispatch but Nothing was expected
what's a nothing
? And the docs say it should be an instance of the class... which it is.. very confused.Karlo Lozovina
03/22/2021, 12:16 PMRooparsh
03/22/2021, 12:36 PMconst {a , …remaining} = user
…
is called spread operator in JavaScript
Where
user = {a : "12", b : 2 , c: "Some random String"}
so the value of user.a
was copied in variable a
and all the remaining keys of user object user.b
and user.c
were copied in remaining
variable.
This concept is called Destructuring
and in kotlin world, the nearest possible syntax that I know, is
val (a,b,c) = user
But, can we write this anyway better without declaring 3 variables ???
So, that my final code should be
val (a,remaining) = user
Fábio Carneiro
03/22/2021, 1:13 PMLuca Piccinelli
03/22/2021, 2:11 PMCdev
03/22/2021, 7:55 PMtherealbluepandabear
03/23/2021, 2:25 AMHolger Steinhauer [Mod]
03/23/2021, 8:34 AMjvmusin
03/23/2021, 10:56 AM1.4.32
is out, but there are no announcements and no new releases out on GitHub.
P.S. Well, basically it said that serialization
and multiplatform
plugins are updated to 1.4.32
, seems like the language is not updated yet, but the plugins are.df
03/23/2021, 11:23 AMRob Elliot
03/23/2021, 11:52 AMlateinit var
on the JVM on a field on an object used from more than one thread do you also need @Volatile
to prevent other threads seeing it as null, or do you get that for free?Mehdi Haghgoo
03/23/2021, 12:28 PMlandon burch
03/23/2021, 3:59 PMShootingStar
03/23/2021, 5:40 PMRestricted suspending functions can only invoke member or extension suspending functions on their restricted coroutine scope
Can I get some advice?
@RestrictsSuspension
abstract class Service() {
protected suspend inline fun yieldAsync() { ... }
protected abstract suspend fun start()
private suspend fun startWrapper() {
yieldAsync()
start()
}
private fun <T> launchCoroutine(block: suspend () -> T) = block.startCoroutine(...)
final override fun onLifecycleStart() {
launchCoroutine { startWrapper() } // <- error spot startWrapper()
}
}
Ademir Queiroga
03/23/2021, 8:58 PM@Model
interface Config {
@Prop(name = "renamedConfigField")
val configField: String
}
When processing the Config interface I would like to be able to iterate over it’s properties (which I’m already doing) but I’m not being able to read the fields annotations (e.g. read the @Prop annotation on the configField
)Danilo Lima
03/23/2021, 10:30 PMJuan B
03/23/2021, 11:12 PMclass InputExample(val map: Map<String, Any?>) {
val name: String? by map
val age: Int? by map
val lastname: String? by map
fun KProperty<Any>.isPresent(): Boolean {
//return map.containsKey(this.name)
return true
}
}
I was trying to get to a interface like this:
inputType.lastname.isPresent()
Before Old
03/24/2021, 1:59 AMBrian Dilley
03/24/2021, 5:04 AMPeter Ertl
03/24/2021, 2:09 PMPeter Ertl
03/24/2021, 2:09 PMRuckus
03/24/2021, 2:18 PMint
instead of char
is so it can return a -1
if you've reached the EOI.Peter Ertl
03/24/2021, 2:18 PMRuckus
03/24/2021, 2:19 PMPeter Ertl
03/24/2021, 2:20 PMinput.read().takeIf { it != -1 }?.toChar()
Ruckus
03/24/2021, 2:21 PMInt
and the Char
. Whether that matters is entirely dependent on your use case. In general it's most likely fine, but worth keeping in mind if it's in a hot loop and you see any performance issues.Peter Ertl
03/24/2021, 2:22 PMRuckus
03/24/2021, 2:27 PMOption
or Result
type, but unfortunately those aren't free on the JVM.Peter Ertl
03/24/2021, 2:28 PMRuckus
03/24/2021, 2:30 PM-1
as a sentinel is such a well established standard in Java at this point I doubt many will see the need (or want) to change it. I could be wrong though...indexOf
uses it.Peter Ertl
03/24/2021, 2:32 PMRuckus
03/24/2021, 2:33 PMephemient
03/24/2021, 4:07 PMindexOf()
uses -1 as a sentinel. that will stay unless there's an inexpensive alternative