Is it possible to return array by value?

41 views
Skip to first unread message

Russoul

unread,
Oct 25, 2017, 7:12:22 AM10/25/17
to ats-lang-users
Is it possible to return array by value?

gmhwxi

unread,
Oct 25, 2017, 1:42:35 PM10/25/17
to ats-lang-users

Only for arrays of fixed size.

Here is a sketch for arrays of size 2:

(* ****** ****** *)

#include
"share/atspre_staload.hats"

(* ****** ****** *)

abst@ype
htuple2(a:t@ype) = @(a, a)

(* ****** ****** *)
//
extern
fun
{a:t@ype}
htuple2_make
  (x: a, y: a): htuple2(a)
//
extern
fun
{a:t@ype}
htuple2_get_at
  (htuple2(a), natLt(2)): a
//
overload [] with htuple2_get_at
//
(* ****** ****** *)

local

assume
htuple2(a:t@ype) = @(a, a)

in

implement
{a}
htuple2_make(x, y) = @(x, y)

implement
{a}
htuple2_get_at
  (xy, i) =
(
  if i = 0 then xy.0 else xy.1
)

end // end of [local]

implement
main0() =
{
  val xy = htuple2_make<int>(0, 1)
  val () = println! ("xy[0] = ", xy[0])
  val () = println! ("xy[1] = ", xy[1])
}

(* ****** ****** *)
Reply all
Reply to author
Forward
0 new messages