Alex Kuznetsov
04/19/2020, 7:32 PMTerry Franklin
04/20/2020, 12:43 AMclass AddressEntity(
var address1: String?,
var address2: String?,
var city: String?,
var country: String?,
var postalCode: String?,
var stateOrProvince: String?
) {...
to something like this -
class AddressEntity(
@Property var address1: String?,
@Property var address2: String?,
@Property var city: String?,
@Property var country: String?,
@Property var postalCode: String?,
@Property var stateOrProvince: String?
) {
nwillc
04/20/2020, 4:48 AMIvan Brko
04/20/2020, 2:34 PMfun getSomeString(): String?
and I have a function that wraps a String (not String?) in some Response
fun getResponse(msg: String): Response
then I have a third function in which I need to return a Response and I am calling the getSomeString, if it returns a String I need to feed it to getResponse, but if it returns null I need to return some ErrorResponse object.
Now, I wanted to make this a one liner, but I'm having problems with thinking how to do it
It would be great if I could do something like this:
fun GetResponse: Response =
when(getSomeString()) {
is String message -> getResponse(message)
else -> ErrorResponse()
}
but I can't do this is String message. In my real use-case this is not a String but a data class with a lot of fields, and I could match on all those fields and then recreate the object on the right side of -> but that just doesn't seem right.
How would this usually be done in Kotlin? I am used to some other languages where this approach of naming the String would be done in is String message ->...Amiedema
04/20/2020, 9:25 PMkenkyee
04/20/2020, 10:17 PMChills
04/21/2020, 6:51 AMZabGo
04/21/2020, 8:07 AMfun foo(bar: Activity){}
Do I have to add anything in the build.gradle.kts?
Thanks for your help.Vivekpanchal64
04/21/2020, 9:29 AMChills
04/21/2020, 10:55 AMVitali Plagov
04/21/2020, 11:33 AMJakub
04/21/2020, 2:46 PMobject
private val processor = DeeplinkProcessor()
object branchListener : Branch.BranchReferralInitListener {
override fun onInitFinished(referringParams: JSONObject?, error: BranchError?) {
processor.process(referringParams)
}
}
in that case, I can’t access processor
from object BranchReferralInitListener
LeoColman
04/21/2020, 7:27 PMSrSouza
04/21/2020, 8:02 PMV
type only from the KProperty.Slackbot
04/22/2020, 12:07 AMnapperley
04/22/2020, 1:53 AMuser
04/22/2020, 9:08 AMiex
04/22/2020, 9:22 AMvalue
used in it should be publicly readable:
@Parcelize
data class UnixTime private constructor(value: Long) : Parcelable {
companion object {
fun fromValue(value: Long): UnixTime =
UnixTime(value)
fun minTimestamp(): UnixTime =
UnixTime(0)
fun now(): UnixTime =
UnixTime(Date().time / 1000)
}
}
fun UnixTime.debugString() =
"$value, ${toDate()}"
fun UnixTime.toDate() =
Date(value * 1000)
This doesn't compile, and has also a warning in private
. How do I implement it correctly?gabrielfv
04/22/2020, 6:23 PMval url = "<https://google.com/>"
val rgxReplace = url.replace("/".toRegex(), "\\/")
val strReplace = url.replace("/", "\\/")
println(rgxReplace) // outputs: <https://google.com/>
println(strReplace) // outputs: https:\/\/google.com\/
CLOVIS
04/22/2020, 6:28 PMclass Test(
var test: Int,
get = test++
private set,
val test2: Int
)
Is there any reason like a syntax ambiguity, or is it just because it's not ‘clean'?CLOVIS
04/22/2020, 6:30 PMclass Test(
/** Put the documentation comment here */
val test: Int
)
Michael Sim
04/22/2020, 6:56 PMvar x = 10
var z: Long = x.toLong()
// or can I just write var z as:
var z = x.toLong()
// compiler should be able to infer the type, no?
Antoine Gagnon
04/22/2020, 8:52 PMclass Dog{
val name:String
val age:Int
init { this = fetchDogFromSomewhere() }
}
fun fetchDogFromSomewhere():Dog{
// Things
}
Is there some way to achieve this? Right now I’m using a companion method that calls fetchDogFromSomwhereHexa
04/22/2020, 10:01 PMIsaac Miti
04/22/2020, 10:25 PMcamdenorrb
04/23/2020, 6:13 AMspand
04/23/2020, 8:10 AMKagurazaka Tsuki
04/23/2020, 9:08 AMPeter Kucera
04/23/2020, 11:25 AMsvenjacobs
04/23/2020, 12:09 PMlist.asSequence().withIndex().find { some predicate }
svenjacobs
04/23/2020, 12:09 PMlist.asSequence().withIndex().find { some predicate }
streetsofboston
04/23/2020, 12:13 PMasSequence()
is necessary...svenjacobs
04/23/2020, 12:18 PMasSequence()
makes sense because then the values are computed lazily. So if the predicate matches the second item of the list out of 500, then only two IndexedValue
needed to be created and 498 IndexedValue
where never created 🙂marstran
04/23/2020, 12:41 PMwithIndex
on a list actually returns a lazy iterable: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/with-index.htmlsvenjacobs
04/23/2020, 12:43 PMmarstran
04/23/2020, 12:44 PMIterable
that wraps each element ...".svenjacobs
04/23/2020, 12:44 PMIterable
you're correct, but that's the reason why I convert the list to a Sequence
firstmarstran
04/23/2020, 12:44 PMlist.withIndex()
is just as lazy as the sequence.svenjacobs
04/23/2020, 12:46 PM