capnproto / capnproto

Cap'n Proto serialization/RPC system - core tools and C++ library
https://capnproto.org
Other
11.71k stars 927 forks source link

Provide a way to specify type ids in constants. #926

Open zenhack opened 4 years ago

zenhack commented 4 years ago

It's not uncommon to define structs where one of the fields is supposed to be a type id for some other type, but afaik there's currently no way in the schema language to just say "use the id of this type" in a constant definition. This affects Sandstorm's powerbox requests, since if they are generated from schema files they end up looking like this:

const descriptor :Powerbox.PowerboxDescriptor = (
  tags = [
    (
      id = 0xc879e379c625cdc7,
      # The type id for ApiSession.

      value = .tagValue
    ),
  ],
);

Developers shouldn't have to look up and insert the magic constant themselves. Ideally you could just write:

const descriptor :Powerbox.PowerboxDescriptor = (
  tags = [
    (
     id = ApiSession,
     value = .tagValue
    ),
  ],
);

i.e, if a type name is used in a context where a value is expected, treat is as the type id of that name.

kentonv commented 4 years ago

I'd use an intrinsic function like typeId(ApiSession), but otherwise I agree this would be very useful.

(Or, another alternative might be to introduce a TypeId built-in type, which is encoded like UInt64, but uses type names as its literal values. But that'd be more complicated to implement.)