Discussion:
[Ur] Set / Update values within a structure
Harshita Kasera
2017-11-10 03:42:22 UTC
Permalink
Hello all,

I am trying to use a structure but, I am stuck at one point. This is my
structure definition -


*functor Make(M : sig*
* val callback : _*
* end) = struct*
* val cb = M.callback*

* fun simple nn =*
* cb(nn);*
* return nn*
*end*

What I am trying to do is to initiate a structure with a callback function.
Below demonstrates using a function debugMe as a callback. This works
totally fine.

*fun debugMe a =*
* alert a*

*structure AB = Mymaths.Make(struct*
* val callback = debugMe*
* end)*

*fun main()=*
*let*
* fun callMe () =*
* x <- AB.simple "test";*
* debug x*

*in*
* return <xml>*
* <body>*
* <h1> Sample </h1>*
* <button value="Click Me" onclick={fn _ => callMe()}></button>*
* </body>*
* </xml>*
*end*



What I ultimately want to achieve is to be able to change the callback
function *debugMe* at a later point of time. Is it possible to set or
update values within a structure at a later point of time?

I am new to urweb and SML in general so pardon me if this is really silly.
Any help would be really appreciated. Thanks a lot!

Regards,
Harshita
Adam Chlipala
2017-11-10 12:48:02 UTC
Permalink
Post by Harshita Kasera
What I ultimately want to achieve is to be able to change the callback
function /debugMe/ at a later point of time. Is it possible to set or
update values within a structure at a later point of time?
No, module (structure) fields can't be modified at runtime in Ur/Web or
ML.  Therefore, the module system is not the right tool for runtime
rewriting of callbacks.
Nitin Surana
2017-11-10 21:19:21 UTC
Permalink
Hi Adam

Thanks for responding. Is there any alternative to change the callback ? I
want to trigger a urweb function defined within `let` block upon a
javascript event.
Since, the function is within `let` block (because it needs access to
certain variables within the parent function), I'm unable to use execF(...)
from javascript.

Also, is `ref` not supported in urweb ?

Thanks
Post by Harshita Kasera
What I ultimately want to achieve is to be able to change the callback
function *debugMe* at a later point of time. Is it possible to set or
update values within a structure at a later point of time?
No, module (structure) fields can't be modified at runtime in Ur/Web or
ML. Therefore, the module system is not the right tool for runtime
rewriting of callbacks.
_______________________________________________
Ur mailing list
https://urldefense.proofpoint.com/v2/url?u=http-3A__www.
impredicative.com_cgi-2Dbin_mailman_listinfo_ur&d=DwIGaQ&c=
clK7kQUTWtAVEOVIgvi0NU5BOUHhpN0H8p7CSfnc_gI&r=fRXDP9LYNqdFgci7lxN2fw&m=
X8Ib40TF-qUD7bakiyGU9t4RAhizaUVZMC4hWQyT48Y&s=rYoIQIsMrBs_
ryXBiGe6ZvYAeF7m5GICNRlSuX__A7w&e=
--
Regards
Nitin Surana
MS Computer Science
323-690-6529
Nitin Surana
2017-11-10 21:21:23 UTC
Permalink
Here is the associated stackoverflow question :

https://stackoverflow.com/questions/47230123/how-to-associate-a-getter-setter-with-a-sml-structure/47230248#47230248
Post by Nitin Surana
Hi Adam
Thanks for responding. Is there any alternative to change the callback ? I
want to trigger a urweb function defined within `let` block upon a
javascript event.
Since, the function is within `let` block (because it needs access to
certain variables within the parent function), I'm unable to use execF(...)
from javascript.
Also, is `ref` not supported in urweb ?
Thanks
Post by Harshita Kasera
What I ultimately want to achieve is to be able to change the callback
function *debugMe* at a later point of time. Is it possible to set or
update values within a structure at a later point of time?
No, module (structure) fields can't be modified at runtime in Ur/Web or
ML. Therefore, the module system is not the right tool for runtime
rewriting of callbacks.
_______________________________________________
Ur mailing list
https://urldefense.proofpoint.com/v2/url?u=http-3A__www.impr
edicative.com_cgi-2Dbin_mailman_listinfo_ur&d=DwIGaQ&c=clK7k
QUTWtAVEOVIgvi0NU5BOUHhpN0H8p7CSfnc_gI&r=fRXDP9LYNqdFgci7lxN
2fw&m=X8Ib40TF-qUD7bakiyGU9t4RAhizaUVZMC4hWQyT48Y&s=
rYoIQIsMrBs_ryXBiGe6ZvYAeF7m5GICNRlSuX__A7w&e=
--
Regards
Nitin Surana
MS Computer Science
323-690-6529 <(323)%20690-6529>
--
Regards
Nitin Surana
MS Computer Science
323-690-6529
Adam Chlipala
2017-11-10 21:45:40 UTC
Permalink
You can register a callback that, when called, reads another callback
out of a [source] and calls it.  Is that sufficient?
Post by Nitin Surana
Hi Adam
Thanks for responding. Is there any alternative to change the callback
? I want to trigger a urweb function defined within `let` block upon a
javascript event.
Since, the function is within `let` block (because it needs access to
certain variables within the parent function), I'm unable to use
execF(...) from javascript.
Also, is `ref` not supported in urweb ?
Thanks
Post by Harshita Kasera
What I ultimately want to achieve is to be able to change the
callback function /debugMe/ at a later point of time. Is it
possible to set or update values within a structure at a later
point of time?
No, module (structure) fields can't be modified at runtime in
Ur/Web or ML.  Therefore, the module system is not the right tool
for runtime rewriting of callbacks.
Nitin Surana
2017-11-10 22:53:13 UTC
Permalink
Hi Adam

