# Templates and C

**URL:** https://discourse.ats-lang.org/t/templates-and-c/41
**Category:** Google Group
**Created:** [October 6, 2014, 4:08am UTC](https://discourse.ats-lang.org/t/templates-and-c/41 "2014-10-06T04:08:16Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)
#### Post date: [October 6, 2014, 4:08am UTC](https://discourse.ats-lang.org/t/templates-and-c/41/1 "2014-10-06T04:08:16Z")

</div>

Here is a way to make it work:

%{^  
#undef ATSextfcall  
#define ATSextfcall(f, xs) f xs  
#define foo(x) ((x) \>\> 1)  
%}

extern  
fun  
{tk:tkind}  
foo: {i:int}(g1int (tk, i)) -\<\> [j:int] g1int (tk, j)

implement  
{tk}  
foo (x) = let  
typedef res = [j:int] g1int(tk, j)  
in  
$extfcall (res, “foo”, x)  
end // end of [foo]

implement  
main0 () =  
{  
val I = 16  
val () = println! (“foo(”, I, ") = ", foo(I))  
val L = 16L  
val () = println! (“foo(”, L, ") = ", foo(L))  
val LL = 16LL  
val () = println! (“foo(”, LL, ") = ", foo(LL))  
}On Sunday, October 5, 2014 11:25:05 PM UTC-4, Barry Schwartz wrote:

> Something like
> 
> %{^  
> #define foo(x) ((x) \>\> 1)  
> %}
> 
> extern fun {tk : tkind} foo :  
> {i : int} (g1int (tk, i)) -\<\> [j : int] g1int (tk, j))  
> = “mac#”
> 
> doesn’t work, does it? Even though the C is agnostic about which  
> integer type is needed? On account of foo being a template rather than  
> a function proper.
> 
> I have something like that and it does not seem to work. If I am  
> right, what is the best alternative? Something tedious as appears in  
> integer.{s,d,c}ats?
> 
> (It’s the sort of job one rolls m4 scripts to do.)

---

<div class="post-metadata">

### Author: ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)
#### Post date: [October 6, 2014, 5:45pm UTC](https://discourse.ats-lang.org/t/templates-and-c/41/2 "2014-10-06T17:45:42Z")

</div>

I tidied up the code and put it on-line:

[https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/ATS-QA-LIST/qa-list-387.datsOn](https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/ATS-QA-LIST/qa-list-387.datsOn) Monday, October 6, 2014 12:08:16 AM UTC-4, gmhwxi wrote:

> Here is a way to make it work:
> 
> %{^  
> #undef ATSextfcall  
> #define ATSextfcall(f, xs) f xs  
> #define foo(x) ((x) \>\> 1)  
> %}
> 
> extern  
> fun  
> {tk:tkind}  
> foo: {i:int}(g1int (tk, i)) -\<\> [j:int] g1int (tk, j)
> 
> implement  
> {tk}  
> foo (x) = let  
> typedef res = [j:int] g1int(tk, j)  
> in  
> $extfcall (res, “foo”, x)  
> end // end of [foo]
> 
> implement  
> main0 () =  
> {  
> val I = 16  
> val () = println! (“foo(”, I, ") = ", foo(I))  
> val L = 16L  
> val () = println! (“foo(”, L, ") = ", foo(L))  
> val LL = 16LL  
> val () = println! (“foo(”, LL, ") = ", foo(LL))  
> }
> 
> On Sunday, October 5, 2014 11:25:05 PM UTC-4, Barry Schwartz wrote:
> 
> > Something like
> > 
> > %{^  
> > #define foo(x) ((x) \>\> 1)  
> > %}
> > 
> > extern fun {tk : tkind} foo :  
> > {i : int} (g1int (tk, i)) -\<\> [j : int] g1int (tk, j))  
> > = “mac#”
> > 
> > doesn’t work, does it? Even though the C is agnostic about which  
> > integer type is needed? On account of foo being a template rather than  
> > a function proper.
> > 
> > I have something like that and it does not seem to work. If I am  
> > right, what is the best alternative? Something tedious as appears in  
> > integer.{s,d,c}ats?
> > 
> > (It’s the sort of job one rolls m4 scripts to do.)
