elect
12/15/2017, 2:03 PM((A) -> B)?
, but the alternatives look worstjkbbwr
12/15/2017, 3:38 PMtypealias init = (Int) -> Int
fun main(args: Array<String>) {
val a: init? = null
}
natpryce
12/15/2017, 3:43 PMlet
, apply
, also
, etc. to work with tail recursive functions?
For example, I’d have thought the following two functions were the same, but the foo2
is not tail recursive.
tailrec fun foo1(acc: Int, x: Int?): Int? =
if (x == null) {
null
} else if (x <= 0) {
acc
} else {
foo1(acc+1, x-1)
}
tailrec fun foo2(acc: Int, x: Int?): Int? =
x?.let {
if (it <= 0) {
acc
} else {
foo2(acc+1, it-1)
}
}
karelpeeters
12/16/2017, 8:09 PMoperator fun invoke
in the companion object.elect
12/17/2017, 6:55 PMreturn
inside a constructor? Because right now I'm forced to use multiple if-else
with the consequent indentationsbenleggiero
12/17/2017, 6:56 PMreturn
makes sense in a `constructor`…oblakr24
12/19/2017, 8:35 PMvoddan
12/20/2017, 4:17 PMmap
, filter
, etc)? I remember some discussions going, but I can't find the issue.dsavvinov
12/22/2017, 10:12 PMwhen
can be quite nice in some use-cases, and we're aware of that. It's just not the first-priority feature currently.Slackbot
01/04/2018, 9:34 AMianbrandt
01/08/2018, 6:53 AMval bowlingFrames = arrayOf(*1..10)
Also:
val notAnArray = listOf(1, 2, 3)
val list = asList(-1, 0, *notAnArray, 4)
(https://discuss.kotlinlang.org/t/arrays-from-ranges/5216)
Update: Looks like I'm late to the party with this one: https://youtrack.jetbrains.com/issue/KT-12663.dh44t
01/10/2018, 6:19 PMpoohbar
01/11/2018, 1:54 PMniccorder
01/11/2018, 6:53 PMraulraja
01/11/2018, 6:57 PMval getPremiumWithTotal = CreditCard.getPremium(creditCards.length) _
fun getPremuimWithTotal(creditCard: CreditCard) = CreditCard.getPremium(creditCards.length, creditCard)
@Ruckus It's an improvement because depends on how they are implemented in the lang the value creditCards.length
can be memoized vs recomputed.orangy
val getPremiumWithTotal = { cc -> CreditCard.getPremium(creditCards.size, cc) }
will it be sufficient? Not much more than a single and to my taste confusing _
after something that looks like a function callorangy
val (width, height) = point
and such), so if possible, I wouldn’t add more confusion here.
E.g. given fun f(a: String, b: String)
, how do you use PA to bind first or second parameter to some value?ilya.gorbunov
01/11/2018, 7:57 PM::f.partial(b = value)
orangy
receiver::f(_, _, value, _, complex.expression())
, or with the use of named arguments receiver::f(paramName = value)
. I don’t see a good syntax for unbound receiver though, because ::f
is already reserved for functions without receivers.cedric
01/11/2018, 11:19 PMclass Student(name: String, age: Int)
fun createYoungStudent(name: String): (String) -> Student = { Student(name, 18) }
dalexander
01/12/2018, 4:20 PMfooObject::fooFunction
is the same as { p1, p2, p3 -> fooObject.fooFunction(p1, p2, p3) }
so
fooObject::fooFunction("value1", "value2", _)
is the same as { p3 -> fooObject.fooFunction("value1", "value2", p3) }
If we want a general FooObject::fooFunction with the first two parameters applied but not bound to an object then
FooClass::fooFunction("Hello", "Functional", _)
would be the same as FooClass.(String) -> Unit = { p3 -> this.fooFunction("Hello", "Functional", p3) }
I haven’t seen this mentioned, but one of the major advantages for having language support for PA would be the preservation of parameter names (and parameters with default values?), which tend to be lost with library-provided PA as far as I’ve seen.
I don’t know how frequently I would use this feature in Kotlin, but it seems like it would fit reasonably well with the rest of the language, so I’d like to see examples or edge cases where there would be issues.raulraja
01/12/2018, 7:19 PMdef
for fun
😉raulraja
01/12/2018, 8:44 PMfun javaApi() = run { effect() }
or something along those linesorangy
Ruckus
01/12/2018, 8:55 PMcedric
01/13/2018, 5:14 AMandyfleming
01/16/2018, 7:56 AMalex.hart
01/16/2018, 2:09 PMcedric
01/16/2018, 2:17 PMmyList.last
?snrostov
01/25/2018, 9:22 AMclass Builder<T> {
operator fun <V> getProperty(key: KProperty1<T, V>): V
operator fun <@kotlin.internal.OnlyInputTypes V> setProperty(key: KProperty1<T, V>, value: V)
}
class MyUser(val name: String, val age: Int)
fun buildUser() {
val userBuilder = Builder<MyUser>()
userBuilder.name = "test"
userBuilder.age = 132
// should be compiled to:
// userBuilder.setProperty(MyUser::name, "test")
// userBuilder.setProperty(MyUser::age, 132)
// @OnlyInputTypes is required to disallow userBuilder.name = 123, since R in KProperty1 is contravariant
}
Another use case is LINQ:
class Expression<T> {
operator fun <V> getProperty(key: KProperty1<T, V>): Expression<V>
}
fun Expression<T>.equals(other: Expression<T>): Expression<Boolean> = TODO()
fun Expression<T>.greater(other: Expression<T>): Expression<Boolean> = TODO()
fun Expression<Boolean>.and(other: Expression<Boolean>): Expression<Boolean> = TODO()
inline fun <reified T> query(predicateProvider: Expression<T>.() -> Expression<Boolean>) = TODO()
fun makeQuery() = query<MyUser> { (name == "test") and (age greater name.size) }
// lambda should be compiled to:
// (getProperty(MyUser::name) == "test") and (getProperty(MyUser::age) greater getProperty(MyUser::age).getProperty(String::size))
Second use case may be also covered by expression trees, but first likely not.