-
Notifications
You must be signed in to change notification settings - Fork 201
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
Signatures as a special case of enums #434
Comments
I don't think I understand this proposal. For starters, I don't understand what "allowing a value of any type to be enum" means. Vast majority of our target languages treats enums as an integer-based type with a set of predefined, namespaced integer constants. There are no other "enums", and I don't understand why we should invent & emulate them, and even if we will, why we should name these things "enums", as it would only confuse end-users. As far as I understand, what you imply is:
|
Let's look what enums are. They are often not quite numbers, but just unique identifiers, which exact numeric value doesn't matter, what matter are relations "the same as" and "contains" (usually achieved with bit operations). I mean a programmer usually uses enums in the following cases: So sometimes we don't need their underlying types.
enum:
a:
"'chck'": check than enum:
a:
0x6b636863: check #chck
I guess these generic enums will be only in Kaitai Struct, but in underlying languages something more suitable for that language (maybe a enum, but not necessary) would be used.
Because they are compared like enums and we think of them as enums.
I have not said that. I just run into something which may cover it use case and some other. What I don't like, it is current enum implementation since it is not consistent across languages: the parser from the same ksy compiled into JS will parse a file successfuly, but compiled into python will raise an error.
Of course I do. Don't you?
Not quite. I mean that now there are different formats with different specs. In some formats if you encounter an unknown frame type id you cannot parse the file any further because you don't know the size of a frame, in another ones you can just ignore the frames with unknown to you type identifiers. So a ksy developer needs control on what to do in the case of an uexpected value. Now we don't have. Instead it depends entirely on language. So we need to fix this. But once implemented, it turns out that In synalysis grammar language there are 2 types of enums. But it is a bit restricted, I guess we may do better. A more flexible system would be if we are able to check if a value has been matched to a enum not only from that field, but also from other ones. I thought about it one little bit more and now explicit check doesn't look such a good idea: throw:
if: F(a._enum_valid, x) where |
Working on #383 I found that that language doesn't have a distinct tag for a signature. Instead it has 2 types of enums:
mustmatch="yes"
andmustmatch="no"
. And this allows to match against multiple signatures. For now we don't have this distinction, instead the behavior is defined by runtime, for example for JS runtime it's OK to have properties not be a valid enum value, but python runtime raises an error in this case.So the proposal
1 Allow a value of any type to be a enum. For example strings.
2 Widen the set of possible values for a
enum
. If the value is a string, it's a enum name. If the value is an array, it's an array of values of the same type as the property is marked with (the behavior somehow consistent tocontents
).3 Introduce way to check if the matched value a valid enum value. Maybe as
_enum_valid
?4 So with #81 implemented we will be able to throw on incorrect signatures and / or enum values.
5 once it is implemented, deprecate
contents
, automatically transform it into a new equivalent and remove it.The text was updated successfully, but these errors were encountered: