lionel
12/31/2018, 1:31 PMghedeon
01/17/2019, 9:47 PMfun main(){
runBlocking{
with(First()){
with(Second()){
with(Third()) {
foo().zip(bar()).zip(foo()) // fails because of name conflict
}
}
}
}
}
class First(){
fun CoroutineScope.foo():ReceiveChannel<Int>{}
}
class Second(){
fun CoroutineScope.bar(): ReceiveChannel<Int>{}
}
class Third(){
fun CoroutineScope.foo(): ReceiveChannel<Int>{}
}
Deep nesting plus name conflicts. How do you tell the difference between the two methods with the same name?ghedeon
01/17/2019, 9:49 PMthanksforallthefish
01/22/2019, 1:24 PMfun asString(): (Any) -> String = Any::toString
I can call it in 2 ways
- asString()(anObject)
- asString().invoke(anObject)
I believe the first way would be more idiomatic, but coming from java I still prefer the second (()()
looks too weird to me). For the sake of who will come after me, which version would you suggesting to use?Marc Knaup
02/05/2019, 10:05 PMdavid.bilik
02/07/2019, 8:30 AMXXSubject
from RxJava that you init with default value .. but then you want to access this value
elsewhere in code, but its marked as Nullable
so you have to treat it like so in Kotlin as well, so if I want to access it, i should check for null value, but in this case, I know that its not null, because I’ve put default value there. Whats the best practice here? Lately we (at our company) are trying to get rid of all unnecessary !!
and we treat them like code smell, but here its completely legit, but still feel kind of wrong. I can make extension on Subject as in android framework something like requiredValue
that will perform !! for me and I will not see it in my code, but is it worth it? thanks!Slackbot
02/14/2019, 8:43 AMHullaballoonatic
04/11/2019, 8:15 PMval isFoo: Boolean get() = //foo things
fun isFoo(): Boolean = //foo things
Does the get
imply inexpensive and static? Does the fun
imply expensive and dynamic?
I prefer to have functions only be verbs. So in practice i'll write the above always with getters, but I wonder if that's confusing or abusive. It could lead someone to constantly grab a property that has to be computed each time, e.g.
class Matrix(private val data: List<Vector>) {
val rows get() = data
val cols get() = List(numCols) { j -> Vector(List(numRows) { i -> data[j][i] }) }
}
Wouldn't this imply that
fun foo() {
for (row in rows) {
// things
rows.otherThings()
}
}
would take the same time to compute as
fun bar() {
for (col in cols) {
// things
cols.otherThings()
}
}
Hullaballoonatic
04/17/2019, 7:51 PMfor
loop vs forEach
lambda ?Cody Engel
04/18/2019, 3:34 PMOffsetDateTime
where I’d be able to do something like OffsetDateTime.todayAtMidnight
to get the time for today at midnight. If I could do an extension static function on that class then my problem would be solved but alas I don’t think you can do that.
Would the best option be to just use the factory pattern and have a OffsetDateTimeFactory
that’s just a collection of ways to create OffsetDateTime
instances for specific cases?Hullaballoonatic
04/24/2019, 2:47 AMfun Foo.act(with: Bar) = //
The most when paired with
foo.act(with=bar)
thiagoretondar
05/01/2019, 12:18 PMEventFrom
only exists in this context of TrackInfoDTO
. Or should I create another file only for EventFrom
?
data class TrackInfoDTO(
val url: String,
val userID: String,
val eventFrom: EventFrom,
val UTM: Map<String, Any>
) {
data class EventFrom(
val parentElementName: String,
val elementName: String,
val CTCurrent: String,
val payload: Map<String, Any>
)
}
rocketraman
05/10/2019, 3:25 PMAlex
05/16/2019, 5:20 PMinterface A
interface B
class ABImpl(concreteA: A): A by concreteA, B
now say I want a composite interface instead that encapsulates A & B
interface ABComposite: A, B
Then my ABImpl becomes
class ABImpl: ABComposite
If I want to delegate A only, but not B
my solution was to do
class ABImpl(concreteA: A): ABComposite, A by concreteA
Is that the correct way? It just looks slightly odd, but if that’s how you do it all good 🙂Hullaballoonatic
05/20/2019, 6:28 PMfun Double.squareRoot() = pow(0.5)
fun Double.sqrt() = squareRoot()
Florian
05/20/2019, 6:45 PMdarkmoon_uk
05/21/2019, 2:11 PMfun <T,R> Iterable<T>.firstMappedOrNull( mapping: (T)->R? ) : R? {
for(element in this) {
mapping(element)?.let { return it }
}
return null
}
Hullaballoonatic
06/09/2019, 6:13 PMwhen
for just two cases for clarity? e.g.
enum class Foo {
FOO, BAR
}
fun foo(foo: Foo): Int = when(foo) {
FOO -> "foo"
BAR -> "bar"
}
or is this basically a no-no, because an single if
else
can handle it? e.g.
fun foo(foo: Foo) = if (foo == FOO) "foo" else "bar"
BMG
06/10/2019, 10:04 AM@ExperimentalContracts
fun hasChildren(region: Region): Boolean {
contract {
returns(true) implies (region.regions != null)
}
return regions?.isNotEmpty() ?: false
}
Not sure why this doesn’t work.. Smart contracts only works with values passed as parameters?Florian
06/13/2019, 11:28 AMDavide Giuseppe Farella
06/16/2019, 8:55 AMsealed class MyRequest
class MySyncRequest
class MyAsyncRequest
But I use lower case when the file contains generic elements that can not be represented by a single class:
requestExtensions.kt
fun MyRequest.doSomething() = ...
fun MyRequest.doSomethingOther() = ...
Florian
06/16/2019, 9:50 AMHullaballoonatic
06/27/2019, 2:05 AMval foo = bar ?:
3 + 6 + 9 + 2 +
8 + 12
val baz = a && b && c
&& d && e && f
Florian
06/28/2019, 7:09 PMarekolek
07/04/2019, 7:59 PMwhen
and matching an object
from a sealed
class:
sealed class Foo {
object Bar : Foo()
data class Baz(val x: Int) : Foo()
}
fun usingEquality(foo: Foo) = when (foo) {
Foo.Bar -> 1
is Foo.Baz -> 2
}
fun usingInstanceOf(foo: Foo) = when (foo) {
is Foo.Bar -> 1
is Foo.Baz -> 2
}
Any reason to prefer 1️⃣ usingEquality
rather than 2️⃣ usingInstanceOf
? IDE generates usingEquality
, but doing a quick benchmark shows no difference, yet is Foo.Bar
is more useful, in case you decide later to make Bar
a data class
for example. What do you use?Florian
07/04/2019, 8:15 PMdarkmoon_uk
07/25/2019, 11:10 AMMike
07/25/2019, 11:31 AMI
prefix, or Impl
suffix shouldn't be necessary.
The interface should describe attributes/features in a general sense, and the implementation should be concrete. The only time they truly overlap is if there's only one implementation of the interface. Commonly seen when a project also uses IOC/DI.tjb
08/08/2019, 10:37 PMlet
all the time?tjb
08/08/2019, 10:42 PMuserRepository.findByEmail(assignee)
being able to be null from the database