Performing effects inside handlers? Unclear type error #660
Replies: 1 comment 2 replies
-
import std/core/unsafe
alias set = list
pub value struct signal<a,h>
deps: ref<h, set<(a) -> <div,st<h>> ()>>
inner: ref<h, a>
effect state<a,h>
ctl get( self: signal<a,h>): a
ctl set( self: signal<a,h>, value: a): ()
pub fun with-context(action: () -> <state<a, h>,div,st<h>> ()): <div,st<h>> ()
with handler
ctl get(s)
s.deps.modify fn(d)
Cons(resume, d)
resume(!s.inner)
ctl set(s, value)
s.inner.set(value)
(!s.deps).foreach fn(dep)
dep(!s.inner)
action()
pub fun signal/create(inner: a): <alloc<h>|e> signal<a,h>
Signal(ref([]), ref(inner))
type html
Div(attrs: list<(string, string)>, children: list<html>)
P(attrs: list<(string, string)>, info: string)
fun html/show(html: html): div string
match html
Div(attrs, children) ->
"<div " ++ attrs.map(fn((k, v)) k ++ "=\"" ++ v ++ "\"").join(" ") ++ ">" ++ children.map(show).join("") ++ "</div>"
P(attrs, info) ->
"<p " ++ attrs.map(fn((k, v)) k ++ "=\"" ++ v ++ "\"").join(" ") ++ ">" ++ info ++ "</p>"
fun example()
with with-context
val a-signal = signal/create("foo")
Div(
[("style", "color: red;")],
[P([], a-signal.get())]
).show.trace
()First of all, I recommend you don't use unsafe-total if you can avoid it. Next let's take a look at the error: I'll take you through my thought process when I was debugging this error, hopefully it is instructional. I don't claim that it is the only way to debug the issue. First let's take a look at the handler and what the operation handlers are declared as: both are declared as |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm trying to write a small reactive signal library. Here is the core of it:
The idea being:
with-contextgeteffect, the continuation for that call is inserted into that signal's dependenciesI'm running into a couple of problems:
unsafe-total, is there a workaround for this pattern? I've had similar issues implementing a basicwritereffect a la Haskell's writer monadAny advice?
Beta Was this translation helpful? Give feedback.
All reactions