-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
invalid object assignment when converting types #11592
Comments
Types can be "convertible" but the conversion can fail at runtime. Which is what happens here and it's correct. Previously it went undetected. |
Why does it fail? It had predictable behaviour: everything in the child that's not part of the parent is dropped, and in the other way, those same fields are set to default values. How does one know now what kind of conversions are valid and which are not? Since it fails at runtime, it seems it's not a type issue. |
Notice that here only the last line fails type
Base = object of RootObj
name: string
Child = object of Base
age: int
proc print(b: Base) =
echo b.name
let child = Child(name: "foo", age: 42)
print(child) # this is ok
print(Base(child)) # strangely, this is also ok
let b = Base(child) # only when assigning, we have a failure |
Returning it from a procedure also fails, even if not assigning proc convert (b: Base): Base = return b
echo convert(child) |
Whenever data would be lost it fails. Consistent with all the other allowed but checked conversions. |
print(Base(child)) How is this not an example of the loss of data? |
Because it's not stored anywhere? |
According to the convertible relations section, a child type should be explicitly convertible to it's base type.
Although the code does compile, at runtime it throws an
ObjectAssignmentError
. It should either not compile at all, or run correctly.Example
Current Output
Expected Output
Additional Information
It was working in a previous release, I don't know how long ago, but at least beginning 2018
Backwards
It doesn't work backwards either, from child to parent. Fails at runtime too rather than at compile. I don't know if this one worked before, but according to the manual it should too, I believe.
The text was updated successfully, but these errors were encountered: