mg6maciej
01/24/2017, 8:41 PMnstewart
01/24/2017, 8:43 PMalso
. It would be nice if there were something similar for with
so that nested with
statements could use named receivers instead of this
fellshard
01/24/2017, 8:53 PMAndreas Sinz
01/24/2017, 9:08 PMilya.gorbunov
01/24/2017, 9:08 PMwith
with named receiver is just let
nstewart
01/24/2017, 9:12 PMlet
in that way. All examples I’ve seen of it are used to better deal with nullable argumentsilya.gorbunov
01/24/2017, 9:16 PMilya.gorbunov
01/24/2017, 9:22 PMilya.gorbunov
01/24/2017, 9:25 PMcedric
01/24/2017, 9:53 PMval value: Int = map.getValue("key")
Have you considered getValueOrThrows()
? Would make the intent a bit more obvious IMO (arguably carried by the non-nullable return type, but still useful in a language with no checked exceptions)kevinmost
01/24/2017, 10:22 PM.takeIf { ... }
makes me think it might be useful to have a .orElse { ... }
or similar that is basically a function version of the Elvis operator. Would be useful if you're chaining more methods afterwards because with an Elvis, you'd have to wrap the expression from the very start up to after the Elvis in parens, which can get kind of messy
Kind of a contrived example, but:
val lastTransactionId = api.getUser("userId")
.takeIf { it.transactions.isNotEmpty() }
.orElse { error("user has no transactions") }
.transactions.last.id
vs
val lastTransactionId = (api.getUser("userId")
.takeIf { it.transactions.isNotEmpty() }
?: error("user has no transactions"))
.transactions.last.id
Thoughts?kevinmost
01/24/2017, 10:25 PMinline fun <T> T?.orElse(ifNullBlock: () -> T): T = this ?: ifNullBlock()
cedric
01/24/2017, 11:09 PMreturn
work in the orElse{}
block? That’s one of the main appeals of the ?:
current approachkevinmost
01/24/2017, 11:09 PMorElse
is inline, right?kevinmost
01/24/2017, 11:12 PMdamian
01/24/2017, 11:15 PMkevinmost
01/24/2017, 11:17 PMdamian
01/24/2017, 11:18 PMval x = y ?: return
, x
is either y's type (non-null) or Nothing
kevinmost
01/24/2017, 11:19 PMy
is of type T?
, then x
is inferred to be of type T
, right?damian
01/24/2017, 11:19 PMdamian
01/24/2017, 11:19 PMdamian
01/24/2017, 11:19 PMkevinmost
01/24/2017, 11:19 PMdamian
01/24/2017, 11:19 PMkevinmost
01/24/2017, 11:19 PMmk
01/25/2017, 10:12 AMAny
(like also
) pollute the namespace of any object with new symbols, so those that mostly cover corner cases rather than universally useful concepts should have really really good reasons to be added. I looked at https://youtrack.jetbrains.com/issue/KT-6903 and I'm wondering on which basis the decision was actually made to introduce also
? I mostly find discussion around naming rather than a proper rationale for why we need to have all these subtle flavors of let
in the standard library.
I think additions with such reach should be considered "guilty by default" and only made if there is a vast majority of cases proving they're needed. Maybe I'm missing context to the discussion, just wanted to offer another point of view to this.mk
01/25/2017, 10:15 AMAny
, whereas Object
defines methods related to threading on top of that, something which I understand has been pointed out as a mistake not to be repeated. Isn't defining extensions on Any going doing the same path?kirillrakhman
01/25/2017, 2:26 PMwindow
not making it in 1.1 is a shameilya.gorbunov
01/25/2017, 2:37 PMkirillrakhman
01/25/2017, 2:43 PM