-
Notifications
You must be signed in to change notification settings - Fork 200
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
How to make type of a field depend on field in another variable #106
Comments
First of all, you've probably meant Then you'll encounter that you can't have duplicate fields with the same ID. There are two solutions available right now:
seq:
- id: offset8
type: u8
if: '_root.f_version > 1000000'
- id: offset4
type: u4
if: '_root.f_version <= 1000000'
instances:
offset:
value: '_root.f_version <= 1000000 ? offset4 : offset8'
seq:
- id: offset
type:
switch-on: offset_type
cases:
4: u4
8: u8
instances:
offset_type:
value: '_root.f_version <= 1000000 ? 4 : 8' I personally like second solution better. If you want to go fancy there, you can declare a enum of possible |
Thank you for the answer. instances:
offset_type:
value: '_root.f_version <= 1000000 ? 4 : 8' I got
The first solution works. |
I've just tried exactly this .ksy: meta:
id: foo
endian: le
seq:
- id: f_version
type: u4
- id: my_offset
type: offset_obj
types:
offset_obj:
seq:
- id: offset
type:
switch-on: offset_type
cases:
4: u4
8: u8
instances:
offset_type:
value: '_root.f_version <= 1000000 ? 4 : 8' It compiles without errors. Does it work for you?
I don't really like the idea of having a syntax where semantics of the same Actually, if we'll go back to #10, there were some purposes to do
That's generally a bad idea from performance POV. |
Yes, it does. But it doesn't if I insert your type into
Yes, it's true for the cases of small variables. |
Did you notice that the order of evaluation matters, for example:
What about recursive calls, like
Are you sure, that KSC can (easily) implement such syntax for expressions in all supported languages? |
@mbrudka There's no magic. "Order of evaluation" problem is (sort of) solved by the fact that all instances are lazy by default and order of evaluation would be determined in runtime. Attempting to do infinitely recursive things will break due to inability to infer types. |
@GreyCat. Certainly, all described problems are solvable, but at the price of complexity. My proposition is just to keep KaitaiStruct simple without any additional contracts on the ways and results of evaluation. Let the developer of the protocol do some stuff, code the application and ensure that the frame is semantically proper. |
It looks like all the answers have been given, no further discussion occurred. Closing, feel free to reopen if any questions still remain. |
causes
KS compilation error: java.lang.RuntimeException: Unable to access root in List(root, offset_ver) context
How to fix?
The text was updated successfully, but these errors were encountered: