Skip to content
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

Generic Inheritance-tree misbehavior #5687

Open
dustinlacewell opened this issue Apr 9, 2017 · 1 comment
Open

Generic Inheritance-tree misbehavior #5687

dustinlacewell opened this issue Apr 9, 2017 · 1 comment
Labels
Inheritance stale Staled PR/issues; remove the label after fixing them

Comments

@dustinlacewell
Copy link
Contributor

dustinlacewell commented Apr 9, 2017

Preface

Assuming you have the sdl2 and perlin packages installed:

  • install commit 46e66e41f7205b9f7f292d5fb27d68961517abeb of https://github.com/dustinlacewell/dadren with nimble install
  • from dadren/examples/bsp2 execute make run
  • use the h, v, and d keys along with the mouse to interact with the demo.

The demo implements a 2D binary-space-partition graph. That is for a given 2D rectangle, split it either vertically or horizontally and get two new rectangles. Repeat.

The implementation of the binary tree is here: https://github.com/dustinlacewell/dadren/blob/46e66e41f7205b9f7f292d5fb27d68961517abeb/dadren/bsp.nim

It implements a simple inheritance tree:

BSPNode[T] -> Leaf[T]   # contains actual T content
BSPNode[T] -> ParentNode[T] -> VSplit[T] # has two BSPNode children
BSPNode[T] -> ParentNode[T] -> HSplit[T] # has two BSPNode children

Intended Implementation

The example that uses this BSP implementation is here: https://github.com/dustinlacewell/dadren/blob/46e66e41f7205b9f7f292d5fb27d68961517abeb/examples/bsp2/main.nim

In that example Color a simple ref object type is used as the T content for building the BSPTree. This all works as expected.

The BSPNode base type defines the parent field as BSPNode. This is not the ideal expression of the program's design. Only a ParentNode should ever be a parent in the graph. Therefore it would be ideal to have the BSPNode.parent field by of type ParentNode. This would avoid the otherwise unnecessary casts anytime the parent field is accessed:

https://github.com/dustinlacewell/dadren/blob/46e66e41f7205b9f7f292d5fb27d68961517abeb/dadren/bsp.nim#L134
https://github.com/dustinlacewell/dadren/blob/46e66e41f7205b9f7f292d5fb27d68961517abeb/dadren/bsp.nim#L160
https://github.com/dustinlacewell/dadren/blob/46e66e41f7205b9f7f292d5fb27d68961517abeb/dadren/bsp.nim#L172
https://github.com/dustinlacewell/dadren/blob/46e66e41f7205b9f7f292d5fb27d68961517abeb/dadren/bsp.nim#L177

Problem

When changing the BSPNode.parent field from type BSPNode to ParentNode the following error is generated:

bsp.nim(30, 20) Error: inheritance only works with non-final objects

With some exploration an unusual result was discovered. Make the following changes:

  • change the base type of Color from ref object => ref object of RootObj
  • add a field to color: parent: float

The compiler error is now:

bsp.nim(31, 17) Error: type mismatch: got (BSPNode[]) but expected 'float'

This would imply that, even though no type in the BSPNode hierarchy inherits from T, the compiler is some how conflating T into the resolution of BSPNode fields.

@stale
Copy link

stale bot commented Aug 4, 2020

This issue has been automatically marked as stale because it has not had recent activity. If you think it is still a valid issue, write a comment below; otherwise it will be closed. Thank you for your contributions.

@stale stale bot added the stale Staled PR/issues; remove the label after fixing them label Aug 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Inheritance stale Staled PR/issues; remove the label after fixing them
Projects
None yet
Development

No branches or pull requests

2 participants