You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A few weeks ago, a generic encode method was added to the toml module (1bed0b5). While this is pretty useful, the implementation is flawed and it is no longer possible to encode complex types that have a custom to_toml method. This is caused by the default encoding method being always part of the encode method. This method expects types to be encodeable and only specific types are allowed (need to be castable to toml.Any at some point of time). However, from my point of view, each type should be encodable as long as it defines a to_toml method.
If a to_toml method exists, the compile time statements in the beginning create the proper return value. However, the rest of the function seems to be still evaluated by the type checker and causes the error. The most obvious solution would be to prevent compilation of the latter part, if a to_toml method has been found.
I thought about possible implementations, but it seems not that easy. What I could think of is an $if implements operator, which could be used to replace the for loop with an if else condition (after creating an Interface like CustomEncoder that requires implementors to define a to_toml method). Another possible solution would be something like a $break statement, that drops all following definitions within a function once it is hit. No idea if this makes sense or not 🤷
That's true. We really need to make sure it short circuits after the return without doing the rest of the comptime evaluation.
Doing some quick tries I can't force it to early return without the comptime evaluation.
Improving the generic encoding function to not run into the type error can fix this as well, but stoping further evaluation after a return would be 💯
Sorry for the late response. #19338 indeed solved the edge case given above. However, I only chose this edge case to provide a basic example. My actual problem is related to the map type not being handled correctly, which still causes compile time errors in the latest version.
Based on #19338 I just submitted #19408 which catches my edge case and hopefully most others 🙂
Describe the bug
A few weeks ago, a generic
encode
method was added to thetoml
module (1bed0b5). While this is pretty useful, the implementation is flawed and it is no longer possible to encode complex types that have a customto_toml
method. This is caused by the default encoding method being always part of theencode
method. This method expects types to be encodeable and only specific types are allowed (need to be castable totoml.Any
at some point of time). However, from my point of view, each type should be encodable as long as it defines ato_toml
method.Reproduction Steps
The following code lists a minimal example:
Expected Behavior
The above example should compile, as the
toml.encode
method should return[This is Valid]
, which is valid toml produced by the customto_toml
method.Current Behavior
The following compile time error is thrown:
Possible Solution
The problem is caused by the following function
v/vlib/toml/toml.v
Line 80 in 1bed0b5
If a
to_toml
method exists, the compile time statements in the beginning create the proper return value. However, the rest of the function seems to be still evaluated by the type checker and causes the error. The most obvious solution would be to prevent compilation of the latter part, if ato_toml
method has been found.I thought about possible implementations, but it seems not that easy. What I could think of is an
$if implements
operator, which could be used to replace the for loop with an if else condition (after creating an Interface likeCustomEncoder
that requires implementors to define ato_toml
method). Another possible solution would be something like a$break
statement, that drops all following definitions within a function once it is hit. No idea if this makes sense or not 🤷Additional Information/Context
No response
V version
V 0.4.1 3042857.f18086c
Environment details (OS name and version, etc.)
Not relevant
The text was updated successfully, but these errors were encountered: