Kotlin / Kotlin/dataframe

Explore Kotlin contracts for Boolean check functions

Open
#551 1 comment 0 reactions 1 assignee Claimed by @Jolanrensen View on GitHub
enhancement research
Dominant language
Kotlin
Stars
1.1k
Forks
83
Avg merge
4d 12h
Merged PRs (30d)
30

Description

It's odd that after a
```kt
if (myCol.isColumnGroup())
```
check you still need an explicit cast to `ColumnGroup<*>` to be able to call `columnsCount()` on `myCol`. The check should tell the compiler that it's actually a column group, similar to what an `myCol is ColumnGroup<*>` would do.

This is actually possible with Kotlin contracts:

```kt
@OptIn(ExperimentalContracts::class)
public fun AnyCol.isColumnGroup(): Boolean {
contract {
returns(true) implies (this@isColumnGroup is ColumnGroup<*>)
}
return kind() == ColumnKind.Group
}
```
makes
```kt
if (myCol.isColumnGroup()) {
myCol.columnsCount() // compile
} else {
myCol.columnsCount() // not compile
}
```

Unfortunately contracts aren't all-powerful ([yet](https://github.com/Kotlin/KEEP/issues/139)):
```kt
@OptIn(ExperimentalContracts::class)
public inline fun AnyCol.isType(): Boolean {
contract {
returns(true) implies (this@isType is DataColumn) // !! Cannot check for instance of erased type: DataColumn :(
}
return type() == typeOf()
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.