louiscad
06/08/2020, 7:07 PMalllowercasepackagenames
from Java? What was the rationale? I'm about to name a package in camelCase because it's much more readable that way, but I'm wondering why I should follow that specific convention.
EDIT: Same question for underscores (_
) in package names.Ellen Spertus
06/08/2020, 10:27 PMif (intent.getStringExtra(SUGGESTION_KEY) != null) {
suggestionButton.text = intent.getStringExtra(SUGGESTION_KEY)
suggestionButton.visibility = View.VISIBLE
} else {
suggestionButton.visibility = View.INVISIBLE
}
I can’t write the following, because one arm returns a String
and the other Unit
:
intent.getStringExtra(SUGGESTION_KEY)?.let { suggestion ->
suggestionButton.text = suggestion
suggestionButton.visibility = View.VISIBLE
} ?: suggestionButton.visibility = View.INVISIBLE
(I know there is a function intent.hasStringExtra()
, but I’d like a general solution not specific to intents.)
How should I rewrite the original block of code, or is it best as is?jeggy
06/09/2020, 9:50 AMthana
06/09/2020, 10:07 AMclass Foo<T> (val helper1: Helper1<T>, helper2: Helper2<T>)
. here i want Helper1
and Helper2
work on the same type T
but in fact Foo
doesn't really care about what T
actually is - they just must match.
Is there a way not having to declare the T
on Foo
itself? I don't want to write something like Foo<*>
everywhere i'm using Foo
Or maybe the better question: i sense there is some design flaw here but i cannot really put the finger on. What approaches are there, to resolve this flaw?dead.fish
06/09/2020, 10:18 AMKClass::sealedSubclasses
is not only part of the API of Kotlin 1.3, but also of it’s implementation, but apparently this needs kotlin-reflect
. Now I’m on a constrained device and would rather not add a 2.7MB lib just for that. Are there alternatives (beside going back to a good old enum class
)?LastExceed
06/09/2020, 11:03 AM::main
in line 4 gives a an overload resolution ambiguity. I understand why it happens, but how do i resolve it? (i'd like to know the solution to both scenarios)iex
06/09/2020, 4:09 PMResult
?Twferrell
06/09/2020, 4:52 PMColton Idle
06/09/2020, 11:59 PM@Module
class NetworkModule constructor(@Inject public var url: String)
but when I look at the decompiled code in java I see:
public final class NetworkModule {
@Inject
@NotNull
private String url;
This is the error I'm trying to figure out "error: Dagger does not support injection into private fields
private java.lang.String url;"
Not necessarily a dagger issue, but somehow creating a private field in java even though I specify it to be public in kotlin?xii
06/10/2020, 12:11 AMMark
06/10/2020, 4:11 AMfor (value in map.values) { … }
2. for ((_, value) in map) { … }
GarouDan
06/10/2020, 12:02 PMcontinue
keyword that should become ``continue`` when I escape.LastExceed
06/10/2020, 3:32 PMlistOf("banana").map { it to 42 }.toMap()
is there an easier way to do this ?
basically i want to turn a list into a map by providing a lambda that associates each element from the list with a value to form a key-value pairiex
06/10/2020, 5:53 PMPair<T, T>
(or tuple in general), whose elements are case classes? (I need to pattern match the subtypes of the sealed class)herlevsen
06/10/2020, 8:42 PMGarouDan
06/10/2020, 11:31 PM!
operator in Kotlin?
For example, if I have MyClass defined as:
MyClass() {
run() {
println("finished")
}
}
I’d like to create a situation where doing something like this:
val myObject = MyClass()
myObject!
is equivalent to
myObject().run()
the methods will be more complex since this is for a special library that I’m doing, but the simple idea is like this.
How can we define this operator? I think it is possible because we have the !!
one.Barddo
06/11/2020, 12:59 AMedenman
06/11/2020, 4:03 AMTimeMark.elapsedSince(other: TimeMark)
? or plans to add it?Dibya Ranjan
06/11/2020, 5:04 AM*As a rule of thumb, you should not be catching exceptions in general Kotlin code.* That's a code smell.
.
I see that I can make changes to my APIs to validate and return rather than throwing and catching an exception. However, I failed to come up with a better code for one scenario.
I have a Kafka consumer, which makes a call to a web API. so I have onListen(p: Person) -> orchestrator(p: Persons) -> makeRestCall(p: Person) -> API
the exception flow here as I would do in Java is to handle all non response codes 200s. If there is an error returned from the API, I throw and exception, the orchestrator catches it. Is this an anti pattern in Kotlin? I think I am not the first person to have this issue, it would be of great help if someone can give a direction here. Thank you.Stephen Prochnow
06/11/2020, 7:20 AMand(), or(), not()
over Boolean operations like &&, ||, !
. Thank you in advance for your answers.LastExceed
06/11/2020, 10:27 AMPhilipp Mayer
06/11/2020, 10:44 AMdata class SystemName(val value: String) {
init {
require(value == value.toLowerCase().capitalize()) { "Name $value must be capitalized!" }
}
Right now, when I create the Object I always check if its lowercase with the first letter being capital.
I'd like to let the object take care of .toLowercase().capitalize()
, so I don't have to do it on my own. Searching google gives me mixed results which don't look that clean tbh. What would be your take on this? Thanks ahead!
My current take is the following:
data class SystemName(val value: String) {
init {
require(value == value.toLowerCase().capitalize()) { "Name $value must be capitalized!" }
}
/**
* automatically converts string to required format
*/
companion object {
fun createInstance(name: String) = SystemName(name.toLowerCase().capitalize())
}
mplain
06/11/2020, 12:30 PMclass Matrix<N : Number>(private val rows: Int, private val columns: Int, f: (Int, Int) -> N) : List<List<N>>
by List(rows, { i -> List(columns) { j -> f.invoke(i, j) } }) {
Now I want to add a secondary constructor that would take an iterable (list or array) of elements, like this:
constructor(rows: Int, columns: Int, values: Iterable<N>) {
val iterator = values.iterator()
Matrix(rows, columns) { _, _ -> iterator.next() }
}
This doesn't work, says "call to primary constructor expected"
Any advice on how to address this problem, Kotlin-style?nicholasnet
06/11/2020, 7:20 PMid
property value of generic T
this is my code.
class BaseRepository(val client: DatabaseClient, val dataAccessStrategy: ReactiveDataAccessStrategy) {
suspend inline fun <reified T> save(entity: T): T {
val id = // How to get the id of entity?
try {
return client.insert()
.into(T::class.java)
.using(entity)
.map(dataAccessStrategy.converter.populateIdIfNecessary(entity))
.awaitOne()
} catch (exception: Exception) {
throw exception
}
}
}
What is the best approach to get the id of entity here?Shawn Karber_
06/11/2020, 10:45 PMdef is_ten(x) when x > 10, do: "Greater than ten"
def is_ten(x) when x < 10, do: "Less than ten"
I need to write a kotlin function that always takes a string, but the logic is completely different based on that string, is there a way to encapsulate logic into a function that is called based on the parameter value similar to elixir guard clauses?Oleg Siboglov
06/12/2020, 2:50 AMjava.lang.NullPointerException: Attempt to invoke virtual method 'float java.lang.Number.floatValue()' on a null object reference
can occur while iterating over a MutableList<Float
. All Float
values that are added to the list are nonNull
; a Float
value is returned from one function (where the return type is Float
), used by a different function (where the parameter type is Float
), and then added to the MutableList<Float>
. Later I iterate over the list ( for (i in 0 until list.size) list[i]
) and it is here when the exception is thrown, when I’m getting the values from the list. I’ve only ever seen this happen once from crashlytics, but I cannot understand how it even happened once. Values are only added to the list at one point in my execution. After adding values to the list, and before iterating over the list, I may remove the last value in the list ( list.removeAt(list.size - 1)
). Looking at the decompiled code ( ((Number)this.list.get(index)).floatValue()
) tells me that the value at that index is null. Am I misunderstanding the stack trace? Could someone please clear this up for me?Victor Harlan Lacson
06/12/2020, 8:21 AMuser
06/12/2020, 11:12 AMGayan Perera
06/12/2020, 4:55 PMGemiDroid
06/12/2020, 7:56 PMGemiDroid
06/12/2020, 7:56 PMShawn
06/12/2020, 8:14 PMMatteo Mirk
06/15/2020, 10:25 AM