Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infinite loop #363

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add proposal for infinite loop
vmishenev committed Nov 1, 2023
commit a7e5e1adf9709ba4a3f95cc361d648dea5634eb2
82 changes: 82 additions & 0 deletions proposals/infinite-loop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Infinite loop

* **Type**: Design proposal
* **Author**: Vadim Mishenev, Roman Elizarov
* **Status**: In Progress
* **Prototype**: In Progress
* **Issue**: [KT-27970](https://youtrack.jetbrains.com/issue/KT-27970/Support-an-infinite-for-loop)

## Summary

An [infinite loop](https://en.wikipedia.org/wiki/Infinite_loop) (also known as a “while true”) is an idiom that is widely used in Kotlin programming.

## Motivation / use cases


- Infinite loops are widely used to monitor user input or device activity.
The idiomatic approach to reading all the lines from the input stream until it is over (until readLine functions returns null):

```kotlin
while (true) {
val line = input.readLine() ?: break
vmishenev marked this conversation as resolved.
Show resolved Hide resolved
// process line
}

```
Or the while loop can be used for the main game frame which continues to get executed until the user or the game selects some other event.
- Infinite loops also appear quite often in concurrent programming with coroutines, because various concurrent background processes are often conveniently represented as an infinite loop doing something until cancelled.






The proposal is to support infinite loop via `while { ... }` statement without parameters or conditions, which seems to read nicely for the case of infinite loop, changing the motivating example to:

```kotlin
while {
val line = input.readLine() ?: break
// process line
}

```
But it can look like that user forgot to write a condiftion.


In other languages:

* Go (Golang) has `for { ... }` - loop. Probably `for` stems from `repeat forever`.

* In Rust there is `loop { ... }`.

* In C# there was a [proposal](https://github.com/dotnet/csharplang/issues/2475), but the discussion was shut down.

and so on (Ada, Ruby, Fortran).

Infinite loops can be implemented via a function (like `repeat` in StdLib), but it will not support `break`/`continue`. So this feature is shortest way to support it.


## Type inference

Currently, infinite loops cannot be properly used inside scope functions. The following code does not compile due to type mismatch, since while is not an expression and the resulting type of run coerces to Unit:

```kotlin
private fun foo(): Nothing = // ERROR: Type inference failed. Expected: Unit
run {
while (true) {
doSomething()
}
}
```

Infinite loop, on the other hand, shall be an expression of `Nothing` type (similar to `throw` to mark code locations that can never be reached) so the following code should compile:

```kotlin
private fun foo(): Nothing = // ERROR: Type inference failed. Expected: Unit
run {
while {
doSomething()
}
}
```
But an inifinite loop with `break` is an expression of `Unit` type.