Colton Idle
04/26/2020, 6:14 PMmyobject.somethingInner.somethingNested.setter1 = true
myobject.somethingInner.somethingNested.setter2 = false
myobject.somethingInner.somethingNested.setter3 = false
That seems like a legitimate use case to me to not have to repeat so you can do
myobject.somethingInner.somethingNested.apply {
setter1 = true
setter2 = false
setter3 = false
}
I also read that this makes things thread safe. So while that sounds good. I don't know what that actually means. Is my first statement not thread safe?Adam Powell
04/26/2020, 6:27 PMmyobject.somethingInner.somethingNested
can change between each of those lines of code, you're setting values on different objectsAdam Powell
04/26/2020, 6:28 PMapply
for some reason, do a
val nested = myobject.somethingInner.somethingNested
nested.setter1 = true
nested.setter2 = false
// ...
and get the same effectDominik wuttke
04/26/2020, 7:03 PMval calendar = Calendar.getInstance()
calendar.set(2022,8,27)
val dayOfTheWeek = calendar.get(Calendar.DAY_OF_WEEK)
With Kotlin Standard functions you can set the value you want with initialization and immediately retrieve the value you want.
val dayOfTheWeek = Calendar.getInstance().apply { set(2022,8,27) }.get(Calendar.DAY_OF_WEEK)
Colton Idle
04/26/2020, 7:19 PMAdam Powell
04/26/2020, 7:22 PMdoStuffWith(calendar)
on a line of code before calendar
is fully initialized into a known good state without going out of their way and making the code look suspicious. Limiting the scope of object references helps you code defensively.Colton Idle
04/27/2020, 1:24 AMDavid Eriksson
04/27/2020, 9:17 AMdewildte
04/27/2020, 1:21 PMdewildte
04/27/2020, 1:32 PM.run
s over the use of .let
. Not using the ability to rename it
with something meaningful.David Eriksson
04/27/2020, 1:55 PMColton Idle
04/27/2020, 3:21 PM?let
, but it also works just fine if you ?apply
?also
?run
This is what I mean by added overhead. If I read any of those it would take me a second or two longer to read to make sure I understand it vs just using an if (thing != null) { }
. In that case I actually think the if statement is more expressive in terms of intent.Brian Dilley
04/27/2020, 3:28 PMit
variable when/if you’re doing any sort of nesting. And if you’re doing nesting… have a good reason.dewildte
04/27/2020, 5:57 PMdewildte
04/27/2020, 5:57 PMdewildte
04/27/2020, 5:58 PMdewildte
04/27/2020, 5:59 PMBrian Dilley
04/27/2020, 6:34 PMif (someMemberVariable != null)
doesn’t even prevent you from having to do someMemberVariable!!
within the if block since because it’s a member variable it’s value can change. So for me, this:
if (someMember != null) {
someMember!!.value = x
someMember!!.whatever()
}
isn’t as nice as:
someMember?.let {
it.value = x
it.whatever()
}
Brian Dilley
04/27/2020, 6:36 PMif (whatever == null)
makes sense. But other times you want to just change some state of a variable that may be null sometimes and it’s safe to ignore it when it’s null.