I tried to create a sample from what I understand about your suggestion,
but of course I got it wrong.
Following is the code (which doesn't compile), which would demonstrate what
I'm trying to do (look for 4 forward slashes ////)

fun debugMe a =
alert a;
////can't read from source n here, because it's defined within
main(), below doesn't compile
fnn <- get n;
fnn()

structure AB = Mymaths.Make(struct
val callback = debugMe
end)

fun main()=
n <- source Nil;
lss <- source Nil;
let
fun callMe () =
x <- AB.simple "test";
debug x

fun anotherCallback()
//iterate over source lss and do something

in
//Some code to change the callback debugMe to anotherCallback() but what ?!
set n anotherCallback ////this doesn't work
return <xml>
<body>
<h1> Sample </h1>
<button value="Click Me" onclick={fn _ => callMe()}></button>
</body>
</xml>
end

Thanks
You can register a callback that, when called, reads another callback out
of a [source] and calls it. Is that sufficient?
Hi Adam
Thanks for responding. Is there any alternative to change the callback ? I
want to trigger a urweb function defined within `let` block upon a
javascript event.
Since, the function is within `let` block (because it needs access to
certain variables within the parent function), I'm unable to use execF(...)
from javascript.
Also, is `ref` not supported in urweb ?
Thanks
Post by Harshita Kasera
What I ultimately want to achieve is to be able to change the callback
function *debugMe* at a later point of time. Is it possible to set or
update values within a structure at a later point of time?
No, module (structure) fields can't be modified at runtime in Ur/Web or
ML. Therefore, the module system is not the right tool for runtime
rewriting of callbacks.
_______________________________________________
Ur mailing list
https://urldefense.proofpoint.com/v2/url?u=http-3A__www.
impredicative.com_cgi-2Dbin_mailman_listinfo_ur&d=DwIGaQ&c=
clK7kQUTWtAVEOVIgvi0NU5BOUHhpN0H8p7CSfnc_gI&r=fRXDP9LYNqdFgci7lxN2fw&m=
4BPlw-ffwmldYJ_nUfPoCMcCEtXDUqFj73lIc-B35lQ&s=SdXCyX5EL_
W3lxWpCwUlkH65djIEQvhG4MnsKPjypKE&e=
--
Regards
Nitin Surana
MS Computer Science
323-690-6529
Ziv Scully
2017-11-11 17:22:50 UTC
Permalink
The original question smells like a potential XY problem (
http://xyproblem.info/). What's the actual goal? There is probably a
functional pattern that solves the problem in a nicer way than the
imperative mutation of a callback function.

Nitin, if you specifically want to call a mutable Ur/Web callback from
JavaScript, it's probably easiest to pass values you need (sources,
callback functions, or anything else) through a custom JavaScript FFI
function (below called passToJs) that you call from inside main. You can
set global JavaScript variables or do whatever else from inside passToJs.

val main =
src <- source (fn () => return ());
return <xml>
<active code={passToJs (fn () => f <- get src; f ())}/>
(* XML that may mutate src *)
</xml>

But I still suspect that this isn't the best solution to whatever the real
problem is!

Ziv
Post by Nitin Surana
Hi Adam
I tried to create a sample from what I understand about your suggestion,
but of course I got it wrong.
Following is the code (which doesn't compile), which would demonstrate
what I'm trying to do (look for 4 forward slashes ////)
fun debugMe a =
alert a;
////can't read from source n here, because it's defined within main(), below doesn't compile
fnn <- get n;
fnn()
structure AB = Mymaths.Make(struct
val callback = debugMe
end)
fun main()=
n <- source Nil;
lss <- source Nil;
let
fun callMe () =
x <- AB.simple "test";
debug x
fun anotherCallback()
//iterate over source lss and do something
in
//Some code to change the callback debugMe to anotherCallback() but what ?!
set n anotherCallback ////this doesn't work
return <xml>
<body>
<h1> Sample </h1>
<button value="Click Me" onclick={fn _ => callMe()}></button>
</body>
</xml>
end
Thanks
You can register a callback that, when called, reads another callback out
of a [source] and calls it. Is that sufficient?
Hi Adam
Thanks for responding. Is there any alternative to change the callback ?
I want to trigger a urweb function defined within `let` block upon a
javascript event.
Since, the function is within `let` block (because it needs access to
certain variables within the parent function), I'm unable to use execF(...)
from javascript.
Also, is `ref` not supported in urweb ?
Thanks
Post by Harshita Kasera
What I ultimately want to achieve is to be able to change the callback
function *debugMe* at a later point of time. Is it possible to set or
update values within a structure at a later point of time?
No, module (structure) fields can't be modified at runtime in Ur/Web or
ML. Therefore, the module system is not the right tool for runtime
rewriting of callbacks.
_______________________________________________
Ur mailing list
https://urldefense.proofpoint.com/v2/url?u=http-3A__www.impredicative.com_cgi-2Dbin_mailman_listinfo_ur&d=DwIGaQ&c=clK7kQUTWtAVEOVIgvi0NU5BOUHhpN0H8p7CSfnc_gI&r=fRXDP9LYNqdFgci7lxN2fw&m=4BPlw-ffwmldYJ_nUfPoCMcCEtXDUqFj73lIc-B35lQ&s=SdXCyX5EL_W3lxWpCwUlkH65djIEQvhG4MnsKPjypKE&e=
--
Regards
Nitin Surana
MS Computer Science
323-690-6529
_______________________________________________
Ur mailing list
http://www.impredicative.com/cgi-bin/mailman/listinfo/ur
Loading...