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

Implement a constructor that allows initialization of the object with the assertion expressions defined in the struct #26

Open
sgmihai opened this issue Dec 4, 2022 · 3 comments
Labels
enhancement New feature or request

Comments

@sgmihai
Copy link

sgmihai commented Dec 4, 2022

For expresiveness and consistency, it would be nice to use the binarylang's data structure definitions, to define outgoing only data in a network protocol, that does not need to be parsed in any way, just serialized. We can do this, but the object variable still has to be initialized with the values we want, and thus the assertion expressions are just comsetic in this example. I'd like to be able to do something like

let myData = DataObject.initAssertions()

  struct(udpTrackerPing, endian = b):
    64: connection_id = 0x41727101980
    32: action = 0
    32: transaction_id = rand(int32)
  let ping = UdpTrackerPing(connection_id: 0x41727101980, action: 0, transaction_id: rand(int32))
@sealmove sealmove added the enhancement New feature or request label Dec 5, 2022
@sgmihai
Copy link
Author

sgmihai commented Dec 29, 2022

On second thought, there are cases where one might want to initialize the object with different default values than the ones used for assertions. I believe there should be some special syntax to allow defining default values. Nim also recently got support merged for default value in objects: nim-lang/Nim#20480

@sealmove
Copy link
Owner

Sounds like a useful feature. My only concern is that, this essentially it requires extra codegen - some kind of initializer proc. I don't want to bloat codegen with something like this, because most people will probably not need it, but binarylang will still generate it for every single parser definition.

The next big planned change for binarylang is to split codegen into loosely-coupled components, and allow the used to configure which components to use. Once this component-based system is implemented, this feature can be a separate component.

@sealmove
Copy link
Owner

Update: Binarylang now supports plugins as a parser option, so this feature can be implemented as such.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants