Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add instanceOf methods to every type. #49

Open
jhugman opened this issue Jul 31, 2024 · 0 comments
Open

Add instanceOf methods to every type. #49

jhugman opened this issue Jul 31, 2024 · 0 comments

Comments

@jhugman
Copy link
Owner

jhugman commented Jul 31, 2024

So far, Error, Object and non-flat Enum have all grown instanceOf methods.

This ticket is to generate instanceOf methods for all types:

  1. add a instanceOf(obj: any): obj is T to FfiConverter.
  • for types with existing instanceOf methods, the converter should call that type's static methods.
  • for object, the FfiConverter takes a factory which is generated. This should have the instanceOf method.
  • for primitive types, these can be hard coded e.g. typeof v === 'number'.
  • for Record, an structural types, we can implement the FfiConverter.instanceOf in terms of other FfiConverter.instanceOf methods, recursively calling instanceOf until a false is found.
  1. Add instanceOf to the decl_type_name objects: mostly, I think for Record, but there may be others.

Consider a record:

type MyRecord = { name: string, age: /*uint8/ number; };

const MyRecord = {
  create(value: {  }): MyRecord {  },
  instanceOf(obj: any): obj is MyRecord { return FfiConverterTypeMyRecord.instanceOf(obj); },
};

then, in the FfiConverterTypeMyRecord an instanceOf method can be implemented:

instanceOf(obj: any): obj is T {
  return (
    FfiConverterString.instanceOf(obj.name) && 
    FfiConverterUInt8.instanceOf(obj.age) && 
  );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant