Ayfri
04/05/2022, 5:25 PMJan
04/06/2022, 3:31 PM@PublishedApi
internal var onFinish = {}
inline fun onFinish(action: () -> Unit) {
onFinish = action
}
Anthony Flores
04/06/2022, 10:13 PMclass SomeClass {
var string1: String = "Default"
var string2: String = "Default"
var string3: String = "Default"
}
and lets say in the first class, string1
is "Text," string2
, is "Other Text," and in the second class string1
is "String, " while string2
is still Default
I would want it so that the first string is "String," the second string is "Other Text," and string3
as "Default"migueltb01
04/07/2022, 4:41 PMAhmed
04/08/2022, 3:16 PMrunblock {
val result = methodCall(). // this method is suspended
repository.persistResultToDB(result)
}
so, the methodCall()
do we need to await for its completion, to be able to use the returned result
to persist them to the db ?Jan
04/08/2022, 4:42 PMmikehearn
04/11/2022, 9:37 AMfun <T> bar(): T {
error("")
return "" as T
}
fun foo() {
if (true) {
1
} else {
bar()
}
}
This works - bar can be invoked, because it infers to int. But if you unwrap the if statement, suddenly the return type of bar can't be inferred anymore (the contents of bar aren't important here, this crops up no matter what it contains).Michael de Kaste
04/11/2022, 10:59 AMmarko ziza
04/11/2022, 4:47 PMpublic static Document parse(File file, @Nullable String charsetName) throws IOException {
return DataUtil.load(file, charsetName, file.getAbsolutePath());
}
when I call this code from Kotlin,
val document = Jsoup.parse(inputHTML, null )
I get
Kotlin: Null can not be a value of a non-null type String
How can I call this with Java type null, since parameter is @Nullable and therefore Null should be accepted.Ruckus
04/11/2022, 6:48 PMJan
04/11/2022, 7:15 PMColton Idle
04/11/2022, 10:51 PMval data =
hashMapOf(
"firstName" to firstName,
"lastName" to lastName)
but I wanted to not add firstName or lastName to the hashmap if they are null. What would be the best way to do that?andodeki
04/12/2022, 4:22 AMPavel Matusevich
04/12/2022, 6:29 AMCaused by:
org.opentest4j.AssertionFailedError: Array elements differ at index 0. Expected element <Lesson(place=4, timeConstraints=TimeConstraints(startHour=11, startMinute=25, endHour=12, endMinute=10), title=Биология, classID=8, teacherID=104631, journalID=null)>, actual element <Lesson(place=4, timeConstraints=TimeConstraints(startHour=11, startMinute=25, endHour=12, endMinute=10), title=Биология, classID=8, teacherID=104631, journalID=100125)>.
Expected <[Lesson(place=4, timeConstraints=TimeConstraints(startHour=11, startMinute=25, endHour=12, endMinute=10), title=Биология, classID=8, teacherID=104631, journalID=null)]>, actual <[Lesson(place=4, timeConstraints=TimeConstraints(startHour=11, startMinute=25, endHour=12, endMinute=10), title=Биология, classID=8, teacherID=104631, journalID=100125)]>.
The most bizzare part of that assertion fail is part with journalID=null
in expected element. The problem is that there are no null journal ID's in tests.
Does anybody know what is my problem?
Source code for tests (the failing one is marked): https://github.com/Neitex/Schools_Parser/blob/74c9fb9ed14b47cce4d173dc288d0a91a088b3ed/src/test/kotlin/com/neitex/SchoolsByParserTest.kt#L430
Source code of Lesson constructor (this is a regular data class, nothing special): https://github.com/Neitex/Schools_Parser/blob/74c9fb9ed14b47cce4d173dc288d0a91a088b3ed/src/main/kotlin/com/neitex/DataClasses.kt#L63
Thanks in advance!Ruckus
04/12/2022, 6:47 PMpoohbar
04/12/2022, 8:50 PMVitali Plagov
04/13/2022, 4:03 AMval map = getMapOfDataFromOutside()
val homeCountry = map["Home Country"] ?: error("Data table is missing 'Home Country' entry or its key is incorrect")
val homeCity = map["Home City"] ?: error("Data table is missing 'Home City' entry or its key is incorrect")
Hi. I have this map and I handle the nullable value by a key with ?: error(…)
. The error message is equal for all entries I’m retrieving except one parameter, so I want to extract this into a separate method to make code shorter:
val homeCountry = map["Home Country"] ?: throwAnErrorForKey("Home Country")
private fun throwAnErrorForKey(key: String) {
error("Data table is missing '$key' entry or its key is incorrect")
}
With this code, the return type of the homeCountry
is Any
and not String
.
How can I extract the ?: error()
part properly?Klitos Kyriacou
04/14/2022, 8:38 AMval sqlServerDataSource = SQLServerDataSource()
sqlServerDataSource.url = myUrl
sqlServerDataSource.user = myUser
sqlServerDataSource.password = myPassword
The error is:
Cannot access 'password': it is public/*package*/ for synthetic extension in '<library Maven: com.microsoft.sqlserver:mssql-jdbc:10.2.0.jre17>'
There is no error on url or user; only on password. If I replace it with sqlServerDataSource.setPassword(myPassword)
it works. But why can't I use the property syntax, and what exactly does the error message mean, and why only password has this error?Alexander Maryanovsky
04/14/2022, 12:43 PMRuckus
04/14/2022, 6:04 PMdp
04/14/2022, 6:17 PM// This code
val add: (Int, Int) -> Int = { x, y -> x + y }
fun main() {
add(1, 2, 3)
}
// Gives me
Too many arguments for public abstract operator fun invoke(p1: List<Int>): List<Int> defined in kotlin.Function1
Versus with a named function
//This code
fun add (x: Int, y: Int): Int = x + y
fun main() {
add(1, 2, 3)
}
// Gives me
Too many arguments for public fun add(x: Int, y: Int): Int defined in root package in file File.kt
Side note: I don’t know why the former error message has this type annotation for the function (it doesn’t match what my code specifies): fun invoke(p1: List<Int>): List<Int>
2. Is there a way to type a named function via extracting the types, like you can do with variables + lamda/anonymous functions?
// Lamda with inline type annotations
val add1 = { x: Int, y: Int -> x + y}
// Lamda with extracted type annotations
val add2: (Int, Int) -> Int = { x, y -> x + y}
// Named function with inline type annotations
fun add3 (x: Int, y: Int): Int = x + y
// Named function with extracted type annotations
?
This would be helpful so we could easily share type annotations among functions via a typealias. In addition this is particularly helpful for me because I find it harder to parse type annotations when they’re inlined. And I often like to be able to glance at function types and see if it matches what I need versus having to pick it apart from parameters.
Thanks!Anthony Flores
04/14/2022, 6:56 PMallan.conda
04/15/2022, 10:04 AMval nullableFoo: Any? = null
val nonNullFoo = nullableFoo != null
if (nonNullFoo) {
acceptsNonNullFoo(nullableFoo) // expected Any but is Any?
}
but this works
val nullableFoo: Any? = null
if (nullableFoo != null) {
acceptsNonNullFoo(nullableFoo)
}
🤔Ruckus
04/15/2022, 3:26 PMfun generate(selection: Selection, values: Values) { ... }
context(Scope) fun generate(selection: Selection, values: Values) =
generate(this@Scope.selection refine selection, values)
^ this is a recursive call, but I would like to call the other (non context(Scope)) generate function
Tianyu Zhu
04/15/2022, 9:47 PMfun test(foo: List<Number>) {
if (foo.containsOnly<Int>()) {
val bar: List<Int> = foo
}
}
Note that I don't want to use filterIsInstance()
here.
Is it possible to write an extension function that allows for this smart-cast?smit01
04/16/2022, 9:04 AMinterface Logger {
fun log(value:String)
}
context(Logger)
open class A
class B(): A()
Do i need a Logger context to create a object of class B?LastExceed
04/16/2022, 9:59 AMval x: List<Int> = listOf(1, 2, 3)
val y: List<Number> = x //works fine
but not
val x: Array<Int> = arrayOf(1, 2, 3)
val y: Array<Number> = x //error: type mismatch
?Gabor Torok
04/16/2022, 5:53 PMKevin
04/19/2022, 9:13 AMlink▾
Landry Norris
04/19/2022, 3:25 PMLandry Norris
04/19/2022, 3:25 PMCasey Brooks
04/19/2022, 3:51 PMLandry Norris
04/19/2022, 3:59 PMYoussef Shoaib [MOD]
04/19/2022, 4:22 PMrun containingScope@ {
repeat(250){
if(someCondition){
return@outerScope Unit
}
}
}
Landry Norris
04/19/2022, 4:28 PMRuckus
04/19/2022, 5:07 PMvar tries = 0
while (someCondition && tries++ < 250) {
...
}
is probably what you already tried, but it seems like the most straight forward solution to me. Not sure why you'd want some other construct for it, unless I'm missing some context.Landry Norris
04/19/2022, 5:13 PMrocketraman
04/20/2022, 5:00 AMgenerateSequence { task() }
.takeWhile { someCondition }
.take(250)
Landry Norris
04/20/2022, 4:47 PMrocketraman
04/20/2022, 5:14 PMvar
. If the task is suspending, you use the same pattern with a flow builder e.g. flow { emit(task()) }
.