Burkhard
08/23/2019, 2:41 AMtrimMargin
, trimIndent
at compile time, see https://youtrack.jetbrains.com/issue/KT-17755
I thought this would also allow for const strings using that. I’m guessing this will be the next step.Leon K
08/23/2019, 6:04 PMnullable
types, which actually are a big reason why Kotlin is loved so much.
but as soon as more complex Problems come into play, we have to go to rather ugly exception-style code, or use Either
/ Try
types from libraries like arrow-kt ( or create them ourselves)
while using Try
and either
types isn't a big problem by itself, it quickly looses a huge amount of Kotlins consiseness, because it makes you write
kt
val someTry: Try<List<Int>> = someTryReturner()
val result = someTry
.map { it.map { it * 2 } }
.map { it.sum() }
.map { "result is ${it.toString()}" }
.getOrElse { exception -> "There was an error: ${exception.message!!}" }
println(result)
This would be a lot easier when written with the nullable type:
kt
val someNullable: List<Int>? = someNullableReturner()
val result = someNullable
?.map { it * 2 }
?.sum()
?.let{ "result is ${it.toString()}" }
?: "There was an error, but i don't know what kind of error D:"
println(result)
Here were obviously missing out on the error information.
because of this, i propose to support a Result<E, V>
-like type, that behaves like the already existing nullable. my proposed syntax would look like this:
kt
val someExceptional: List<Int>?? = someExceptionalReturner()
val result = someExceptional
??.map { it * 2 }
??.sum()
??.let{ "result is ${it.toString()}" }
?: { exception -> "There was an error: ${exception.message!!}" }
println(result)
this would make code a lot more readable, and could strongly help to encourage developers to actually handle their errors properly.
one additional thing that should be included with this would be a runExceptional
function, that takes a lambda-block that could throw an exception and turns it into an exceptional type.
another possibility would be to actually have a more terse way of turning a throwing function into an exeptional:
fun thisThrows(): String {
throw SomeException()
}
val someExceptional: String?? = thisThrows??()
the questionmarks could directly turn any function that could throw into an exceptional-type. (this syntax is not possible, as it would be ambiguous in situations where you had a (() -> T)??
value that gets executed, but there should be a solution to that problem.katz
08/26/2019, 11:18 AMtryOrElses
-> val t = try{...}
<- null on fail val t = try{...} else X
val t = try{} ?: X
elect
08/26/2019, 12:20 PMinit
without body, eg: init = bar()
salomonbrys
08/28/2019, 8:29 AMAndy Victors
08/29/2019, 9:44 PMif (c) a else b
) than the classic one (a ? b : c
) it's still a branching (#if-considered-harmful), it's awkward to write and it is hard to use with nesting. Why not to provide a functional programming style operator eitherOr(c, a, b)
, implemented as a function:
fun <T>eitherOr(condition: Boolean, first: T, second: T) : T {
return if (condition) first else second
}
After all, we already have things like min(a,b)
, `or(a)`etc.DALDEI
09/10/2019, 4:41 AMjmfayard
09/17/2019, 5:20 AMjmfayard
09/17/2019, 5:21 AMval (my, destructuring, declaration) = with(object) { Tuple(my, destructuring, declaration)
It’s stil position based obviously since this is what Kotlin support.
But in this case it doesn’t matter because the order of arguments is declared in line, not in another class.
It’s also much more general, it works with much more than “the properties of data classes”. We can put whatever we like in the lambda and transform it to a tuple.
Update: https://youtrack.jetbrains.com/issue/KT-19627elect
09/26/2019, 9:09 AMnull
, could kotlin do the same?stantronic
09/27/2019, 4:34 PMDerek Peirce
10/06/2019, 8:45 PMby
on constructor arguments for delegation.
class Foo(val bar by Lazy<Bar>) { ... }
would be syntactic sugar for
class Foo(barProvider: Lazy<Bar>) {
val bar by barProvider
...
}
This pattern appears over a thousand times in the codebase I'm working on, and cutting out the declared barProvider
middle-man would make switching between Bar
and Lazy<Bar>
extremely simple.stantronic
10/14/2019, 1:02 PMval mylist : MutableList<Thing> exposedas List<Thing>
- keeping the mutable functionality private to the class, but exposing the immutable interface. What do you think?jdemeulenaere
10/15/2019, 11:52 AMBooleanExpression
object than can be returned by some call, e.g. myFutureValue.isGreaterThanOrEquals(aStaticValue)
. It would be nice if instead I could write that using myFutureValue >= aStaticValue
zak.taccardi
10/18/2019, 4:13 PMMyViewModel
in one file, and MyViewModel.State
in another file?Bob Glamm
10/23/2019, 4:12 PMfun Pair<A, B>.flip(): Pair<B, A> = Pair(second, first)
to standard libraryHullaballoonatic
10/25/2019, 3:42 AM@package:Suppress("unused")
useful in spring, for examplejdemeulenaere
10/25/2019, 12:56 PMReadWriteProperty#setValue
so that we could set a variable from different types. Something like this:
import kotlin.reflect.KProperty
interface Named {
val name: String
}
class MyClass {
var name by NameDelegate("foo")
}
class NameDelegate(private var name: String) {
operator fun getValue(thisRef: MyClass, property: KProperty<*>): String {
return name
}
operator fun setValue(thisRef: MyClass, property: KProperty<*>, value: String) {
name = value
}
operator fun setValue(thisRef: MyClass, property: KProperty<*>, value: Named) {
name = value.name
}
}
fun main() {
val namedImpl = object : Named {
override val name: String = "bar"
}
val myObject = MyClass()
myObject.name = "bar" // this compiles
myObject.name = namedImpl // this does not compile
}
Hullaballoonatic
10/27/2019, 8:42 PMfun <K, V> MutableMap<K, V>.putAll(vararg pairs: Pair<K, V>): Unit
kinda strange it's not in the stdlib already...?Hullaballoonatic
10/29/2019, 7:06 PMdata class Foo(val foo: Int, val bar: Int)
val foo = mapOf("foo" to 1, "bar" to 2) as Foo
Hullaballoonatic
10/31/2019, 8:05 PMthis
argument for when statements:
fun Any?.isFalsy = when {
null -> true,
etc
}
louiscad
11/04/2019, 9:25 AMwhen (stuff)
to allow just calling methods, adding the receiver to the branch conditions, or have stuff.when
do that as a new special syntax. (Thinking about how it would translate to actual uses cases, I think I prefer that latter approach)Alpesh Vas
11/07/2019, 6:44 PMnwh
11/12/2019, 12:21 AM/**
* @copy Foo.bar()
*/
For methods that do almost identical things but take slightly different parameters. For different params, a @param
tag on the method would override whatever is present on the comment being copied fromQuy D X Nguyen
11/17/2019, 7:20 PMvoddan
11/20/2019, 10:37 AMLiveData
(a kind of observable):
private val _volume = MutableLiveData(0)
public val volume: LiveData<Int> = _volume
Example with lists:
private val _users = mutableListOf(user1)
public val users: List<User> = _users
Are there any ideas that are on the table that would simplify those patterns or allow them to be encapsulated with property delegates?stantronic
11/20/2019, 10:47 AMprivate val users = MutableLiveData(0)
exposeas LiveData<User>
victor-mntl
11/20/2019, 6:02 PMfun main(args: Array<String>)
function is permited to be simplified as fun main()
(when the entry arguments are not used).
My suggest is that, when the arguments of the entry main
function ARE used, the IDE/Compiler may accept just fun main(args)
, assuming the type of args
would be Array<String>
.elect
11/21/2019, 12:13 PM!!
) we shall also have it after .?
louiscad
11/21/2019, 5:36 PMkotlinx.coroutines.invoke
for CoroutineDispatcher
without having to use either of these two inconvenient options:
1. Adding @UseExperimental(ExperimentalCoroutinesApi::class)
at each use-site
2. Opting-in for all @ExperimentalCoroutinesApi
and being bitten later when other experimental APIs change that I didn't see coming.
I'd want to be able to opt-in for these in Gradle config (or compiler config) in a way or another, I can search for ideas on how to define it.
The fully qualified name (or a pair of imports + symbol names) would be enough, opting-in for all functions overloads, in a KDoc fashion.