-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Introduce another version of Signals that has no uninitialized field. #11238
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
Merged
sjrd
merged 1 commit into
scala:master
from
dotty-staging:no-uninitialized-fields-in-signal
Jan 29, 2021
+87
−0
Merged
Changes from all commits
Commits
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,3 @@ | ||
0 | ||
10 | ||
30 |
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,84 @@ | ||
// An alternative to Signals1 that does not rely on an uninitialized variable | ||
import annotation.unchecked._ | ||
|
||
package frp: | ||
|
||
trait Signal[+T]: | ||
def apply()(using caller: Signal.Caller): T | ||
|
||
object Signal: | ||
|
||
abstract class AbstractSignal[+T] extends Signal[T]: | ||
private var observers: Set[Caller] = Set() | ||
private var currentValue: T = eval(this) | ||
|
||
protected def eval(caller: Caller): T | ||
|
||
protected def computeValue(): Unit = | ||
val newValue = eval(this) | ||
val observeChange = observers.nonEmpty && newValue != currentValue | ||
currentValue = newValue | ||
if observeChange then | ||
val obs = observers | ||
observers = Set() | ||
obs.foreach(_.computeValue()) | ||
|
||
def apply()(using caller: Caller): T = | ||
observers += caller | ||
assert(!caller.observers.contains(this), "cyclic signal definition") | ||
currentValue | ||
end AbstractSignal | ||
|
||
def apply[T](expr: Caller ?=> T): Signal[T] = | ||
new AbstractSignal[T]: | ||
protected def eval(caller: Caller) = expr(using caller) | ||
computeValue() | ||
|
||
class Var[T](private var expr: Caller ?=> T) extends AbstractSignal[T]: | ||
protected def eval(caller: Caller) = expr(using caller) | ||
|
||
def update(expr: Caller ?=> T): Unit = | ||
this.expr = expr | ||
computeValue() | ||
end Var | ||
|
||
opaque type Caller = AbstractSignal[?] | ||
given noCaller: Caller = new AbstractSignal[Unit]: | ||
protected def eval(caller: Caller) = () | ||
override def computeValue() = () | ||
|
||
end Signal | ||
end frp | ||
|
||
import frp._ | ||
class BankAccount: | ||
def balance: Signal[Int] = myBalance | ||
|
||
private val myBalance: Signal.Var[Int] = Signal.Var(0) | ||
|
||
def deposit(amount: Int): Unit = | ||
if amount > 0 then | ||
val b = myBalance() | ||
myBalance() = b + amount | ||
|
||
def withdraw(amount: Int): Int = | ||
if 0 < amount && amount <= balance() then | ||
val b = myBalance() | ||
myBalance() = b - amount | ||
myBalance() | ||
else throw new AssertionError("insufficient funds") | ||
end BankAccount | ||
|
||
@main def Test() = | ||
def consolidated(accts: List[BankAccount]): Signal[Int] = | ||
Signal(accts.map(_.balance()).sum) | ||
|
||
val a = BankAccount() | ||
val b = BankAccount() | ||
val c = consolidated(List(a, b)) | ||
println(c()) | ||
a.deposit(10) | ||
println(c()) | ||
b.deposit(20) | ||
println(c()) | ||
end Test |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added.