You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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:
enumResult<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
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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