-
Notifications
You must be signed in to change notification settings - Fork 372
KEEP for Self types #356
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
Open
maxim092001
wants to merge
17
commits into
Kotlin:main
Choose a base branch
from
maxim092001:master
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
KEEP for Self types #356
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
65312fc
Initial/transformation chains example
maxim092001 b1940f2
Fix tabulation for lazy containers example
maxim092001 35642bf
Add Observers
maxim092001 866efcd
fix space
maxim092001 2290975
Add recursive containers
maxim092001 5caf1d3
Abstract factory
maxim092001 86ec1cd
Initial design
maxim092001 8100706
Add more to design
maxim092001 05e50d2
Assignment
maxim092001 2996129
return generic position
maxim092001 923aad5
Add specification
maxim092001 a072c52
Add initial swift examples
maxim092001 0daa190
Fix example
maxim092001 e798e22
Finish swift examples
maxim092001 ca60bca
Add Rust and TS examples
maxim092001 7c439cf
Add Manifold
maxim092001 8a9c463
Add Python
maxim092001 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
# Self types | ||
|
||
* **Type**: Design proposal | ||
* **Authors**: Maksim Grankin | ||
* **Status**: Prototype implemented | ||
* **Issue**: [KT-6494](https://youtrack.jetbrains.com/issue/KT-6494) | ||
|
||
|
||
A **Self type** is type that refers to the receiver type. | ||
|
||
```kotlin | ||
open class A { | ||
fun foo(): Self { | ||
return this; | ||
} | ||
} | ||
|
||
class B : A { | ||
|
||
} | ||
|
||
val x: B = B().foo(); // foo return type is B | ||
maxim092001 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
``` | ||
|
||
## Motivation | ||
|
||
Self types can be implemented by programmer with boilerplate code using recursive generics and some additional unchecked casts. | ||
They can be used in multiple useful and popular patterns, so there is a reason and community need to make it a language feature. | ||
|
||
## Usage examples | ||
|
||
One of the most common example of Self types application is [abstract builder pattern](https://medium.com/@hazraarka072/fluent-builder-and-powering-it-up-with-recursive-generics-in-java-483005a85fcd). However, in Kotlin builders are usually implemented via extension recievers.Although, if we want to have transformation chain of immutable object/data, Self types are really usefull. | ||
maxim092001 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
### Transformation chains | ||
|
||
Using transformation chains we can implement *Lazy* containers. *Lazy* container is container that contains computation for some *real* container. This allows to create a sequence of changes to container without computing it only when needed. | ||
|
||
```kotlin | ||
|
||
abstract class Lazy<T, out Self : Lazy<T, Self>>(val computation: () -> T) { | ||
protected abstract fun create(computation: () -> T): Self | ||
} | ||
|
||
abstract class LazyContainer<T, out Self : Lazy<T, Self>>(computation: () -> T) : | ||
Lazy<T, Self>(computation) { | ||
fun applyFunction(f: (T) -> T): Self = create { f(computation()) } | ||
} | ||
|
||
class LazyList<T>(computation: () -> List<T>) : LazyContainer<List<T>, LazyList<T>>(computation) { | ||
override fun create(computation: () -> List<T>): LazyList<T> = LazyList(computation) | ||
fun add(elem: T): LazyList<T> = create { computation() + elem } | ||
} | ||
|
||
class LazySet<T>(computation: () -> Set<T>) : LazyContainer<Set<T>, LazySet<T>>(computation) { | ||
override fun create(computation: () -> Set<T>): LazySet<T> = LazySet(computation) | ||
fun add(elem: T): LazySet<T> = create { computation() + elem } | ||
} | ||
val list = LazyList { listOf(1, 2, 3) } | ||
.applyFunction { l -> l.subList(1, 2) } | ||
.add(15) | ||
.computation() | ||
val set = LazySet { setOf(1, 2, 3) } | ||
.applyFunction { s -> s.map { it + 1 }.toSet() } | ||
.add(3) | ||
.computation() | ||
``` | ||
|
||
With **Self type** feature the same code would look much easier to read. | ||
|
||
```kotlin | ||
import kotlin.Self | ||
|
||
@Self | ||
abstract class Lazy<T>(val computation: () -> T) { | ||
protected abstract fun create(computation: () -> T): Self | ||
} | ||
|
||
@Self | ||
abstract class LazyContainer<T>(computation: () -> T) : | ||
Lazy<T, Self>(computation) { | ||
fun applyFunction(f: (T) -> T): Self = create { f(computation()) } | ||
} | ||
|
||
class LazyList<T>(computation: () -> List<T>) : LazyContainer<List<T>, LazyList<T>>(computation) { | ||
override fun create(computation: () -> List<T>): LazyList<T> = LazyList(computation) | ||
fun add(elem: T): LazyList<T> = create { computation() + elem } | ||
} | ||
|
||
class LazySet<T>(computation: () -> Set<T>) : LazyContainer<Set<T>, LazySet<T>>(computation) { | ||
override fun create(computation: () -> Set<T>): LazySet<T> = LazySet(computation) | ||
fun add(elem: T): LazySet<T> = create { computation() + elem } | ||
} | ||
val list = LazyList { listOf(1, 2, 3) } | ||
.applyFunction { l -> l.subList(1, 2) } | ||
.add(15) | ||
.computation() | ||
val set = LazySet { setOf(1, 2, 3) } | ||
.applyFunction { s -> s.map { it + 1 }.toSet() } | ||
.add(3) | ||
.computation() | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.