thana
07/11/2019, 8:28 AMinfix fun <T : Any?> T.`if`(condition: Boolean): Pair<Boolean, T> =
Pair(condition, this)
infix fun <T> Pair<Boolean, T>.`else`(block: () -> T) =
if (component1()) component2()
else block()
ValV
07/11/2019, 8:37 AMCompleter
that has only one method (function) public abstract int complete(arg1, arg2, arg3)
, so when a variable completer: Completer?
is assigned, it's done via Java lambda
completer = (arg1, arg2, arg3) -> { ... }
Instead of creating (at least) anonymous object of type Completer
and override
-ing the complete
method. Is it possible to do something like that in Kotlin, or I have to do
object: Completer {
override fun complete(arg1, arg2, arg3) { ... }
}
Why I can't do it with lambda -- because the compiler complains that completer
must be of type Completer?
, not (???, ???, ???) -> Unit
KindRoacher
07/11/2019, 9:02 AMelect
07/11/2019, 9:32 AMjdeps
, however I need to specify where to find the kotlin.stdlib
module, is there a standard location for that or shall I search in my .m2
local gradle cache?ValV
07/11/2019, 10:04 AMMutableList<String>
-> Array<String>
?elect
07/11/2019, 10:16 AMmanifest.attributes('Multi-Release': 'true')
, but now jdeps
complains: Error: kotlin-stdlib-1.3.41.jar is not a multi-release jar file but --multi-release option is set
Alexpl292
07/11/2019, 11:10 AMValV
07/11/2019, 11:29 AMclass MyClass {
companion object {
public fun run(args: Array<String>) {
...
}
}
public fun main(args: Array<String>) {
...
}
}
How to call run
from main
?Ifvwm
07/11/2019, 11:39 AMxenoterracide
07/11/2019, 5:40 PMinternal inline fun <reified T: BaseResource> find( criteria: (query: IQuery<Bundle>) -> IQuery<Bundle>): T? {
val forResource = client.search<Bundle>().forResource(T::class.java)
val entities = criteria.invoke(forResource).execute()
if ( entities.isEmpty ) {
<http://log.info|log.info>("entity {} was not found", T::class.simpleName)
return null
}
if ( entities.total > 1 ) {
<http://log.info|log.info>("too many {} found", T::class.simpleName)
return null
}
return entities.entryFirstRep.resource as? T
}
internal fun findLocation(name: String): Location? {
return find {it.where(Location.NAME.matches().value(name)) }
}
is there a method signature where I can just call where
instead of it.where
?mathew murphy
07/11/2019, 5:44 PMjava.lang.String
to String?
rather than String!
within a specified context, so that I get errors or warnings if I try to do (say) resultset.getString(2).trim()
?groostav
07/11/2019, 6:12 PMxenoterracide
07/11/2019, 8:36 PMexists { Predicate } : Boolean
on collections?Florian
07/11/2019, 9:14 PM&
and |
boolean operator?Florian
07/11/2019, 9:15 PM&&
and ||
not have an operator fun
?vlad.minaev
07/12/2019, 10:52 AMPick
type which lets you easily (and type-safe) define sub-shape:
interface Todo {
title: string;
description: string;
completed: boolean;
}
type TodoPreview = Pick<Todo, 'title' | 'completed'>; // you can only specify properties of Todo here
const todo: TodoPreview = {
title: 'Clean room',
completed: false,
};
I'm trying to find out whether it's possible to achieve something like this in KotlinChris
07/12/2019, 1:58 PMAndrew Gazelka
07/12/2019, 6:25 PMfun <T> wrapperFunc(block: (…) -> T): (…) -> T{
println("wrapped")
return block (…) -> T
}
and call it like
val testFunc = wrapperFunc {
fun(a: String) -> println("a is $a")
}
testFunc("abc")
pavi2410
07/12/2019, 6:50 PMHullaballoonatic
07/12/2019, 7:25 PMonEach
more efficient than map
?Eric O'Connell
07/12/2019, 7:57 PMfun <T, R> Optional<T>.mapDefault(default: R, mapper: (T) -> R): R
and call it with a default of a subtype of a sealed class
e.g. maybeThing.mapDefault(Action.NoFoo) { thing -> Action.Foo }
then this fails type checking because R
in mapDefault
is bound to Action.NoFoo
— is there a good way around this besides calling maybeThing.mapDefault<Thing, Action>
?bbaldino
07/12/2019, 8:57 PMMutableMap
externally as a Map
without using a "wrapper" member? i.e. private val _data = mutableMapOf<Int, String>()
val data: Map<Int, String>
get() = _data
tipsy
07/13/2019, 8:41 AMinit {
require(level in 0..9) {
"Valid range for parameter 'level' is 0 to 9"
}
}
ciriti
07/13/2019, 9:36 AMEric Martori
07/13/2019, 11:14 AMsealed classes
and method overloading. Imagine I have the following sealed class:
sealed class State {
object A : State()
data class B(val i: Int) : State()
}
And the following methods defined:
fun process(a: State.A): Unit {/**/}
fun process(b: State.B): Unit {/**/}
I should be able to call process(state)
with an instance of State
since the compiler knows that there are only two subtypes of it and that the method is defined for both of them. But it forces me to check the type (with a when
for example):
fun newState(state: State) = process(state) //doesn`t compile (None of the following functions can be called with the arguments supplied)
fun newState(state: State) = when(state) { // this works but its really verbose
is State.A -> process(state)
is State.B -> process(state)
}
turansky
07/14/2019, 1:29 PMFudge
07/14/2019, 2:59 PMv0ldem0rt
07/14/2019, 3:57 PMIfvwm
07/14/2019, 4:22 PMAlexjok
07/14/2019, 8:18 PMAlexjok
07/14/2019, 8:18 PMkarelpeeters
07/14/2019, 8:18 PMAlexjok
07/14/2019, 8:24 PMlistOf(1,2,3,4,5,6,7,8,9,10)
.map { element -> async { delay(1000); element * 2 }}
.awaitAll()
.forEach(::println)
How i can chunk it, send only 2 elements compute it in parallel and after send next 2 elements?karelpeeters
07/14/2019, 8:25 PM