dhananjaykulkarni19
06/19/2019, 5:49 AMbohsen
06/19/2019, 8:58 AMcontracts
and have run into a problem I need some expert help with.
I'm trying to validate user input from a method that takes a couple of nullable parameters and would like to use contracts
and the below checkParamIsNotNullOrBlank
-function to avoid null checks at the end of my expression. Here's a simplified example of what I'm trying to achieve:
@ExperimentalContracts
override fun validateInput(
firstParam: String?,
secondParam: String?,
thirdParam: String?,
listener: ValidationListener?) {
var error: Boolean
error = isParamNullOrBlank(firstParam) { view?.firstParamEmptyError() }
if (secondParam.isNullOrBlank()) {
view?.secondParamEmptyError()
error = true
}
if (thirdParam.isNullOrBlank()) {
view?.thirdParamEmptyError()
error = true
}
if (error == false) {
val validObject = Object.Builder()
.withFirstParam(firstParam)
.withSecondAndThirdParam("$secondParam^$thirdParam")
.create
listener?.onValidated(validObject)
}
}
Here's my "contracts"-method:
@ExperimentalContracts
fun isParamNullOrBlank(param: String?, onInvalid: (() -> Unit)? = null) : Boolean {
contract {
returns(false) implies (param != null)
}
val result = param.isNullOrBlank()
onError?.invoke()
return result
}
This doesn't compile because the compiler can't infer that firstParam
is not null. Also every if
expression should be evaluated as the view can display multiple errors.Ifvwm
06/19/2019, 10:26 AMmbonnin
06/19/2019, 12:25 PMspand
06/19/2019, 1:53 PMUndeclaredThrowableException
.
What are people doing instead ?cbruegg
06/19/2019, 4:34 PMts2kt
?Hullaballoonatic
06/19/2019, 5:53 PMBar1
was also added to foos
, but I imagine in other cases, that would be horrendousJoan Colmenero
06/19/2019, 6:18 PMmathew murphy
06/19/2019, 6:36 PMHullaballoonatic
06/19/2019, 6:44 PMclass MyClass(foo: Int, bar: Int) {
private var fooLock = false
var foo = foo
set(v) {
fooLock = true
if (!barLock) bar += (v - field)
field = v
fooLock = false
}
private var barLock = false
var bar = bar
set(v) {
barLock = true
if (!fooLock) foo += (v - field)
field = v
barLock = false
}
}
Matej Drobnič
06/19/2019, 7:14 PMpavi2410
06/19/2019, 8:00 PMString.encodeToByteArray
experimental API introduced there. And I have been using String.toByteArray
since always. I want to know why there is such redundancy? What's the difference between them? (I don't think there is much or any difference at all)mathew murphy
06/19/2019, 9:49 PMmathew murphy
06/19/2019, 9:53 PMMatt
06/20/2019, 2:04 AMcamdenorrb
06/20/2019, 2:56 AMIfvwm
06/20/2019, 5:12 AMAlbert
06/20/2019, 6:31 AMByteArray.decodeToString
with kotlin 1.3.40?Ruckus
06/20/2019, 2:23 PMIfvwm
06/20/2019, 2:33 PMrobstoll
06/20/2019, 3:21 PMKotlinIdeaResolutionException: Kotlin resolution encountered a problem while analyzing KtNameReferenceExpression
) how can I downgrade the kotlin plugin?dMusicb
06/20/2019, 5:16 PMAllan Wang
06/20/2019, 6:24 PMinterface Call<T> {
suspend fun call(): T
}
suspend fun <T> Call<T>.callNotNull(): T = call() ?: throw RuntimeException("Example")
val nonnullCall = object : Call<String> {
override suspend fun call(): String = "notnull"
}
val nullCall = object : Call<String?> {
override suspend fun call(): String? = null
}
suspend fun t() {
val r: String = nonnullCall.callNotNull()
val s: String = nullCall.callNotNull()
}
At its current state, I will get an error for nullCall
since s
is String?
Type T
may or may not be nullable, but the returning type is definitely not null.
I can’t use contracts because this is a suspended function
I can’t make the extension to Call<T?>
or it won’t work for nonnullCall
Do I have to make two extension functions for this to work? Is there something like T!
that would indicate a type with nullability removed?Ruckus
06/20/2019, 9:57 PMfun fred(map: Map<String, Any?>) {
...
}
fun test() {
fred(mapOf("a" to { x: Color -> x.grayValue() }, "b" to Color.BLUE))
}
The { x: Color -> x.grayValue()}
is marked as an error due to
Type mismatch
Required: Color
Found: (Color) -> UByteBut it compiles and runs fine
Ruckus
06/20/2019, 10:02 PMfun getResult(value: Any?) {
val result = (value as? Map<*, *>)?.get("result")
....
}
The get
call is marked as an error due to
Type inference failed. The value of the type parameter K should be mentioned in input types (argument types, receiver type or expected type). Try to specify itBut again it compiles and runs fine
ValV
06/21/2019, 1:26 AMclass App {
val listA = listOf(1, 8, 12, 17, 26, 31)
}
fun main(args: Array<String>) {
val app = App()
while (app.listA.hasNext()) {
println(app.listA.next())
}
}
But get this on gradle run
Unresolved reference: hasNext
Unresolved reference: next
Have I forgot something? Something in Gradle?ValV
06/21/2019, 5:22 AMfun main() {
val a = listOf(listOf(32,53,23), listOf(13), listOf(34))
val its = a.map(it.iterator())
println(its)
}
Alexjok
06/21/2019, 7:57 AMfun main() {
val fileList = listOf<FileName>()
}
data class FileName(val code: String, val uid: String, val dateTime: LocalDateTime)
Antanas A.
06/21/2019, 8:18 AMIfvwm
06/21/2019, 10:31 AMIfvwm
06/21/2019, 10:31 AMdiesieben07
06/21/2019, 10:32 AMInt
and String
are final, so no, you cannot inherit from them.gildor
06/21/2019, 10:33 AMStephan Schroeder
06/21/2019, 3:02 PMEither<Int, String?>
is probably what you’re looking for assuming your variable is fine with being either an Int
or a String?
, but not both at the same time.