binarylang icon indicating copy to clipboard operation
binarylang copied to clipboard

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

Open sgmihai opened this issue 3 years ago • 3 comments

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

sgmihai avatar Dec 04 '22 11:12 sgmihai

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: https://github.com/nim-lang/Nim/pull/20480

sgmihai avatar Dec 29 '22 12:12 sgmihai

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 avatar Jan 11 '23 13:01 sealmove

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

sealmove avatar Jun 27 '23 18:06 sealmove