Justin Fiedler
08/09/2021, 2:26 PMGaurav Singh
08/09/2021, 6:06 PMPriNova
08/09/2021, 9:21 PMnapperley
08/10/2021, 2:24 AMinterface Closable {
val isClosed: Boolean
fun close()
}
class ResourceX : Closable {
val isClosed: Boolean
get() = _isClosed
private var _isClosed = false
fun close() {
if (!_isClosed) {
// Do cleanup here.
_isClosed = true
}
}
fun operation() {
if (_isClosed) throw IllegalStateException("ResourceX is closed")
// Do stuff related to the operation here.
}
}
ephemient
08/10/2021, 3:59 AMfun f(l: MutableList<String>) {}
@ExperimentalStdlibApi
val l: List<Any> = buildList {
javaClass
f(this)
}
Did something change with builder inference?Florian
08/10/2021, 10:41 AMInt?
into a String?
but if the Int?
is null
I don't want to have a literal "null"
String, I want the string value to be null
. Is there an operator for this or do I have to check it manually?emwy
08/10/2021, 12:20 PMvar a = 2
private set
we could write something like
local var a = 2
Jeremy Soles
08/10/2021, 4:58 PMRahul Mishra
08/10/2021, 5:02 PMAsq
08/10/2021, 9:12 PM1: val aut = setOf(2, 3, 1, 4, 0)
2: print("$aut")
3: val aux2a: List<Int> = aut.dropWhile{ 2 < it }
4: a2.containsAll(aux2a) shouldBe true
5: (aux2a.size < a2.size) shouldBe true
fails (i.e. (aux2a.size < a2.size)
is false
) at line 5. the documentation for dropWhile
says :
Returns a list containing all elements except first elements that satisfy the given predicate.
I would have expected that at least item 3
(from line 1) xor item 4
would have been dropped. Perhaps both under certain circumstances (ordering is not a property of Set
). What is the expected behavior of dropWhile
for a Set?Ubinyou
08/11/2021, 2:48 AMJavier
08/11/2021, 7:49 AMthana
08/11/2021, 9:32 AMfun foo(): Boolean {
val bool = isConditionMet()
if (bool) {
log(…)
}
return bool
}
Mark
08/11/2021, 12:58 PMif
condition (at least 3 branches) where the checks are things like Build.VERSION.SDK_INT > 12
and then branch depends on such a later SDK level. How to suppress this warning?smit01
08/11/2021, 5:14 PMBart
08/11/2021, 7:18 PMshama
08/11/2021, 9:22 PMvar firstName: String = "Charlie"
var lastName: String = "Brown"
val fullName: String by lazy {
"$firstName $lastName"
}
println(fullName) // Charlie Brown
firstName = "Snoopy"
// Let "fullName" know it will need to recompute the next time it is accessed
println(fullName) // Snoopy Brown
I’m currently going down writing my own ReadWriteProperty
for it but wondering if there is an existing pattern for this already? Thanks!Florian
08/11/2021, 11:49 PMval showSelectNewTaskConfirmationDialog = newTask != null
if (showSelectNewTaskConfirmationDialog) {
// smart cast for newTask here not working
[...]
}
franztesca
08/12/2021, 8:08 AMinterface Counter {
fun add()
fun remove()
}
// We have some interfaces extending counter
interface MyCounter : Counter {
fun doStuff()
}
interface MyOtherCounter : Counter {
fun doOtherStuff()
}
// Implements counter by wrapping a counter and calling add/remove twice.
class DoubleCounter(val baseCounter: Counter) : Counter {
override fun add() {
baseCounter.add()
baseCounter.add()
}
override fun remove() {
baseCounter.remove()
baseCounter.remove()
}
}
// These two are okay, but very boilerplate and non-scalable
fun MyCounter.double(): MyCounter {
val double = DoubleCounter(this)
return object : MyCounter by this {
override fun add() = double.add()
override fun remove() = double.remove()
}
}
fun MyOtherCounter.double(): MyOtherCounter {
val double = DoubleCounter(this)
return object : MyOtherCounter by this {
override fun add() = double.add()
override fun remove() = double.remove()
}
}
// This is illegal, is there any way to do this or similar?
inline fun <reified T: Counter> T.double(): T {
val double = DoubleCounter(this)
return object : T by T {
override fun add() = double.add()
override fun remove() = double.remove()
}
}
K Merle
08/12/2021, 12:46 PMwhen (result) {
Result.Loading -> {}
is Result.Success -> SomeScreen(data= (dataResult as Result.Success<List<SomeData>>).value)
is Result.Error -> {}
}
How do I prevent casting type on Success?
My Result class
sealed class Result<out T> {
object Loading : Result<Nothing>()
data class Error(val message: String) : Result<Nothing>()
data class Success<T>(val value: T) : Result<T>()
}
Alina Dolgikh [JB]
althaf
08/13/2021, 1:17 PMenum class Role(val letter: Char) {
APPROVER('U'),
ADMIN('D'),
INQUIRER('R'),
OPERATOR('O');
}
we have create this enum for code readability and we don't have direct control over how server is returning constants for the user roles, so they have done it this way 'U' , R etc
apiRespose.role = "U"
Role.valueOf(apiResponse.role) will not work ,as it is expecting 'APPROVER' as input . How can i achieve same benefit of Role.valueOf( ) wrt ot the 'letter' attribute in the enumheisenberg
08/13/2021, 1:42 PMfun foo(name: String) {
lastCallMethod = ::foo
}
Now
I want to call the method lastCallMethod
May I ask if there is a way to retrieve the name
String that pass using reflection?althaf
08/14/2021, 4:20 PMfun String?.throwOnEmptyOrNull(emessage: String, exceptionClass: Class<out Exception>): String {
return this.takeIf { !it.isNullOrBlank() } ?: throw exceptionClass.newInstance()
}
val ss : String? = null
println("test 1 ${ss.throwOnEmptyOrNull("roles can't be empty or not", IllegalArgumentException::class.java)}")
This is working for me, however i'm stuck at passing the message to exceptionJoshua F
08/15/2021, 8:21 AMErik
08/15/2021, 8:38 AMkotlin
and then at the REPL prompt I type (-1).toUInt()
or 1u - 2u
, then the result is kotlin.UInt = -1
. Is that expected? kotlin.UInt
ranges from 0 to 2^32-1, but can it be rendered as negative too somehow? Or is kotlin.UInt = -1
actually equal to 2^32-1
?Jason5lee
08/15/2021, 10:27 AMCicero
08/15/2021, 11:37 AMStrum355
08/15/2021, 12:47 PMMark
08/16/2021, 4:02 AM// common module
interface Brand {
val foo: String // plus lots more properties and funs
// implement Brand so usage like Brand.foo instead of Brand.INSTANCE.foo
companion object : Brand {
// this must be set very early in Application.onCreate()
// why not use DI? Because sometimes we need access to Brand before DI has finished setting up
lateinit var INSTANCE: Brand
override val foo: String
get() = INSTANCE.foo
}
}
// brand module (dependency on common module)
// different brand modules for different product flavors
object ActualBrand : Brand {
override val foo: String
get() = "actual brand"
}
fun Brand.Companion.init() {
INSTANCE = ActualBrand
}
// app module (dependencies on brand and common modules)
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
Brand.init() // note: we do not need to hardcode ActualBrand
// proceed to DI initialisation
}
}