RafaelLange
11/10/2020, 2:55 AMxiaobailong24
11/10/2020, 4:57 AMAnaniya
11/10/2020, 8:57 AMfun main(){
val list = listOf(1, 10,-1, -2, 3,12)
val joined = list.joinToString(" + ")
println("${joined} = 10")
}
which prints the list as string with + sine added("1 + 10 + -1 + -2 + 3 + 12 = 10"
). but i wan't it to really add and return either true or false. how can i do thatAstronaut4449
11/10/2020, 10:40 AMfun foo() = println("No receiver")
fun Int.foo() = println("My receiver is $this")
with(42) { foo() } // will print 'My receiver is 42' but I want 'No receiver'
Is it somehow possible to call the function without receiver, although one is given by the current scope?thana
11/10/2020, 12:37 PMfetch
methods, all of which might or might not return a value, depending on what the sql returns.
The compiler doesn;t recognize it's nullable and so we are always in danger to produce NPEs...Nick
11/10/2020, 2:39 PMview.findViewById<AppCompatTextView>(
R.id.strike_price
)?.let {
it.text = price.toString()
}
Would you nit
this, or request changes?
1️⃣ nit
2️⃣ request changes, unnecessary use of let
ursus
11/11/2020, 1:06 AMfun User(...) : User? {
// if invalid input return null
return User(..)
}
data class User(...)
Nuru
11/11/2020, 7:55 AMjro
11/11/2020, 9:03 AMfoo
being null
inside the init block expected behavior? Doesn't seem right for a property that's declared not null. Printing foo
directly results in an error.
Run code in kotlin playgroundelect
11/11/2020, 11:18 AM@Deprecated
annotation just to remind me some critical logic when using a specific variable (in the context of some native code port).. has anyone a better idea?spand
11/11/2020, 12:19 PMval (p1,p2) = pair ?: null to null
elect
11/11/2020, 3:28 PMdf
11/11/2020, 11:28 PMwhile (value.links.next != null) {
any chance to smartcast value.links.next to non-null inside the loop? Basically looking for a counterpart to nullable?.let { it is not null }
ron
11/12/2020, 11:53 AMcode with me
?Geert
11/12/2020, 1:35 PMinterface PrimitiveType<T> {
var value: T?
}
interface Defaultable<T> {
var defaultValue: T
}
@ExperimentalContracts
fun <T> PrimitiveType<T>.defaultValue() : T {
contract {
returns() implies ( this@defaultValue is Defaultable<*>)
}
return this.value!!
}
@ExperimentalContracts
fun <T> PrimitiveType<T>.editValue() : T {
contract {
returns() implies ( this@editValue is Defaultable<*>)
}
return this.value!!
}
@ExperimentalContracts
fun <T> PrimitiveType<T>.setEditValue(value: T) {
contract {
returns() implies ( this@setEditValue is Defaultable<*>)
}
this.value = value
}
ribesg
11/12/2020, 2:23 PMval
before but I’m not sure if it’s needed
myListWithOneMillionElements.filter { it.a in listOf("a", "b") }
Alexander Black
11/12/2020, 2:52 PMval regex = "Regex here".toRegex()
regex.matches("<mailto:first.last@test.com|first.last@test.com>")
what am I missing?Ewan
11/12/2020, 3:40 PMclass Person(val name: String) {
fun greeting() = "Hello, I'm $name"
}
I can run this test in Android Studio:
class ExampleUnitTest {
@Test
fun name() {
val person = Person("Jane")
assert(person.name == "Jane")
}
}
but...
If I set a non-suspending breakpoint to print out the value of person.name or a set a suspending breakpoint and use Evaluate Expression to evaluate person.name I get the error:
Class 'uk.co.telesense.controller.Person' is compiled by a new Kotlin compiler backend and cannot be loaded by the old compiler
If I debug the test with no breakpoints it runs green. Any idea what's going on here?
In the kotlin library I have:
compileKotlin {
kotlinOptions.useIR = true
}
compileTestKotlin {
kotlinOptions.useIR = true
}
in the Android Studio project I have
buildscript {
ext.kotlin_version = '1.4.10'
...
dependencies {
classpath 'com.android.tools.build:gradle:4.2.0-alpha15'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
...
}
I hope that makes sense! Any ideas?Martin Barth
11/12/2020, 4:03 PMNish Patel
11/12/2020, 5:13 PMAnaniya
11/12/2020, 6:32 PMakuleshov7
11/12/2020, 8:16 PMPatrick Ramsey
11/12/2020, 11:33 PMFareesHussain
11/13/2020, 1:26 AMExerosis
11/13/2020, 11:48 AMdata class First(
val partOfFirst: String
)
data class Second(
val partOfSecond: String
)
fun First.Second.multiExtension() {
println(partOfFirst + partOfSecond)
}
fun main() {
First("hello ").apply {
Second("world").apply {
//possible because First and Second are both in this scope.
multiExtension()
}
}
}
andreasmattsson
11/13/2020, 12:46 PMclass C
object O {
operator fun C.plus(other: C): C = this
operator fun C.inc(): C = this
}
Could anyone please explain to me why the second function (inc) is illegal/doesn't compile, but the first is okay (plus)?
Fails with 'operator' modifier is inapplicable on this function: receiver must be a supertype of the return type.
Isn't the receiver (C.
) a supertype of the return type (: C
) in both cases?
This compiles and runs if I move the fun declaration of inc outside the object to root level BTW.TwoClocks
11/13/2020, 9:00 PMFlorian
11/13/2020, 9:22 PMandreasmattsson
11/13/2020, 10:17 PM@JvmField
annotations:
interface Interface {
val value: Int
}
data class Implementation(@JvmField override val value: Int) : Interface
class Test {
@Test
fun test() {
assertEquals(Implementation(123), Implementation(123))
}
}
It works if I do one of three things:
• don't override the interface variable
• remove the @JvmField
annotation
• make it a normal (non-data class)
Dunno if this is intended to not be permitted, or a bug in the generated equals
code. Either way if I leave it as is it compiles, but then throws an exception at runtime:Wilhelm Fitzpatrick
11/14/2020, 1:37 AMfun <T> iterate(seed: T, predicate: (T) -> Boolean, transform: (T) -> T): Sequence<T> {
var current = seed
return generateSequence() {
if (!predicate(current)) null
else {
val was = current
current = transform(was)
was
}
}
}
val seq: Sequence<Int> = iterate(0, { it < 1000 }, { it * 2 + 1 })
fun main() { println(seq.toList()) }
[0, 1, 3, 7, 15, 31, 63, 127, 255, 511]
Wilhelm Fitzpatrick
11/14/2020, 1:37 AMfun <T> iterate(seed: T, predicate: (T) -> Boolean, transform: (T) -> T): Sequence<T> {
var current = seed
return generateSequence() {
if (!predicate(current)) null
else {
val was = current
current = transform(was)
was
}
}
}
val seq: Sequence<Int> = iterate(0, { it < 1000 }, { it * 2 + 1 })
fun main() { println(seq.toList()) }
[0, 1, 3, 7, 15, 31, 63, 127, 255, 511]
Nir
11/14/2020, 1:45 AMephemient
11/14/2020, 1:46 AMval seq = generateSequence(0) { it * 2 + 1 }.takeWhile { it < 1000 }
Wilhelm Fitzpatrick
11/14/2020, 3:24 AMephemient
11/14/2020, 3:34 AM.drop(1).takeWhile { ... }
.withIndex().filter { (index, value) -> index == 0 || ... }.map { it.value }
would let the first item pass regardlesssequenceOf(seed) + generateSequence(transform(seed), transform).takeWhile(predicate)
would be clearer most of the time, IMO