Genric Enum ic Cap'p Proto

29 views
Skip to first unread message

Mostafa Sedaghat joo

unread,
May 10, 2020, 11:35:30 AM5/10/20
to Cap'n Proto
AS Cap'p Scheme Language says: "A struct or interface type may be parameterized, making it “generic”."

I am wondering is it possible (technically) to have Generic Enums?
In Rust, this is very common that a method can return "Result" Generic Enum, that can be an error or a generic value:

enum Result<T, E> {
 
Ok(T),
 
Err(E),
}

If technically it is not possible to define a generic enum, what is the bests practice of define this function in Cap'n Proto:

fn foo() -> Result<String, Error> {
   
...
}


Ian Denhardt

unread,
May 10, 2020, 12:54:06 PM5/10/20
to Cap'n Proto, Mostafa Sedaghat joo
The closest analogue to Rust's enums is a union. You could define
`Result` like:

struct Result(T, E) {
union {
ok @0 :T;
err @1 :E;
}
}

Note however that rust-style error checking has problems in an rpc
context, in particular using this as a return value will defeat promise
pipelining, so if you intend on calling methods on the value wrapped in
`ok`, you may not want to do this if latency is a concern, using
capnproto exceptions instead. The rust implementation maps these to rust
`Result`s when you wait for a promise anyway.

One downside of using capnproto exceptions is that you don't have
the ability to define custom error types; there are a fixed set defined
by the RPC protocol and you can attach a string to them, but you can't
really do "structured" errors.

-Ian

Quoting Mostafa Sedaghat joo (2020-05-10 11:35:29)
> AS [1]Cap'p Scheme Language says: "A struct or interface type may be
> --
> 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 [2]capnproto+...@googlegroups.com.
> To view this discussion on the web visit
> [3]https://groups.google.com/d/msgid/capnproto/5515b78c-8da3-4fc8-9fb3-
> 93ff7c21ea26%40googlegroups.com.
>
> Verweise
>
> 1. https://capnproto.org/language.html#generic-types
> 2. mailto:capnproto+...@googlegroups.com
> 3. https://groups.google.com/d/msgid/capnproto/5515b78c-8da3-4fc8-9fb3-93ff7c21ea26%40googlegroups.com?utm_medium=email&utm_source=footer
Reply all
Reply to author
Forward
0 new messages