question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proposal: Inherit constructor and static methods from base Message class

See original GitHub issue

I 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:open
  • Created a year ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
smaye81commented, Nov 8, 2022

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!

0reactions
fubhycommented, Nov 7, 2022

@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 😃

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found