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

Is it possible to do recursive types? #37

Closed
calebsander opened this issue Sep 29, 2016 · 3 comments
Closed

Is it possible to do recursive types? #37

calebsander opened this issue Sep 29, 2016 · 3 comments

Comments

@calebsander
Copy link
Owner

calebsander commented Sep 29, 2016

It is currently impossible to instantiate a recursive type because types are immutable, so you would have to have the type already created in order to pass it into its own construction.

Idea:

  • Have a global registry of named types
  • To use a type in its own definition, would use new sb.RecursiveType(typeName)
  • Then add the type to the global registry

Example:

sb.registerType({
  type: new sb.SetType(
    new sb.RecursiveType('node')
  ),
  name: 'node'
})

When the type is written, the name (or equivalent identifier) probably has to be written with the first instance, and then when it is repeated, it would refer to that identifier to specify what type to use.

Each value write buffer would need to keep track of the positions in which recursive types' values were written. If the value was already written, a pointer would be created to it. Otherwise, the value would have to be added to the set of pointers to fill in later.

@calebsander
Copy link
Owner Author

To fix issues where a recursive type's definition contains a REPEATED_TYPE pointing to a parent of the recursive type, REPEATED_TYPE should be avoided if writing a descendant of a recursive type where it would point to before the recursive type

@calebsander
Copy link
Owner Author

Can a WeakMap be used instead of adding properties to write buffers?

@calebsander
Copy link
Owner Author

calebsander commented Oct 2, 2016

Todo still:

  • Documentation of RecursiveType and registerType(), with examples
  • Comments
  • Increase coverage
  • Version bump to 2.16.9

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