what is the syntax for declaring pointers

21 views
Skip to first unread message

nikos efthias

unread,
May 4, 2020, 6:01:45 PM5/4/20
to Cap'n Proto
I have a Struct containing generics which accepts another struct such as
```
struct foo (a){
    field @0 :a
}

struct b{}

struct c{
field:List(a(b)
}
```
the example above wont work because I need to pass a b pointer to a but There is no documentation for defining pointers
I tried *b and &b which does not work.
How do I define the pointer?

Ian Denhardt

unread,
May 4, 2020, 7:12:40 PM5/4/20
to Cap'n Proto, nikos efthias
"Pointers" aren't an explicit thing in the schema language. Data, Text,
structs, lists and interfaces are implicitly pointers, while primitive
types like Bool and Float64 are not. Type parameters must always be
pointer types. So in this case you can just do:

struct Foo(A) {
field @0 :A;
}

struct B{};

struct C {
field :List(B);
}

-Ian

Quoting nikos efthias (2020-05-04 18:01:44)
> --
> You received this message because you are subscribed to the Google
> Groups "Cap'n Proto" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to [1]capnproto+...@googlegroups.com.
> To view this discussion on the web visit
> [2]https://groups.google.com/d/msgid/capnproto/bd400995-9d98-4caf-8e10-
> 81a3fe1de564%40googlegroups.com.
>
> Verweise
>
> 1. mailto:capnproto+...@googlegroups.com
> 2. https://groups.google.com/d/msgid/capnproto/bd400995-9d98-4caf-8e10-81a3fe1de564%40googlegroups.com?utm_medium=email&utm_source=footer
Message has been deleted

nikos efthias

unread,
May 4, 2020, 7:21:02 PM5/4/20
to Cap'n Proto

I need a List(foo(b)) that is the problem capnp compile command generates an error sayingI need to pass pointers rather than b itself
so I cant compile my schema because it need a pointer type at compile time and you are saying there is no such thing in capnproto which makes using generics impossible in nested levels as i understand

>    an email to [1]capn...@googlegroups.com.
>    To view this discussion on the web visit
>    [2]https://groups.google.com/d/msgid/capnproto/bd400995-9d98-4caf-8e10-
>    81a3fe1de564%40googlegroups.com.
>
> Verweise
>

Kenton Varda

unread,
May 4, 2020, 7:29:29 PM5/4/20
to nikos efthias, Cap'n Proto
Hi Nikos,

If you'd like us to help you, you need to provide the exact code you actually wrote, and the exact error it produced.

The code you provided in your first e-mail has numerous syntax errors such that it can't compile as-is. When I fixed those errors, it worked fine. That is, if I put the following in a file and compile it, I get no errors:

@0xc39ccf0ba082696f;

struct Foo(A) {
  field @0 :A;
}

struct B {}

struct C {
  field @0 :List(Foo(B));
}

So, I do not know what problem you are actually having.

-Kenton

To unsubscribe from this group and stop receiving emails from it, send an email to capnproto+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/capnproto/d4352bf6-762a-4367-b26e-ed0f6be92018%40googlegroups.com.

nikos efthias

unread,
May 4, 2020, 7:36:19 PM5/4/20
to Kenton Varda, Cap'n Proto
Hi,
@0x8bc5b84ae7dd5db8;

struct Game{
id @0 :UInt32;
c1 @1 :List(UInt32);
c2 @2 :List(UInt32);
t @3 :List(UInt32);
balls @4:List(UInt8);
cardState @5:CardStateMap;
cardIndexByNumber @6:Data;
}
struct CardStateMap {
contents @0:List(KV(UInt8,CardState));
struct CardState{
r1 @0:UInt8;
r2 @1:UInt8;
r3 @2:UInt8;
}
}

struct KV(K,V){
k @0:K;
v @1:V;
}

the code above is the exact code from my file and when I try to compile I get the following error 

Kenton Varda

unread,
May 4, 2020, 7:48:46 PM5/4/20
to nikos efthias, Cap'n Proto
Hi Nikos,

The problem is this line:

    contents @0:List(KV(UInt8,CardState));

UInt8 is not a pointer type, so you can't use it as a type parameter. You will need to box it, like:

    struct UInt8Box {
      value @0 :UInt8;
    }

