You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
type Id<T = IdInternal> = T & number;
class IdInternal extends Uint8Array {
static create<T extends typeof IdInternal>(this: T): Id<InstanceType<T>> {
return (new this() as Id<InstanceType<T>>);
}
baseMethod () {
}
}
type NodeId = Id<NodeIdInternal>;
class NodeIdInternal extends IdInternal {
derivedMethod () { }
}
class ClaimIdInternal extends IdInternal {
anotherMethod () {}
}
class SomeOtherIdOfId extends ClaimIdInternal {
}
const x = IdInternal.create();
const y = NodeIdInternal.create();
const z = SomeOtherIdOfId.create();
// console.log(x.baseMethod());
function doSomething(x: NodeId) {
const pojo = { };
pojo[x] = 123;
}
doSomething(x); // this is a type error
doSomething(y);
This makes Id itself a generic type, and it allows all downstream classes to override instance encoding methods like toMultibase or otherwise, and also mean that NodeIdInternal.create would create Id<NodeIdInternal>, which you can match with type NodeId = Id<NodeIdInternal>.
One tradeoff is that's complex for child classes to extend any static methods of IdInternal, but that's unlikely to be needed at all. The only methods that child classes may want to extend are all the encoding methods, and potentially adding new features to their ID objects.
Note that this means right now where alot of functions hand off their decoding to idUtils, they would need to be changed to refer to the current constructor. So like fromBuffer cannot just call idUtils.fromBuffer since that's hardcoded to call IdInternal, but instead refer to this.
This could be applied to GRPCClient and its child classes as well. Right now we had to separate the creation functions with GRPCClient.createClient needing to be called by the child classes. If GRPCClient could have a static method that constructs all child classes as well, this would be nice.
However as I said it's difficult to override the parent static method with this technique, and child classes of grpc client may need to do it differently.
If MatrixAI/Polykey#333 is resolved though, there won't be a need for this issue to be applied to the grpc domain.
Specification
It would be nice for users to be able to extend
IdInternal
which can allow us to specialise the encoding functions into the child classes.However this is not really possible easily due to the fact that
Id
type is a union ofIdInternal
andnumber
.There's a potential way to do this though with "polymorphic static this" MatrixAI/Polykey#321 (comment)
This makes
Id
itself a generic type, and it allows all downstream classes to override instance encoding methods liketoMultibase
or otherwise, and also mean thatNodeIdInternal.create
would createId<NodeIdInternal>
, which you can match withtype NodeId = Id<NodeIdInternal>
.One tradeoff is that's complex for child classes to extend any static methods of
IdInternal
, but that's unlikely to be needed at all. The only methods that child classes may want to extend are all the encoding methods, and potentially adding new features to their ID objects.Note that this means right now where alot of functions hand off their decoding to
idUtils
, they would need to be changed to refer to the current constructor. So likefromBuffer
cannot just callidUtils.fromBuffer
since that's hardcoded to callIdInternal
, but instead refer tothis
.Additional context
Tasks
The text was updated successfully, but these errors were encountered: