protobuf icon indicating copy to clipboard operation
protobuf copied to clipboard

Protobuf is really missing inheritance

Open zatziky opened this issue 7 years ago • 10 comments

What language does this apply to? proto3 OR proto-future

Describe the problem you are trying to solve. Lot's of DRY code in statically typed languages.

Describe the solution you'd like I'd like you to add inheritance to current proto3 or at least the future protobuf versions. An example is:

message Parent { }

message ChildA extends Parent { }
message ChildB extends Parent { }
message ChildC extends Parent { }

Alternatives you've considered I know about composition and it's really not a cure.

Additional context I know that protobuf favors composition over inheritance but it's really not a cure to remove inheritance completely. Statically typed languages like Java or C# suffer from composition only approach. These languages cannot use polymorphism because all the messages have the same parent GeneratedMessageV3. If inheritance was available, we could use polymorphism. Instead of having to write:

void method(ChildA arg)
void method(ChildB arg)
void method(ChildC arg)

we could simply write void method(Parent arg). Consequently, the missing inheritance generates a lot of DRY code is hardly or not at all DRYable. It leads to wider test suites that take more time to execute. I could go on but I hope I explained myself. (If not, just ask and I'll clarify it.)

zatziky avatar Jan 28 '19 13:01 zatziky

FWIW

To quote the Protocol Buffers tutorial: "Don't go looking for facilities similar to class inheritance, though – protocol buffers don't do that."

Instead of inheritance, Protocol Buffers tends to favor composition as a pattern.

perezd avatar Jan 29 '19 03:01 perezd

@perezd

GRPC is not doing "favor composition over inheritance" pattern, it's applying "remove inheritance" pattern. :D

There are cases when inheritance is more suitable. Just because it's a good practice to "favor somehing", it doesn't mean it should be removed completely (or not implemented at all).

Real life example

Favor public transport over driving a car when going to work

Yes, most of the time we should use public transport because it's better for Earth. On the other hand, you have cases when driving a car is a better option, e.g. if there is no public transport in your surroundings.

zatziky avatar Jan 30 '19 08:01 zatziky

How are you supposed to implement inheritance in a language that doesn't support inheritance like Rust or Go which only support struct-like data types?

ObsidianMinor avatar Jan 31 '19 02:01 ObsidianMinor

For proto3, use Any. For proto2, user extension.

TeBoring avatar Jan 31 '19 04:01 TeBoring

I want to have a base type for all server messages, request and response, then I want to have different types of requests/responses. Anyone has experimented with this idea and found any adequate way to implement that?

weitzhandler avatar Oct 10 '21 02:10 weitzhandler

I want to have a base type for all server messages, request and response, then I want to have different types of requests/responses. Anyone has experimented with this idea and found any adequate way to implement that?

For one project I did something like this to minimize redundancy:

message Request {
  uint64 magic = 1;  // Optional unique identifier for this request
  oneof _Request_oneof {
    ProfileRequest     profile_request     = 3;
    // ...other kinds of requests
  }
}

message Response {
  uint64 magic = 1;  // Your optional unique identifier echoed back so you may match our responses with your requests
  oneof _Response_oneof {
    string              error                = 2;  // Defined instead of your expected response below, on failure
    ProfileResponse     profile_response     = 3;
    // ...other kinds of responses
  }
}

message Message {
  // ... auth stuff
  repeated Request  requests       = 4;
  repeated Response responses      = 5;
  string            status         = 6;  // Protocol-level status or error message
}

So a Russian doll, basically: a Message is what's always on the wire, each can contain any number of requests and responses (they're multiplexed by magic ID to minimize the number of distinct messages being exchanged), then in the Request and Response I point to the specific one it represents. For example a ProductRequest might request the details of a product by ID and the ProductResponse would contain a Product, etc.

vphantom avatar Oct 10 '21 17:10 vphantom

totally agree with this. Lacking inheritance in my case is generating a TON of boilerplate code that I really don't want to. You are forcing us to move complexity (and performances bottleneck) from the protocol to the application.

fer-marino avatar Mar 11 '25 14:03 fer-marino

Also agree - we have various base classes to represent our request and response objects and composition will be a huge lift.

Why is this issue closed as “completed” by the way? Are we saying this is implemented or that it will never be implemented?

igalnassi avatar Apr 21 '25 12:04 igalnassi

+1

fawzibazari avatar Nov 17 '25 12:11 fawzibazari

I've been using complex inheritance with protobuf and it works without issues. Alas I've been using Protobuf code-first in .NET, so there's no really proto files, but it take it this is possible.

weitzhandler avatar Nov 17 '25 18:11 weitzhandler