Questions tagged [kotlin-contracts]

13 questions
9
votes
1 answer

Which are the benefits of Kotlin callsInPlace contract?

Can anyone explain to me which are the benefits of Kotlin callsInPlace contract? How the compiler takes advantage of knowing that the lambda function will get called in place? Also, the developer get any benefit from it? Like smartcast or so? Thank…
Luca Piccinelli
  • 379
  • 1
  • 17
8
votes
4 answers

Kotlin Contracts: assert instance on reified type parameter

I'm trying to write an assert function that checks if a given object is of a type T: @UseExperimental(ExperimentalContracts::class) inline fun assertIsInstance(value: Any?) { contract { returns() implies (value is T) } …
s1m0nw1
  • 76,759
  • 17
  • 167
  • 196
5
votes
1 answer

Kotlin contract infer return value instead of argument value

I have a function that looks something like: fun MyInput?.toOutput() : Output? { if (this == null) return null return Output(this.someValue) } In places where I know that my MyInput is non-null (for example, inside a method that takes a input:…
karl
  • 3,544
  • 3
  • 30
  • 46
4
votes
0 answers

Kotlin Contracts not working for null-check in extension function

I'm trying to write an extension function that returns true if the value is not null or 0 and use a contract to guarantee to the compiler that if I return true, the value is non-null. However, it doesn't seem to be working with smart casting. It…
rexar5
  • 470
  • 2
  • 10
4
votes
1 answer

Kotlin - Why do compiler contracts only allow references to function parameters

When writing validation function, often times these validation functions check for nullability. Even though, after calling these functions an objects members can be safely used as if they were non-nullable, Kotlin compiler contracts don't allow…
J Heschl
  • 177
  • 4
  • 15
4
votes
1 answer

Kotlin contracts: link not-null of two properties

Say I have a class like this: data class URLAndPath( val baseUrl: URL, val path: String? ) { val url get(): URL? = try { path?.let { URL(baseUrl, it) } } catch(_: Exception) { null } init { require(path == null || url…
dtech
  • 13,741
  • 11
  • 48
  • 73
2
votes
1 answer

Can a Kotlin contract imply non-nullability of all vararg arguments of a function?

I'd like to write a top-level function like this: @ExperimentalContracts fun containsNull(vararg objs: Any?): Boolean { contract { returns(false) implies (/* ???? */) } for (o in objs) if (o == null) return true …
2
votes
0 answers

How does the Kotlin null-check compiler contract work?

I have a couple of custom scope functions that all look similar to this one: @OptIn(ExperimentalContracts::class) inline fun R.applyIf(condition: Boolean, block: R.() -> Unit): R { contract { callsInPlace(block,…
neph
  • 55
  • 7
2
votes
2 answers

kotlin - remove nullability from class properties

I have a class with some nullable properties data class RequestModel( val description: String? ) and a validation function fun validate(model: RequestModel): RequestModel{ if(model.description == null) throw…
Fartab
  • 4,725
  • 2
  • 26
  • 39
1
vote
1 answer

Using Kotlin contracts to cast type inside Iterable function predicate

I have this sealed class PictureEvent: sealed class PictureEvent { data class PictureCreated(val pictureId: String, val url: String) : PictureEvent() //more classes extending PictureEvent } Now, from a list of PictureEvents, I want to…
Héctor
  • 24,444
  • 35
  • 132
  • 243
1
vote
3 answers

How to write the following Kotlin contract?

The question is very simple: (using Kotlin 1.3.71) I have the following data alike this one: data class Location(val lat: Double, val lng: Double) I want to achieve a type-safety with a call like so: val loc = location { lat = 2.0 lng =…
Some random IT boy
  • 7,569
  • 2
  • 21
  • 47
1
vote
3 answers

Kotlin contract with high order functions

I have problem with Kotlin nullability and I'm wondering am I able to resolve it with contracts. For such Java interface: interface Action{ void execute(T param); } there is two extensions: fun Action.map(mapper:(R)->T): Action { …
Karol Kulbaka
  • 1,136
  • 11
  • 21
1
vote
1 answer

After upgrade to Kotlin 1.3 I still can not use contract

I upgraded the Kotlin plugin in IDEA from 1.2.71 to 1.3.0 and made sure the new SDK is the Project SDK: I expected to be able to use the new kotlin.contracts.contract, but it's deep red! Information:Kotlin: kotlinc-jvm 1.3.0 (JRE…
Raphael
  • 9,779
  • 5
  • 63
  • 94