Then you can do:

    contents @0:List(KV(UInt8Box,CardState));

-Kenton

nikos efthias

unread,
May 4, 2020, 7:54:47 PM5/4/20
to Kenton Varda, Cap'n Proto
It seems to use specific version of KV for every place where there might be generics is a better option then this.
I believe on rust side of things it will also look more clear instead of creating wrappers for everything.
In what cases it is better to use generics then ? 
171e214bbe58c38b3171

Kenton Varda

unread,
May 4, 2020, 8:01:21 PM5/4/20
to nikos efthias, Cap'n Proto
On Mon, May 4, 2020 at 6:54 PM nikos efthias <nikos....@gmail.com> wrote:
It seems to use specific version of KV for every place where there might be generics is a better option then this.

That may be true, depending on your efficiency needs.
 
I believe on rust side of things it will also look more clear instead of creating wrappers for everything.
In what cases it is better to use generics then ? 

Generics work great when the input type is always expected to be a struct (or list or Text or Data).

Unfortunately, supporting non-pointer types (which may have varying sizes) is challenging. The reasons for this were discussed in another recent thread:

171e214bbe58c38b3171

nikos efthias

unread,
May 4, 2020, 8:34:26 PM5/4/20
to Kenton Varda, Cap'n Proto
thanks
171e21f84b1369f04df1

Ian Denhardt

unread,
May 4, 2020, 9:35:44 PM5/4/20
to Cap'n Proto, nikos efthias
This works on my end:

@0xdebdf27f66c354b6;

struct Foo(A) {
field @0 :A;
}

struct B {}

struct C {
field @0 :List(Foo(B));
}

Including the exact error message would make it easier to help.

-Ian

Quoting nikos efthias (2020-05-04 19:17:51)
> I need a List(foo(b)) that is the problem capnp compile command
> generates an error saying this is impossible
>
> On Mon, May 4, 2020 at 7:12 pm, Ian Denhardt <i...@zenhack.net> wrote:
>
> "Pointers" aren't an explicit thing in the schema language. Data, Text,
> structs, lists and interfaces are implicitly pointers, while primitive
> types like Bool and Float64 are not. Type parameters must always be
> pointer types. So in this case you can just do: struct Foo(A) { field
> @0 :A; } struct B{}; struct C { field :List(B); } -Ian Quoting nikos
> efthias (2020-05-04 18:01:44)
>
> I have a Struct containing generics which accepts another struct
> such as ``` struct foo (a){ � � � field @0 :a } struct b{}
> struct c{ field:List(a(b) } ``` the example above wont work because
> I need to pass a b pointer to a but There is no documentation for
> defining pointers I tried *b and &b which does not work. How do I
> define the pointer? -- You received this message because you are
> subscribed to the Google Groups "Cap'n Proto" group. To unsubscribe
> from this group and stop receiving emails from it, send an email to
> [1][1]capnproto+...@googlegroups.com. To view this
> discussion on the web visit
> [2][2]https://groups.google.com/d/msgid/capnproto/bd400995-9d98-4caf
> -8e10- 81a3fe1de564%40googlegroups.com. Verweise 1.
> [3]mailto:capnproto+...@googlegroups.com 2.
> [4]https://groups.google.com/d/msgid/capnproto/bd400995-9d98-4caf-8e
> 10-81a3fe1de564%40googlegroups.com?utm_medium=email&utm_source=foote
> r
>
> --
> You received this message because you are subscribed to the Google
> Groups "Cap'n Proto" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to [5]capnproto+...@googlegroups.com.
> To view this discussion on the web visit
> [6]https://groups.google.com/d/msgid/capnproto/R1YT9Q.RHGO2QSOY5ZV1%40g
> mail.com.
> 3. mailto:capnproto+...@googlegroups.com
> 4. https://groups.google.com/d/msgid/capnproto/bd400995-9d98-4caf-8e10-81a3fe1de564%40googlegroups.com?utm_medium=email&utm_source=footer
> 5. mailto:capnproto+...@googlegroups.com
> 6. https://groups.google.com/d/msgid/capnproto/R1YT9Q.RHGO2QSOY5ZV1%40gmail.com?utm_medium=email&utm_source=footer
Reply all
Reply to author
Forward
0 new messages