Replies: 2 comments 5 replies
-
|
Hey, thanks for your question. No there is no shorthand. How would you imagine to look this like? Typically, I also wouldn't "shadow" (use the same name), but call the parameter I am saying this since I am not even sure whether this program would work in Effekt as written above :) |
Beta Was this translation helpful? Give feedback.
4 replies
-
|
Hi, in addition to what was shown already, you can use the following to achieve something similar to mutable parameters:
import ref
...
def runConsolePure(state: Ref[PureConsoleState]) { prog: () => Unit / Console }: Unit = {
try {
prog()
} with Console {
def printLn(s) = {
state.set(PureConsoleState(state.get.inputs, state.get.outputs.pushBack(s)));
resume(())
}
def readLn() = {
state.get.inputs.popFront() match {
case None() => () // TODO: How to panic here?
case Some((input, inputsRest)) => {
state.set(PureConsoleState(inputsRest, state.get.outputs));
resume(input)
}
}
}
}
}Complete code hereimport dequeue
import ref
record PureConsoleState(
inputs: Dequeue[String],
outputs: Dequeue[String]
)
def main() = {
var inputs = emptyQueue[String]();
inputs = inputs.pushBack("first line");
inputs = inputs.pushBack("second line");
val outputs = emptyQueue[String]();
val consoleState = ref(PureConsoleState(inputs, outputs));
runConsolePure(consoleState) { test() };
println("Inputs: " ++ consoleState.get.inputs.show());
println("Outputs: " ++ consoleState.get.outputs.show());
()
}
interface Console {
def printLn(s: String): Unit
def readLn(): String
}
def runConsolePure(state: Ref[PureConsoleState]) { prog: () => Unit / Console }: Unit = {
try {
prog()
} with Console {
def printLn(s) = {
state.set(PureConsoleState(state.get.inputs, state.get.outputs.pushBack(s)));
resume(())
}
def readLn() = {
state.get.inputs.popFront() match {
case None() => () // TODO: How to panic here?
case Some((input, inputsRest)) => {
state.set(PureConsoleState(inputsRest, state.get.outputs));
resume(input)
}
}
}
}
}
def test(): Unit / Console = {
do printLn("First output");
do printLn("Second output");
do readLn();
do readLn();
()
}
interface State[St] {
def getState(): St
def setState(s: St): Unit
}
...
def runConsolePure { prog: () => Unit / Console }: Unit / State[PureConsoleState] = {
try {
prog()
} with Console {
def printLn(s) = {
do setState(PureConsoleState(do getState().inputs, do getState().outputs.pushBack(s)));
resume(())
}
def readLn() = {
do getState().inputs.popFront() match {
case None() => () // TODO: How to panic here?
case Some((input, inputsRest)) => {
do setState(PureConsoleState(inputsRest, do getState().outputs));
resume(input)
}
}
}
}
}Complete code hereimport dequeue
import ref
interface State[St] {
def getState(): St
def setState(s: St): Unit
}
/// Handle the `State` effect using a `Ref` (global mutable reference)
// (but you could also handle it directly, there's no real reason for a `Ref` here anymore)
def statefully[R, St](r: Ref[St]) { prog: () => R / State[St] }: R =
try { prog() } with State[St] {
def getState() = resume(r.get())
def setState(newState) = { r.set(newState); resume(()) }
}
record PureConsoleState(
inputs: Dequeue[String],
outputs: Dequeue[String]
)
def main() = {
var inputs = emptyQueue[String]();
inputs = inputs.pushBack("first line");
inputs = inputs.pushBack("second line");
val outputs = emptyQueue[String]();
val consoleState = ref(PureConsoleState(inputs, outputs));
statefully(consoleState) { runConsolePure { test() } };
println("Inputs: " ++ consoleState.get.inputs.show());
println("Outputs: " ++ consoleState.get.outputs.show());
()
}
interface Console {
def printLn(s: String): Unit
def readLn(): String
}
def runConsolePure { prog: () => Unit / Console }: Unit / State[PureConsoleState] = {
try {
prog()
} with Console {
def printLn(s) = {
do setState(PureConsoleState(do getState().inputs, do getState().outputs.pushBack(s)));
resume(())
}
def readLn() = {
do getState().inputs.popFront() match {
case None() => () // TODO: How to panic here?
case Some((input, inputsRest)) => {
do setState(PureConsoleState(inputsRest, do getState().outputs));
resume(input)
}
}
}
}
}
def test(): Unit / Console = {
do printLn("First output");
do printLn("Second output");
do readLn();
do readLn();
()
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Can I update a function parameter? The only way I could find is copying the parameter to a
var:Is there a shorthand for this?
Beta Was this translation helpful? Give feedback.
All reactions