Proposal: Inherit constructor and static methods from base Message class
See original GitHub issueI was wondering why we are generating static methods instead of simply inheriting them from the base class. Is there a reason for that?
With these messages also also used in the frontend as part of e.g. connect-web, I think we have to be extra careful not to add too much weight especially for projects with a lot of messages.
I was thinking of doing something like this with the base Message class:
export class Message<T extends Message<T> = AnyMessage> {
static readonly runtime: ProtoRuntime;
static readonly typeName: string;
static readonly fields: FieldList;
static fromBinary<T extends Message<T>>(
this: MessageType<T>,
bytes: Uint8Array,
options?: Partial<BinaryReadOptions>
): T {
return new this().fromBinary(bytes, options);
}
static fromJson<T extends Message<T>>(
this: MessageType<T>,
jsonValue: JsonValue,
options?: Partial<JsonReadOptions>
): T {
return new this().fromJson(jsonValue, options);
}
static fromJsonString<T extends Message<T>>(
this: MessageType<T>,
jsonString: string,
options?: Partial<JsonReadOptions>
): T {
return new this().fromJsonString(jsonString, options);
}
static equals<T extends Message<T>>(
this: MessageType<T>,
a: T | PlainMessage<T> | undefined | null,
b: T | PlainMessage<T> | undefined | null
): boolean {
return this.runtime.util.equals(this, a, b);
}
constructor(data?: PartialMessage<T>) {
const self = <typeof Message<T>>this.constructor;
self.runtime.util.initPartial(data, this as any);
}
// ...
}
Are there any reasons why this wouldn’t work at runtime?
Issue Analytics
- State:
- Created a year ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
c# - What's the correct alternative to static method inheritance?
I want to create an abstract base class (let's call it a Fruit) that encapsulates some complex initialization code. This code cannot be...
Read more >Execution of static constructors with inheritance - bug or feature?
Hello experts, I used static constructors in a simple scenario with one child class inheriting from a parent class. Both classes had static...
Read more >ECMAScript proposal: private static methods and accessors in ...
This post explains private static methods and accessors in classes, as described in the ECMAScript proposal “Static class features” by ...
Read more >Inherited Static properties · Issue #43 · tc39/proposal-class-fields
I want it to equal 'hello' , but I can't find where the behavior is defined. It's likely just because I'm not familiar...
Read more >Order of Execution of Static constructor in case of inheritance ...
Hi,. I have two classes , A is base class and B is child class . Class B inherits class A . both...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Sorry for the delay. I don’t actually remember what it was either. If you are able to get around it though, as @timostamm said, we’d be all ears!
@timostamm Do you think it would make sense for me to try and experiment with this a bit? Does @smaye81 maybe remember what the limitation was or one of your colleagues? I’d be keen to give it a shot unless you can confirm it’s indeed proven to be a dead-end 😃