Initializing statically-allocated arrays within templates

55 views
Skip to first unread message

M88

unread,
Sep 25, 2018, 3:28:05 PM9/25/18
to ats-lang-users
Hello,

I was wondering if it's possible to use a template to define the size of a statically allocated array in a template.

An example (that won't compile) is:

extern
fun {a:t@ype} example () : void

extern
val {} example$bufsz  : [n:pos] size_t n

implement {a}
example () =
    let
          val sz = example$bufsz<>
          var buf = @[a?][sz]()
    in
    end

Is it possible to define example$bufsz<> as a constant with late-binding?  I suppose behavior like sizeof<a> would be ideal, but that seems to be internal.

Thanks,
-M



Julian Fondren

unread,
Sep 25, 2018, 4:46:17 PM9/25/18
to ats-lang-users
An option.

testarr.dats:

#include "share/atspre_staload.hats"

local
 
#define example$bufsz 100
in
 
#include "./testarr.hats"
end

implement main0
() = example<int>()

testarr.hats:

extern fun {a:t@ype} example(): void

implement
{a} example() =
  let
    val sz
= example$bufsz
   
var buf = @[a?][example$bufsz]()
 
in end

Hongwei Xi

unread,
Sep 25, 2018, 5:13:24 PM9/25/18
to ats-lan...@googlegroups.com
You can use a function template:

fun {} example$bufsz() : [n:pos] size_t n

Because of inlining, you should get what you wanted in the first place.



--
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/b5bd37d6-cfe7-4a30-a956-b8aada50a005%40googlegroups.com.

M88

unread,
Sep 25, 2018, 7:01:48 PM9/25/18
to ats-lang-users

I get an error during C compilation, ATSERRORarrdim_unknown, when I try to use a template function.

Thanks Julian -- that's a decent solution.

gmhwxi

unread,
Sep 25, 2018, 9:22:04 PM9/25/18
to ats-lang-users
I see.

Then there isn't really a way to do this as the 'sz' in @[a?][sz]
is static.

I will keep an eye on it. Maybe I can find a way to support it in ATS3.

Julian Fondren

unread,
Sep 25, 2018, 9:46:45 PM9/25/18
to ats-lang-users
Slightly neater. Note that you can have cover (multiple; expressions) with a single where {}

#include "share/atspre_staload_hats."

implement main0
() = example<int>() where {
 
#define example$bufsz 100
 
#include "./testarr.hats"
}

M88

unread,
Sep 26, 2018, 4:08:03 AM9/26/18
to ats-lang-users
Scoped includes and constants .... are....awesome.


On Tuesday, September 25, 2018 at 3:28:05 PM UTC-4, M88 wrote:
Reply all
Reply to author
Forward
0 new messages