Viral Thakker
09/04/2018, 7:14 AMViral Thakker
09/04/2018, 7:15 AMDaniele Segato
09/04/2018, 8:38 AM@IntDef(
flag = false,
value = {
AspectRatioMode.AUTO,
AspectRatioMode.BY_WIDTH,
AspectRatioMode.BY_HEIGHT,
}
)
public @interface AspectRatioMode {
int AUTO = 0;
int BY_WIDTH = 1;
int BY_HEIGHT = 2;
}
@IntDef
is an Android annotation allowing to define an annotation for Integer constants on Integers:
@AspectRatioMode
int myMode = AspectRatioMode.AUTO; // can only be one of AUTO / BY_WIDTH / BY_HEIGHT
While in kotlin the conversion would be:
@IntDef(flag = false, value =[
AspectRatioMode.AUTO,
AspectRatioMode.BY_WIDTH,
AspectRatioMode.BY_HEIGHT
])
annotation class AspectRatioMode {
companion object {
val AUTO = 0
val BY_WIDTH = 1
val BY_HEIGHT = 2
}
}
but I get an error on `companion object`: "Members are not allowed in annotation class"
I can move those constants out:
object AspectRatioModeConst {
const val AUTO = 0
const val BY_WIDTH = 1
const val BY_HEIGHT = 2
}
@IntDef(flag = false, value = [
AspectRatioModeConst.AUTO,
AspectRatioModeConst.BY_WIDTH,
AspectRatioModeConst.BY_HEIGHT
])
annotation class AspectRatioMode
but that is a lot uglier then Java where I could more cleanly use the annotation directly for constants.
@AspectRatioMode
val myGoodMode : Int = AspectRatioMode.AUTO; // I want this
val myBadMode : Int = AspectRatioModeConst.AUTO; // not this
Is there any other way I can obtain the same result of Java with Kotlin?Daniele Segato
09/04/2018, 9:11 AMvar
bypassing a getter?
See the codeDaniele Segato
09/04/2018, 9:37 AMView
is part of the Android framework and has methods like:
public int getPaddingLeft() { ... }
my Java interface was:
interface MyInterface {
int getPaddingLeft();
}
And for my widget in java I just had to implement the interface without doing anything else, the methods were already there.
With Kotlin tough, the interface become a val
declaration and the compiler complains I should implement those `val`s. If I do I get the error:
Accidental override: The following declarations have the same JVM signature (getPaddingRight()I):
* public open fun <get-paddingRight>(): Int defined in my.package.MyWidget
* public open fun getPaddingRight(): Int defined in my.package.MyWidget"
the error happens on the get()
The second one is of course the Platform method.
Is there a way to tell Kotlin those are the same thing and I do not need to override anything?
see kotlin codeBernhard
09/04/2018, 10:42 AMjuliocbcotta
09/04/2018, 4:43 PMUldis Indriksons
09/05/2018, 8:33 AM.?
works. So we decompiled it using AndroidStudio. The outcome was unexpected for us.
The class:
class NullSafeCall {
var nullableString: String? = null
fun nullSafe() {
nullableString?.length
}
}
Decompiled nullSafe
method:
public final void nullSafe() {
String var10000 = this.nullableString;
if (this.nullableString != null) {
var10000.length();
}
}
It looks to me it can still get NPE as a result of nasty race-condition. Can someone explain to me wether it’ s kotlin issue or decompiler output is not quite accurate.david-wg2
09/05/2018, 9:26 AMval myValue = try {
getValue()
} catch (e: Exception) {
throw MyException()
}
Daniele Segato
09/05/2018, 10:36 AMwakingrufus
09/05/2018, 1:17 PMUzi Landsmann
09/05/2018, 1:41 PMjava.util.List
with kotlin.collections.List
and were surprised to find that the Kotlin serialization took twice the time. The theory is that Jackson serializes to a Java list, and then Kotlin converts it to a Kotlin list. If that is true, then it would be worth the time experimenting with native Kotlin serializers.Daniele Segato
09/05/2018, 4:15 PMeygraber
09/06/2018, 4:28 AMpublic operator fun <T> Set<T>.minus(elements: Iterable<T>): Set<T> {
val other = elements.convertToSetForSetOperationWith(this)
if (other.isEmpty())
return this.toSet()
if (other is Set)
return this.filterNotTo(LinkedHashSet<T>()) { it in other }
val result = LinkedHashSet<T>(this)
result.removeAll(other)
return result
}
My request is that an initial capacity get added to the LinkedHashSet
that is returned if other
is a Set
. I know there's no way to know what the value would be, but it could certainly be approximated at better than the default by doing LinkedHashSet<T>(this.size - other.size)
. That way, in the "best case" where everything in other
is in the set, the capacity will be correct, and in the "worst case", where nothing in other
is in the set, the capacity will have to grow to the size of the set, but we'll likely be closer to that than the default (16).
Am I missing something / is this not necessary, or is this worth filing a bug over?Manish
09/06/2018, 9:02 AMPaul Woitaschek
09/06/2018, 9:55 AMPaul Woitaschek
09/06/2018, 9:59 AMIvan
09/06/2018, 11:52 AMdave08
09/06/2018, 4:37 PMflatMapValues
to a map? All that currently exists is mapValues
... My use case is a json hierarchy that needs to be flattened into a Map<String, Map<String, JsonElement>>
. It's something like { "level1": { "ns": { "s1": "", "s2": "" } } }
, and needs to be [level1 = [ ns.s1 = "", ns.s2 = ""]]
. And it's using Gson's JsonObject, etc...lifter
09/06/2018, 7:45 PMy
and z
are defined, or is it really the same thing?
val x: Int? = 0
val y = x!!
val z = x as Int
Dico
09/06/2018, 8:39 PMval printHash: (Any) -> Unit = fun (any: Any) { println(any.hashCode()) }
Michael
09/06/2018, 8:56 PMException during code generation
(The rest of us use linux/mac). Looks like it tried to generate a class file with a name that included the name of a test method - and the method was ``a method name with “quotes” in it``.lifter
09/06/2018, 9:29 PMmkDivider1
compiles but mkDivider2
doesn't? ('this' is not defined in this context
)
fun mkDivider1(x: Int): (Int.() -> Int) = { this / x }
fun mkDivider2(x: Int): (Int.() -> Int)? = if (x == 0) null else { this / x }
By the way, this one does compile:
fun mkDivider3(x: Int) = if (x == 0) null else fun Int.(): Int { return this / x }
Paul Woitaschek
09/07/2018, 8:45 AMNikky
09/07/2018, 1:25 PMvar Wrapper<Env>.someProperty by ref(actual::someProperty)
this only works in the class seems like i do not have access to this or properties of the class i try to extend when using by
btw i am using 1.3-M2
the intent here is to have one large data class , but disallow setting all vars under all circumstances in a DSL
only when its matching the Env in the wrapper the extension functions should be there and forwardNikky
09/07/2018, 1:50 PMБежан Александр
09/07/2018, 5:03 PMPsiJavaFile
Dominaezzz
09/07/2018, 7:54 PMrook
09/07/2018, 9:16 PMList<String?>
even though I’m filtering for null.
someListOfNullableStrings.map { it?.let { str -> Foo(str) } }.filter { it != null }
Shawn
09/07/2018, 10:41 PM@Throws(KClass<out Throwable>...)
annotation but have you considered not using checked exceptionsShawn
09/07/2018, 10:41 PM@Throws(KClass<out Throwable>...)
annotation but have you considered not using checked exceptionsSam
09/07/2018, 10:43 PMelizarov
09/08/2018, 6:39 AMString.toInt
and String.toIntOrNull
in the standard library for example).Sam
09/08/2018, 1:57 PM