# Get two refs on same dataviewtype object

**URL:** <https://discourse.ats-lang.org/t/get-two-refs-on-same-dataviewtype-object/323>\
**Category:** Google Group\
**Created:** [October 15, 2013, 7:23am UTC](https://discourse.ats-lang.org/t/get-two-refs-on-same-dataviewtype-object/323 "2013-10-15T07:23:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cyrille\_Duret](https://avatars.discourse-cdn.com/v4/letter/c/3d9bf3/32.png) [@Cyrille\_Duret](https://discourse.ats-lang.org/u/Cyrille_Duret)\
**Post date:** [October 15, 2013, 7:23am UTC](https://discourse.ats-lang.org/t/get-two-refs-on-same-dataviewtype-object/323/1 "2013-10-15T07:23:44Z")

</div>

hello,  
I would like to make a datastructure with a dataviewtype that could  
possibly be referenced more than once and I don’t know if it is possible to  
do this with linear types of ATS.

Basically I want to achieve this (this code doesn’t compile) :

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

staload UN = "prelude/SATS/unsafe.sats"  
staload “prelude/SATS/reference.sats”

dataviewtype toto\_vt =  
Toto of (Strptr1, double)  
| Titi of (double, Strptr1)

extern fun make\_toto(i: double): toto\_vt

extern fun print\_toto(t: ref(toto\_vt)): void  
extern fun free\_toto(t: toto\_vt): void

implement make\_toto(i) = if( i \< 10.0 ) then  
Toto(string0\_copy(“hello”), i)  
else  
Titi(i, string0\_copy(" world !"))

implement print\_toto(t) = let  
val tot: toto\_vt = !t  
val () = (case+ tot of  
| Toto(s, x) =\> println!("TOTO -\> ", s, ", ", x)  
| Titi(x, s) =\> println!("TITI -\> ", s, ", ", x) ): void  
prval () = $UN.castview0{void}(tot)  
in end

implement free\_toto(t) = case+ t of  
| ~Toto(s, x) =\> strptr\_free s  
| ~Titi(x, s) =\> strptr\_free s

implement main0() = let  
val t = make\_toto(17.0)  
val ref1 = ref\<toto\_vt\>(t) //ref\_make\_viewptr(view@t, addr@t)  
val ref2 = ref\<toto\_vt\>(t) //ref\_make\_viewptr(view@t, addr@t)  
val () = print\_toto(ref1)  
val () = print\_toto(ref2)  
in  
free\_toto t  
end

I tried with reference, maybe I will have better fortune with pointer ?  
Is it possible to do this in ATS ?

thanks a lot 😉

---

<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 15, 2013, 6:26pm UTC](https://discourse.ats-lang.org/t/get-two-refs-on-same-dataviewtype-object/323/2 "2013-10-15T18:26:16Z")

</div>

See:

[http://www.ats-lang.org/LIBRARY/libats/refcount.htmlOn](http://www.ats-lang.org/LIBRARY/libats/refcount.htmlOn) Tuesday, October 15, 2013 1:49:58 PM UTC-4, Cyrille Duret wrote:

> Let’s take the example of a scene management tree of a 3d engine where you  
> can find several references to the same geometry so I would like to do the  
> same here.  
> To have one instance of geometry and several references to it on another  
> data structure.
> 
> Indeed reference-counting might be the way for me. I will try it out.
> 
> thanks 😉
> 
> Le mardi 15 octobre 2013 17:45:13 UTC+2, gmhwxi a écrit :
> 
> > This cannot be done.  
> > It is like wanting to put a physical object into distinct places.
> > 
> > Why not do
> > 
> > val ref2 = ref1
> > 
> > Or maybe you were thinking about reference-counting?
> > 
> > On Tuesday, October 15, 2013 3:23:44 AM UTC-4, Cyrille Duret wrote:
> > 
> > > hello,  
> > > I would like to make a datastructure with a dataviewtype that could  
> > > possibly be referenced more than once and I don’t know if it is possible to  
> > > do this with linear types of ATS.
> > > 
> > > Basically I want to achieve this (this code doesn’t compile) :
> > > 
> > > #include “share/atspre\_staload\_tmpdef.hats”
> > > 
> > > staload UN = “prelude/SATS/unsafe.sats”  
> > > staload “prelude/SATS/reference.sats”
> > > 
> > > dataviewtype toto\_vt =  
> > > Toto of (Strptr1, double)  
> > > | Titi of (double, Strptr1)
> > > 
> > > extern fun make\_toto(i: double): toto\_vt
> > > 
> > > extern fun print\_toto(t: ref(toto\_vt)): void  
> > > extern fun free\_toto(t: toto\_vt): void
> > > 
> > > implement make\_toto(i) = if( i \< 10.0 ) then  
> > > Toto(string0\_copy(“hello”), i)  
> > > else  
> > > Titi(i, string0\_copy(" world !"))
> > > 
> > > implement print\_toto(t) = let  
> > > val tot: toto\_vt = !t  
> > > val () = (case+ tot of  
> > > | Toto(s, x) =\> println!("TOTO → ", s, ", ", x)  
> > > | Titi(x, s) =\> println!("TITI → ", s, ", ", x) ): void  
> > > prval () = $UN.castview0{void}(tot)  
> > > in end
> > > 
> > > implement free\_toto(t) = case+ t of  
> > > | ~Toto(s, x) =\> strptr\_free s  
> > > | ~Titi(x, s) =\> strptr\_free s
> > > 
> > > implement main0() = let  
> > > val t = make\_toto(17.0)  
> > > val ref1 = ref\<toto\_vt\>(t) //ref\_make\_viewptr(view@t, addr@t)  
> > > val ref2 = ref\<toto\_vt\>(t) //ref\_make\_viewptr(view@t, addr@t)  
> > > val () = print\_toto(ref1)  
> > > val () = print\_toto(ref2)  
> > > in  
> > > free\_toto t  
> > > end
> > > 
> > > I tried with reference, maybe I will have better fortune with pointer ?  
> > > Is it possible to do this in ATS ?
> > > 
> > > thanks a lot 😉

---

<div class="post-metadata">

**Author:** ![Cyrille\_Duret](https://avatars.discourse-cdn.com/v4/letter/c/3d9bf3/32.png) [@Cyrille\_Duret](https://discourse.ats-lang.org/u/Cyrille_Duret)\
**Post date:** [October 15, 2013, 8:29pm UTC](https://discourse.ats-lang.org/t/get-two-refs-on-same-dataviewtype-object/323/3 "2013-10-15T20:29:20Z")

</div>

thank you !Le mardi 15 octobre 2013 20:34:31 UTC+2, gmhwxi a écrit :

> Some of the functions in
> 
> ${PATSHOME}/contrib/libfloats/DATS/lamatrix.dats
> 
> makes use of refcnt.
> 
> On Tuesday, October 15, 2013 2:26:16 PM UTC-4, gmhwxi wrote:
> 
> > See:
> > 
> > [http://www.ats-lang.org/LIBRARY/libats/refcount.html](http://www.ats-lang.org/LIBRARY/libats/refcount.html)
> > 
> > On Tuesday, October 15, 2013 1:49:58 PM UTC-4, Cyrille Duret wrote:
> > 
> > > Let’s take the example of a scene management tree of a 3d engine where  
> > > you can find several references to the same geometry so I would like to do  
> > > the same here.  
> > > To have one instance of geometry and several references to it on another  
> > > data structure.
> > > 
> > > Indeed reference-counting might be the way for me. I will try it out.
> > > 
> > > thanks 😉
> > > 
> > > Le mardi 15 octobre 2013 17:45:13 UTC+2, gmhwxi a écrit :
> > > 
> > > > This cannot be done.  
> > > > It is like wanting to put a physical object into distinct places.
> > > > 
> > > > Why not do
> > > > 
> > > > val ref2 = ref1
> > > > 
> > > > Or maybe you were thinking about reference-counting?
> > > > 
> > > > On Tuesday, October 15, 2013 3:23:44 AM UTC-4, Cyrille Duret wrote:
> > > > 
> > > > > hello,  
> > > > > I would like to make a datastructure with a dataviewtype that could  
> > > > > possibly be referenced more than once and I don’t know if it is possible to  
> > > > > do this with linear types of ATS.
> > > > > 
> > > > > Basically I want to achieve this (this code doesn’t compile) :
> > > > > 
> > > > > #include “share/atspre\_staload\_tmpdef.hats”
> > > > > 
> > > > > staload UN = “prelude/SATS/unsafe.sats”  
> > > > > staload “prelude/SATS/reference.sats”
> > > > > 
> > > > > dataviewtype toto\_vt =  
> > > > > Toto of (Strptr1, double)  
> > > > > | Titi of (double, Strptr1)
> > > > > 
> > > > > extern fun make\_toto(i: double): toto\_vt
> > > > > 
> > > > > extern fun print\_toto(t: ref(toto\_vt)): void  
> > > > > extern fun free\_toto(t: toto\_vt): void
> > > > > 
> > > > > implement make\_toto(i) = if( i \< 10.0 ) then  
> > > > > Toto(string0\_copy(“hello”), i)  
> > > > > else  
> > > > > Titi(i, string0\_copy(" world !"))
> > > > > 
> > > > > implement print\_toto(t) = let  
> > > > > val tot: toto\_vt = !t  
> > > > > val () = (case+ tot of  
> > > > > | Toto(s, x) =\> println!("TOTO → ", s, ", ", x)  
> > > > > | Titi(x, s) =\> println!("TITI → ", s, ", ", x) ): void  
> > > > > prval () = $UN.castview0{void}(tot)  
> > > > > in end
> > > > > 
> > > > > implement free\_toto(t) = case+ t of  
> > > > > | ~Toto(s, x) =\> strptr\_free s  
> > > > > | ~Titi(x, s) =\> strptr\_free s
> > > > > 
> > > > > implement main0() = let  
> > > > > val t = make\_toto(17.0)  
> > > > > val ref1 = ref\<toto\_vt\>(t) //ref\_make\_viewptr(view@t, addr@t)  
> > > > > val ref2 = ref\<toto\_vt\>(t) //ref\_make\_viewptr(view@t, addr@t)  
> > > > > val () = print\_toto(ref1)  
> > > > > val () = print\_toto(ref2)  
> > > > > in  
> > > > > free\_toto t  
> > > > > end
> > > > > 
> > > > > I tried with reference, maybe I will have better fortune with pointer ?  
> > > > > Is it possible to do this in ATS ?
> > > > > 
> > > > > thanks a lot 😉

---

<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 15, 2013, 3:45pm UTC](https://discourse.ats-lang.org/t/get-two-refs-on-same-dataviewtype-object/323/4 "2013-10-15T15:45:13Z")

</div>

This cannot be done.  
It is like wanting to put a physical object into distinct places.

Why not do

val ref2 = ref1

Or maybe you were thinking about reference-counting?On Tuesday, October 15, 2013 3:23:44 AM UTC-4, Cyrille Duret wrote:

> hello,  
> I would like to make a datastructure with a dataviewtype that could  
> possibly be referenced more than once and I don’t know if it is possible to  
> do this with linear types of ATS.
> 
> Basically I want to achieve this (this code doesn’t compile) :
> 
> #include “share/atspre\_staload\_tmpdef.hats”
> 
> staload UN = “prelude/SATS/unsafe.sats”  
> staload “prelude/SATS/reference.sats”
> 
> dataviewtype toto\_vt =  
> Toto of (Strptr1, double)  
> | Titi of (double, Strptr1)
> 
> extern fun make\_toto(i: double): toto\_vt
> 
> extern fun print\_toto(t: ref(toto\_vt)): void  
> extern fun free\_toto(t: toto\_vt): void
> 
> implement make\_toto(i) = if( i \< 10.0 ) then  
> Toto(string0\_copy(“hello”), i)  
> else  
> Titi(i, string0\_copy(" world !"))
> 
> implement print\_toto(t) = let  
> val tot: toto\_vt = !t  
> val () = (case+ tot of  
> | Toto(s, x) =\> println!("TOTO → ", s, ", ", x)  
> | Titi(x, s) =\> println!("TITI → ", s, ", ", x) ): void  
> prval () = $UN.castview0{void}(tot)  
> in end
> 
> implement free\_toto(t) = case+ t of  
> | ~Toto(s, x) =\> strptr\_free s  
> | ~Titi(x, s) =\> strptr\_free s
> 
> implement main0() = let  
> val t = make\_toto(17.0)  
> val ref1 = ref\<toto\_vt\>(t) //ref\_make\_viewptr(view@t, addr@t)  
> val ref2 = ref\<toto\_vt\>(t) //ref\_make\_viewptr(view@t, addr@t)  
> val () = print\_toto(ref1)  
> val () = print\_toto(ref2)  
> in  
> free\_toto t  
> end
> 
> I tried with reference, maybe I will have better fortune with pointer ?  
> Is it possible to do this in ATS ?
> 
> thanks a lot 😉

---

<div class="post-metadata">

**Author:** ![Cyrille\_Duret](https://avatars.discourse-cdn.com/v4/letter/c/3d9bf3/32.png) [@Cyrille\_Duret](https://discourse.ats-lang.org/u/Cyrille_Duret)\
**Post date:** [October 15, 2013, 5:49pm UTC](https://discourse.ats-lang.org/t/get-two-refs-on-same-dataviewtype-object/323/5 "2013-10-15T17:49:58Z")

</div>

Let’s take the example of a scene management tree of a 3d engine where you  
can find several references to the same geometry so I would like to do the  
same here.  
To have one instance of geometry and several references to it on another  
data structure.

Indeed reference-counting might be the way for me. I will try it out.

thanks ;-)Le mardi 15 octobre 2013 17:45:13 UTC+2, gmhwxi a écrit :

> This cannot be done.  
> It is like wanting to put a physical object into distinct places.
> 
> Why not do
> 
> val ref2 = ref1
> 
> Or maybe you were thinking about reference-counting?
> 
> On Tuesday, October 15, 2013 3:23:44 AM UTC-4, Cyrille Duret wrote:
> 
> > hello,  
> > I would like to make a datastructure with a dataviewtype that could  
> > possibly be referenced more than once and I don’t know if it is possible to  
> > do this with linear types of ATS.
> > 
> > Basically I want to achieve this (this code doesn’t compile) :
> > 
> > #include “share/atspre\_staload\_tmpdef.hats”
> > 
> > staload UN = “prelude/SATS/unsafe.sats”  
> > staload “prelude/SATS/reference.sats”
> > 
> > dataviewtype toto\_vt =  
> > Toto of (Strptr1, double)  
> > | Titi of (double, Strptr1)
> > 
> > extern fun make\_toto(i: double): toto\_vt
> > 
> > extern fun print\_toto(t: ref(toto\_vt)): void  
> > extern fun free\_toto(t: toto\_vt): void
> > 
> > implement make\_toto(i) = if( i \< 10.0 ) then  
> > Toto(string0\_copy(“hello”), i)  
> > else  
> > Titi(i, string0\_copy(" world !"))
> > 
> > implement print\_toto(t) = let  
> > val tot: toto\_vt = !t  
> > val () = (case+ tot of  
> > | Toto(s, x) =\> println!("TOTO → ", s, ", ", x)  
> > | Titi(x, s) =\> println!("TITI → ", s, ", ", x) ): void  
> > prval () = $UN.castview0{void}(tot)  
> > in end
> > 
> > implement free\_toto(t) = case+ t of  
> > | ~Toto(s, x) =\> strptr\_free s  
> > | ~Titi(x, s) =\> strptr\_free s
> > 
> > implement main0() = let  
> > val t = make\_toto(17.0)  
> > val ref1 = ref\<toto\_vt\>(t) //ref\_make\_viewptr(view@t, addr@t)  
> > val ref2 = ref\<toto\_vt\>(t) //ref\_make\_viewptr(view@t, addr@t)  
> > val () = print\_toto(ref1)  
> > val () = print\_toto(ref2)  
> > in  
> > free\_toto t  
> > end
> > 
> > I tried with reference, maybe I will have better fortune with pointer ?  
> > Is it possible to do this in ATS ?
> > 
> > thanks a lot 😉

---

<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 15, 2013, 6:34pm UTC](https://discourse.ats-lang.org/t/get-two-refs-on-same-dataviewtype-object/323/6 "2013-10-15T18:34:31Z")

</div>

Some of the functions in

${PATSHOME}/contrib/libfloats/DATS/lamatrix.dats

makes use of refcnt.On Tuesday, October 15, 2013 2:26:16 PM UTC-4, gmhwxi wrote:

> See:
> 
> [http://www.ats-lang.org/LIBRARY/libats/refcount.html](http://www.ats-lang.org/LIBRARY/libats/refcount.html)
> 
> On Tuesday, October 15, 2013 1:49:58 PM UTC-4, Cyrille Duret wrote:
> 
> > Let’s take the example of a scene management tree of a 3d engine where  
> > you can find several references to the same geometry so I would like to do  
> > the same here.  
> > To have one instance of geometry and several references to it on another  
> > data structure.
> > 
> > Indeed reference-counting might be the way for me. I will try it out.
> > 
> > thanks 😉
> > 
> > Le mardi 15 octobre 2013 17:45:13 UTC+2, gmhwxi a écrit :
> > 
> > > This cannot be done.  
> > > It is like wanting to put a physical object into distinct places.
> > > 
> > > Why not do
> > > 
> > > val ref2 = ref1
> > > 
> > > Or maybe you were thinking about reference-counting?
> > > 
> > > On Tuesday, October 15, 2013 3:23:44 AM UTC-4, Cyrille Duret wrote:
> > > 
> > > > hello,  
> > > > I would like to make a datastructure with a dataviewtype that could  
> > > > possibly be referenced more than once and I don’t know if it is possible to  
> > > > do this with linear types of ATS.
> > > > 
> > > > Basically I want to achieve this (this code doesn’t compile) :
> > > > 
> > > > #include “share/atspre\_staload\_tmpdef.hats”
> > > > 
> > > > staload UN = “prelude/SATS/unsafe.sats”  
> > > > staload “prelude/SATS/reference.sats”
> > > > 
> > > > dataviewtype toto\_vt =  
> > > > Toto of (Strptr1, double)  
> > > > | Titi of (double, Strptr1)
> > > > 
> > > > extern fun make\_toto(i: double): toto\_vt
> > > > 
> > > > extern fun print\_toto(t: ref(toto\_vt)): void  
> > > > extern fun free\_toto(t: toto\_vt): void
> > > > 
> > > > implement make\_toto(i) = if( i \< 10.0 ) then  
> > > > Toto(string0\_copy(“hello”), i)  
> > > > else  
> > > > Titi(i, string0\_copy(" world !"))
> > > > 
> > > > implement print\_toto(t) = let  
> > > > val tot: toto\_vt = !t  
> > > > val () = (case+ tot of  
> > > > | Toto(s, x) =\> println!("TOTO → ", s, ", ", x)  
> > > > | Titi(x, s) =\> println!("TITI → ", s, ", ", x) ): void  
> > > > prval () = $UN.castview0{void}(tot)  
> > > > in end
> > > > 
> > > > implement free\_toto(t) = case+ t of  
> > > > | ~Toto(s, x) =\> strptr\_free s  
> > > > | ~Titi(x, s) =\> strptr\_free s
> > > > 
> > > > implement main0() = let  
> > > > val t = make\_toto(17.0)  
> > > > val ref1 = ref\<toto\_vt\>(t) //ref\_make\_viewptr(view@t, addr@t)  
> > > > val ref2 = ref\<toto\_vt\>(t) //ref\_make\_viewptr(view@t, addr@t)  
> > > > val () = print\_toto(ref1)  
> > > > val () = print\_toto(ref2)  
> > > > in  
> > > > free\_toto t  
> > > > end
> > > > 
> > > > I tried with reference, maybe I will have better fortune with pointer ?  
> > > > Is it possible to do this in ATS ?
> > > > 
> > > > thanks a lot 😉
