# I'm seeking better way to use global struct

**URL:** <https://discourse.ats-lang.org/t/im-seeking-better-way-to-use-global-struct/52>\
**Category:** Google Group\
**Created:** [August 20, 2016, 2:39am UTC](https://discourse.ats-lang.org/t/im-seeking-better-way-to-use-global-struct/52 "2016-08-20T02:39:13Z")\
**Posts on this page:** 6\
**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:** [August 20, 2016, 2:39am UTC](https://discourse.ats-lang.org/t/im-seeking-better-way-to-use-global-struct/52/1 "2016-08-20T02:39:13Z")

</div>

Please try the following code:

#include “share/atspre\_define.hats”  
#include “share/atspre\_staload.hats”

%{^  
struct foo {  
int a;  
int \*p;  
};  
struct foo foo\_var;  
%}

absvt@ype struct\_foo  
vtypedef struct\_foo\_impl = $extype\_struct"struct foo" of {  
a = int,  
p = [l:addr] (int@l | ptr l)  
}

vtypedef pstruct\_foo = [l:addr] (struct\_foo@l | ptr(l))

assume struct\_foo = struct\_foo\_impl  
macdef takeout\_pstruct\_foo = $extval(pstruct\_foo, “&foo\_var”)  
extern praxi addback\_pstruct\_foo (pstruct\_foo): void

implement main0 () = {  
val (pf | p) = takeout\_pstruct\_foo  
// val () = foo.a := 1 // error(3): a non-proof component is replaced of  
the type[S2Ecst(int)].  
val () = println! !p.a  
// val () = println! foo.p // error(3): the symbol [print] cannot be  
resolved as no match is found.  
prval () = addback\_pstruct\_foo @(pf | p)  
}

I put a running version here:

[https://github.com/githwxi/ATS-Postiats-test/blob/master/contrib/hwxi/TEST30/test39.datsOn](https://github.com/githwxi/ATS-Postiats-test/blob/master/contrib/hwxi/TEST30/test39.datsOn) Friday, August 19, 2016 at 8:56:08 PM UTC-4, Kiwamu Okabe wrote:

> Hi all,
> 
> I’m writing following code, because seeking better way to use global  
> struct.
> 
> [https://github.com/jats-ug/practice-ats/blob/master/extval/main.dats](https://github.com/jats-ug/practice-ats/blob/master/extval/main.dats)
> 
> ```auto
> #include "share/atspre_define.hats" 
> #include "share/atspre_staload.hats" 
> 
> %{^ 
> struct foo { 
> int a; 
> int *p; 
> }; 
> struct foo foo_var; 
> %} 
> 
> absvt@ype struct_foo 
> vtypedef struct_foo_impl = $extype_struct"struct foo" of { 
> a = int, 
> p = [l:addr] (int@l | ptr l) 
> } 
> assume struct_foo = struct_foo_impl 
> macdef takeout_struct_foo = $extval(struct_foo, "foo_var") 
> extern praxi addback_struct_foo (struct_foo): void 
> 
> implement main0 () = { 
> val foo = takeout_struct_foo 
> // val () = foo.a := 1 // error(3): a non-proof component is replaced 
> of the type[S2Ecst(int)]. 
> val () = println! foo.a 
> // val () = println! foo.p // error(3): the symbol [print] cannot be 
> resolved as no match is found. 
> prval () = addback_struct_foo foo 
> } 
> 
> ```
> 
> However, the code can’t touch the member of the struct.  
> Is it non-sense code?
> 
> ## Best regards,
> 
> Kiwamu Okabe at METASEPI DESIGN

---

<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:** [August 20, 2016, 4:49am UTC](https://discourse.ats-lang.org/t/im-seeking-better-way-to-use-global-struct/52/2 "2016-08-20T04:49:28Z")

</div>

> > And also, we can’t use at-view in struct define? I should use cPtr0() in  
> > struct?

You can have at-views in a struct, but handling such views is quite  
involved. I would  
not recommend it.On Fri, Aug 19, 2016 at 11:02 PM, Kiwamu Okabe [kiw...@debian.or.jp](mailto:kiw...@debian.or.jp) wrote:

> Hi Hongwei,
> 
> ```auto
> vtypedef struct_foo_impl = $extype_struct"struct foo" of {
> a = int,
> p = ptr
> }
> 
> ```
> 
> And also, we can’t use at-view in struct define? I should use cPtr0() in  
> struct?
> 
> ## Best regards,
> 
> Kiwamu Okabe
> 
> –  
> 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](mailto:ats-lang-user...@googlegroups.com).  
> To post to this group, send email to [ats-lan...@googlegroups.com](mailto:ats-lan...@googlegroups.com).  
> Visit this group at [https://groups.google.com/group/ats-lang-users](https://groups.google.com/group/ats-lang-users).  
> To view this discussion on the web visit [https://groups.google.com/d/](https://groups.google.com/d/)  
> msgid/ats-lang-users/CAEvX6d%3Dw8Vfi4z7KOUTdUBpZi3kRm-  
> ab0gq0c7NCvPS\_mHR6pw%[40mail.gmail.com](http://40mail.gmail.com).

---

<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:** [August 20, 2016, 4:52am UTC](https://discourse.ats-lang.org/t/im-seeking-better-way-to-use-global-struct/52/3 "2016-08-20T04:52:24Z")

</div>

You can have at-views inside a struct, but handing such views is quite  
involved.  
I would not recommend it.On Friday, August 19, 2016 at 11:02:23 PM UTC-4, Kiwamu Okabe wrote:

> Hi Hongwei,
> 
> ```auto
> vtypedef struct_foo_impl = $extype_struct"struct foo" of { 
> a = int, 
> p = ptr 
> } 
> 
> ```
> 
> And also, we can’t use at-view in struct define? I should use cPtr0() in  
> struct?
> 
> ## Best regards,
> 
> Kiwamu Okabe

---

<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:** [August 20, 2016, 4:54am UTC](https://discourse.ats-lang.org/t/im-seeking-better-way-to-use-global-struct/52/4 "2016-08-20T04:54:41Z")

</div>

In this case, buz is a ‘var’ (instead of a ‘val’). A ‘var’ is always  
a left-value.On Sat, Aug 20, 2016 at 12:52 AM, Kiwamu Okabe [kiw...@debian.or.jp](mailto:kiw...@debian.or.jp) wrote:

> Hi Hongwei,
> 
> On Sat, Aug 20, 2016 at 1:44 PM, Hongwei Xi [gmh...@gmail.com](mailto:gmh...@gmail.com) wrote:
> 
> > If you do:
> > 
> > val foo = $extval(struct\_foo, “foo”)
> > 
> > the foo is a value (a flat record); it cannot be used as a left-value.
> 
> Thanks. It means the answer for following:
> 
> > Totally, `$extval` can only maintain viewtype as pointer. Is it right?
> 
> However, following code is using flat record with well-typed…
> 
> ```auto
> var buz: @{ a = int, b = ptr }
> val () = buz.a := 1
> 
> ```
> 
> ## Best regards,
> 
> Kiwamu Okabe at METASEPI DESIGN
> 
> –  
> 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](mailto:ats-lang-user...@googlegroups.com).  
> To post to this group, send email to [ats-lan...@googlegroups.com](mailto:ats-lan...@googlegroups.com).  
> Visit this group at [https://groups.google.com/group/ats-lang-users](https://groups.google.com/group/ats-lang-users).  
> To view this discussion on the web visit [https://groups.google.com/d/](https://groups.google.com/d/)  
> msgid/ats-lang-users/CAEvX6dnRibuCXTDgv9gkLoYC92eRz  
> 09eoEYi0cu42zBgJWd5mA%[40mail.gmail.com](http://40mail.gmail.com).

---

<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:** [August 20, 2016, 4:44am UTC](https://discourse.ats-lang.org/t/im-seeking-better-way-to-use-global-struct/52/5 "2016-08-20T04:44:32Z")

</div>

If you do:

val foo = $extval(struct\_foo, “foo”)

the foo is a value (a flat record); it cannot be used as a left-value.On Fri, Aug 19, 2016 at 10:52 PM, Kiwamu Okabe [kiw...@debian.or.jp](mailto:kiw...@debian.or.jp) wrote:

> Hi Hongwei,
> 
> On Sat, Aug 20, 2016 at 11:39 AM, gmhwxi [gmh...@gmail.com](mailto:gmh...@gmail.com) wrote:
> 
> > Please try the following code:
> 
> Thanks a lot!  
> Totally, `$extval` can only maintain viewtype as pointer. Is it right?  
> I’m some confusing…
> 
> > val () = println! !(!pfoo.p) // print 10
> 
> I like following style, better. The code is well-typed. My thinking is  
> a bad thing?
> 
> ```auto
> val () = println! !(pfoo->p) // print 10
> 
> ```
> 
> ## Best regards,
> 
> Kiwamu Okabe at METASEPI DESIGN
> 
> –  
> 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](mailto:ats-lang-user...@googlegroups.com).  
> To post to this group, send email to [ats-lan...@googlegroups.com](mailto:ats-lan...@googlegroups.com).  
> Visit this group at [https://groups.google.com/group/ats-lang-users](https://groups.google.com/group/ats-lang-users).  
> To view this discussion on the web visit [https://groups.google.com/d/](https://groups.google.com/d/)  
> msgid/ats-lang-users/CAEvX6d%3DtzQTALLbZkLisbBOfR-VqD%  
> 3DTQS4BXBGYj6WKnbcGPyA%[40mail.gmail.com](http://40mail.gmail.com).

---

<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:** [August 20, 2016, 5:02am UTC](https://discourse.ats-lang.org/t/im-seeking-better-way-to-use-global-struct/52/6 "2016-08-20T05:02:11Z")

</div>

Yes, you can use ‘extvar’. However, ‘extvar’ is usually for targeting a  
language  
that does support explicit pointers (e.g., Lisp (Scheme), JavaScript).On Saturday, August 20, 2016 at 12:58:18 AM UTC-4, Kiwamu Okabe wrote:

> On Sat, Aug 20, 2016 at 1:54 PM, Hongwei Xi \<…\> wrote:
> 
> > In this case, buz is a ‘var’ (instead of a ‘val’). A ‘var’ is always  
> > a left-value.
> 
> Thanks. You mean $extval return ‘val’.  
> May I use extvar for this purpose?
> 
> ## Best regards,
> 
> Kiwamu Okabe at METASEPI DESIGN
