rook
04/30/2019, 2:45 PMabstract class AbstractFoo<T> {
operator fun invoke(action: T.() ->Unit) {
action() // <-- error here: No value passed for parameter 'p1'
}
}
LeoColman
04/30/2019, 3:27 PMinline fun <reified T> foo() {
when(T) {
is Foo -> tFoo()
is Bar -> tBar()
}
}
kevinherron
04/30/2019, 4:55 PMr2vq
04/30/2019, 6:23 PMclass MyClass(private val getString: (Context, Int, Array<String>) -> String)
I can call it with a lambda as such:
MyClass { context, resId, formatArgs -> context.getString(resId, formatArgs) }
But I can also use a method reference for the first parameter and if the signature matches the second and third parameter.
MyClass(Context::getString)
This is really cool. Is there a specific page on the docs where I can read more about this? I would love to learn more but my :google:-fu is failing me on this topic.xenoterracide
04/30/2019, 6:50 PMsite = <http://dto.site|dto.site> ?: <http://this.site|this.site>!!
is this the best way to say use first if not null else use second if that also is null thow?Nezteb
04/30/2019, 9:35 PMNezteb
05/01/2019, 1:00 AMYossi Saiada
05/01/2019, 11:50 AMif (y) x.getSomething() else null
Note that x.getSomething().takeIf { y }
isn't identical, because it always calculate x.getSomething()
.Don Phillips
05/01/2019, 6:17 PMKirill Zhukov
05/01/2019, 8:01 PMxenoterracide
05/01/2019, 9:02 PM@Target(AnnotationTarget.FUNCTION)
@Retention
@RabbitListener(
bindings = [QueueBinding(
key = [Amqp.FOLEY_NEW],
value = Queue(Amqp.FOLEY_NEW),
exchange = Exchange(name = "amq.topic", type = ExchangeTypes.TOPIC )
)]
)
annotation class Listener( val value: String )
Andrew Gazelka
05/01/2019, 10:52 PMHullaballoonatic
05/02/2019, 1:43 AMMutableList::toList
? Is it just basically a toggle or something, or does it have to copy values into a new object? I imagine that perhaps mutable list is a wrapper for list, but i'm not sureHullaballoonatic
05/02/2019, 4:40 AMfun Double.equals(b: Double): Boolean = abs(this - b) < 1e-20
?Geert
05/02/2019, 8:28 AMBernhard
05/02/2019, 10:07 AMribesg
05/02/2019, 10:15 AMprivate class A {
var x: Int
}
private val a = A()
var x: Int by a.x // Something like that
carlos cdmp
05/02/2019, 11:12 AMobject TextUtils
for instance, instead of just top level functions, as it is creating an instance, it should be using more memory than an old java static function. Do someone know if an object with only functions its a waste of memory when the functions could be top-level?Dias
05/02/2019, 11:29 AMBernhard
05/02/2019, 12:49 PMgustavo.ross
05/02/2019, 12:57 PMStephan Schroeder
05/02/2019, 2:23 PM<T> T getBean(String name, Class<T> requiredType) throws BeansException;
Object getBean(String name, Object... args) throws BeansException;
I want to call the former, but Kotlin calls the later and then flags my implementation as not guaranteeing the specified type
fun worker(worker: String): PipelineElementProcessor = try {
context.getBean(worker, PipelineElementProcessor::javaClass)
}
Is there as solution apart from casting the result to PipelineElementProcessor?Stephan Schroeder
05/02/2019, 3:11 PM/** ...
* @param name the name of the bean to retrieve
* @return an instance of the bean
* @throws NoSuchBeanDefinitionException if there is no bean with the specified name
* @throws BeansException if the bean could not be obtained
*/
Object getBean(String name) throws BeansException;
doesn’t seem to be?! (code from spring-beans-5.1.5.RELEASE)Hullaballoonatic
05/02/2019, 7:25 PMsubList
since they both take two `Int`s as params.earroyoron
05/03/2019, 7:27 AMenum
or sealed
.
The scenario is I have implemented an enum and he is asking me to change to sealed, in the first wave of this discussion I was saying that instead of having a thing like:
sealed class Days {
object Monday: Days(),
object Tuesday: Days(),...
is better to use just an enum
He almost has convinced me when he complains about using when
because with enum you have one type..
Thinking about ADTs in our application now I think the sealed class is the right way, it does matter all elements are `object`(singletons) in it.
What’s your opinion?Stephan Schroeder
05/03/2019, 12:57 PMfun main() {
val result: String = "xY"
println("${result.toUpperCase()}-${result.toString()}")
}
fun String.toUpperCase() = "overridde_toUpperCase"
fun String.toString() = "overridde_toString"
leads to output overridde_toUpperCase-xY
Thomas
05/03/2019, 2:40 PMoperator fun invoke
that takes a vararg
. Here's my stripped-down sample code, the trouble is with the syntax { fn }
in main
, it says "Too many arguments for public constructor NamesBuilder() defined in NamesBuilder". :
class NamesBuilder {
private val names:MutableList<String> = ArrayList()
fun build(): List<String> = names.toList()
operator fun invoke(vararg data: () -> String): NamesBuilder = this.apply { data.forEach { names.add(it()) } }
companion object Helper {
operator fun invoke(vararg data: () -> String): List<String> = NamesBuilder()(*data).build()
}
}
fun main() {
val fn: () -> String = { "1" }
println(NamesBuilder {fn}) // does not work -- Too many arguments...
println(NamesBuilder.invoke(fn)) // works fine, prints [1]
}
As I understand it, the { fn }
syntax should invoke the companion operator fun invoke function... and this works fine if I use .invoke(fn)
. I've tried numerous variations on this theme but no joy.
Can someone help me make this work?Hullaballoonatic
05/03/2019, 11:56 PMDouble::div
? i'd like for it to return 0.0
when dividing by 0.0
in this scopeHullaballoonatic
05/04/2019, 3:01 AMMatrix
class. It extends MutableList<Vector>
(Vector
extends MutableList<Double>
). This allows me to add really nice and intuitive operator functions for row operations like val A = Matrix(5, 5) { i, j -> 1 }
A.row(2) *= 3
A[0] += Vector(0.0, 1.0, 3.0, 4.0, 5.0)
println(A[2]) // < 3.0, 3.0, 3.0, 3.0, 3.0 >
However, column operations aren't going to work the same way because the row and col getters are as so:
fun row(i: Int) = this[i]
fun col(j: Int) = Vector(numRows) { this[it][j] }
so a row operation is performed on the stored Vector from the MutableList, while the col operation is performed on a newly created Vector that wraps the corresponding values.
Does anyone have any ideas how I could allow for col operations in the same intuitive way?Hullaballoonatic
05/04/2019, 3:12 AMoperator fun get(rowIndices: IntRange, j: Int) = col(j)[rowIndices]
print(A[1..3, 0]) // < 1.0, 3.0, 1.0 >
operator fun set(i: Int, colIndices: IntRange, values: List<Double>) {
for (j in colIndices) this[i,j] = values[j]
}
A[2, 0..4] /= 2.0
print(A[2]) // < 1.5, 1.5, 1.5, 3.0, 3.0 >
operator fun set(rowIndices: IntRange, colIndices: IntRange, op: (Int, Int) -> Double) {
for (i in rowIndices)
for (j in colIndices)
this[i,j] = op(i, j)
}
A[1..2, 3..4] = { 0.0 }
You get the picture. They make me REALLY wish Kotlin allowed for rangeTo operators such as ..3
and 4..
that I could bound in the function or supply default arguments into the missing number since it ruins the prettiness of these Range getters/setters to do stuff like A[3..lastRowIndex, 2]
or A[5, 2 until n]
Hullaballoonatic
05/04/2019, 3:12 AMoperator fun get(rowIndices: IntRange, j: Int) = col(j)[rowIndices]
print(A[1..3, 0]) // < 1.0, 3.0, 1.0 >
operator fun set(i: Int, colIndices: IntRange, values: List<Double>) {
for (j in colIndices) this[i,j] = values[j]
}
A[2, 0..4] /= 2.0
print(A[2]) // < 1.5, 1.5, 1.5, 3.0, 3.0 >
operator fun set(rowIndices: IntRange, colIndices: IntRange, op: (Int, Int) -> Double) {
for (i in rowIndices)
for (j in colIndices)
this[i,j] = op(i, j)
}
A[1..2, 3..4] = { 0.0 }
You get the picture. They make me REALLY wish Kotlin allowed for rangeTo operators such as ..3
and 4..
that I could bound in the function or supply default arguments into the missing number since it ruins the prettiness of these Range getters/setters to do stuff like A[3..lastRowIndex, 2]
or A[5, 2 until n]
til
infix to be short for until
, even though it is far worse 😮