voddan
07/22/2017, 8:06 AMgabrielfv
08/14/2017, 9:48 PMfun foo(bar: Int?)
can be called like foo()
, instead of having to write fun foo(bar: Int? = null)
. I see people comonly reading the ?
as sort of "opitional" in this context, and I don't see the point of calling foo(null)
, though makes some sense when
val a: Int? = mayReturnInt()
foo(a)
Is a common scenariodavid.bilik
08/16/2017, 7:20 AMalso
vs apply
usages. We have a method that needs to return some type but I would like to store instance of returned type to member property, something like
lateinit var view : View
fun createView(context : Context) : View {
return with(context) {
view = frameLayout {
}
}
}
this example would not compile because return type of view = frameLayout is unit. The variants to solve that are
fun createView(context : Context) : View {
return with(context) {
view = frameLayout {
}
view
}
}
fun createView(context : Context) : View {
return with(context) {
view = frameLayout {
}.apply { view = this }
}
}
fun createView(context : Context) : View {
return with(context) {
view = frameLayout {
}.also { view = it }
}
}
I prefer the also
variant because it seems that this operator was made for this usages “do something also with this expression” and apply
is for cases when i would like to modify instance in its context. Any opinions?Slackbot
09/04/2017, 9:22 PMlovis
09/05/2017, 8:23 AMbj0
09/09/2017, 2:21 AMlist?.isNotEmpty() == true
karelpeeters
09/09/2017, 3:22 PMnull
-related problems in Java is to return a "default value", and this works especially well with collections, there's rarely a reason for them to be null. Just make your mehods return empty collections instead.Dmitry Kandalov
09/15/2017, 10:08 AM+
on new lines as @gabrielfv suggested:
foo("one"
+ "two"
+ "three")
only to find that it doesn’t work with return
, e.g. the following doesn’t compile because of +
on new line:
fun bar(): String {
return "one;"
+ "two;"
+ "three"
}
I’d argue this means that +
and .plus()
are already different enough to have different formatting 🙂 /cc @kevinmostdimsuz
09/15/2017, 3:26 PMvlastachu
09/20/2017, 4:33 PMCPTViewModel(
selectionType = null,
taskName = task.displayName,
projectName = projectInfo.project.name,
customerName = projectInfo.customer.name,
isTaskVisible = true
)
to
CPTViewModel(
selectionType = null,
taskName = task.displayName,
projectName = projectInfo.project.name,
customerName = projectInfo.customer.name,
isTaskVisible = true
)
dimsuz
09/25/2017, 11:19 AMgildor
09/27/2017, 5:33 PMval url = it.imageUrl ?: return null
val link = it.resizerlink ?: return null
val width = it.imageWidth ?: return null
val height = it.imageHeight ?: return null
return Image(url, link, width.toInt(), height.toInt())
gabrielfv
10/11/2017, 7:33 PMval a = foo.computeBar(Baz::class.java).fetchFirst() ?:
return@thisComputationFun
or
val a = foo.computeBar(Baz::class.java).fetchFirst()
?: return@thisComputationFun
or none at all? The IDE has recomended me to do this in place of the classic if (a == null) return@thisComputationFun
miha-x64
10/12/2017, 4:31 PMval (
distributor,
items,
shippingMethods,
addresses,
preferredAddressId
) = argument
I propose:
val (
distributor,
items,
shippingMethods,
addresses,
preferredAddressId
) = argument
michaelsims
10/22/2017, 5:31 PMmainThreadScheduler.executeOnThread(Runnable {
result.errorResult.let {
if (it == null) {
taskCallback.onSuccess(result.successResult)
} else {
taskCallback.onFailure(it)
}
}
})
yole
10/23/2017, 7:51 AMraulraja
10/24/2017, 11:58 PM)
is what I find more readableuhe
10/25/2017, 8:08 AM) {
on the same line as the last property because it's just a wasted line otherwiseraulraja
10/25/2017, 10:02 AMmarcinmoskala
10/25/2017, 3:33 PMbamdmux
10/26/2017, 5:27 AMSean Corfield
10/27/2017, 6:02 PMzredna
11/06/2017, 2:14 PMyole
11/14/2017, 1:34 PMliminal
11/19/2017, 2:18 PMfun isValid(): Boolean = noteText.isNotEmpty() && noteText.isNotBlank() && noteText.length > 3
to avoid repeating noteText several times? thanksenleur
11/19/2017, 2:19 PMmarcinmoskala
11/21/2017, 1:49 PMmarcinmoskala
11/21/2017, 1:49 PMmarcinmoskala
11/21/2017, 1:52 PMSlackbot
11/21/2017, 1:58 PMSlackbot
11/21/2017, 1:58 PMyole
11/21/2017, 2:18 PMmarcinmoskala
11/21/2017, 2:24 PMyole
11/21/2017, 2:25 PMmarcinmoskala
11/21/2017, 2:59 PMval gson = Gson()
or like that:
val GSON = Gson()
?Andreas Sinz
11/21/2017, 3:06 PMmarcinmoskala
11/21/2017, 3:48 PMAndreas Sinz
11/21/2017, 10:07 PMNames of constants
. properties that hold data
probably can be confusingmarcinmoskala
11/22/2017, 6:15 AMyole
11/22/2017, 8:32 AMmarcinmoskala
11/22/2017, 3:11 PMyole
11/27/2017, 3:14 PM