Working with views and values

48 views
Skip to first unread message

Vanessa McHale

unread,
Sep 3, 2018, 9:15:08 PM9/3/18
to ats-lan...@googlegroups.com
Is there any way to implement

extern
fn {a:vt@ype} as_view (a) : [ l : addr | l > null ] (a @ l | aptr(l))

extern
fn {a:vt@ype} as_value {l:addr} ((a @ l | aptr(l))) : a

?

I've tried using view@ and addr@ but it seems they're for stack
allocations. I suppose I could write my own view in place of using a @
l, but I don't see any way around addr@.

Thanks!


signature.asc

Hongwei Xi

unread,
Sep 3, 2018, 10:09:29 PM9/3/18
to ats-lan...@googlegroups.com
What is aptr(l)?

Here is some code that may be helpful:

extern
fun
{a:vt0p}
ptr_alloc((*void*))
  :<> [l:agz] (a? @ l, mfree_gc_v(l) | ptr(l))
// end of [ptr_alloc]

extern
fn
{a:vt@ype}
as_view(a):
[l:addr | l > null]
(a @ l, mfree_gc_v(l) | ptr(l))

implement
{a}
as_view(x) = let
  val
  (pf,fpf|p) =
  ptr_alloc<a>()
in
  !p := x; (pf, fpf | p)
end


--
You received this message because you are subscribed to the Google Groups "ats-lang-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ats-lang-user...@googlegroups.com.
To post to this group, send email to ats-lan...@googlegroups.com.
Visit this group at https://groups.google.com/group/ats-lang-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/ats-lang-users/af2fe422-c973-2353-7d8d-b6719a5cdc1c%40iohk.io.

Hongwei Xi

unread,
Sep 3, 2018, 10:13:49 PM9/3/18
to ats-lan...@googlegroups.com
If you just want to pass a value via call-by-reference, you can do

fun{a:vt@ype}
foo(x: a): ... = let
   var x = x
   // now the name 'x' refers to a variable, which is a form of left-value
  // that can be passed via call-by-reference
... ...

Vanessa McHale

unread,
Sep 3, 2018, 10:37:44 PM9/3/18
to ats-lan...@googlegroups.com

aptr(l) is an atomic pointer. I believe you can cast between prl(l) and aptr(l) though my C is not particularly good.

And thanks! I'll have a look :)

signature.asc
Reply all
Reply to author
Forward
0 new messages