Discriminatory unions and inheritance

I am looking to create a scene graph for my F # somthing like project:

root
->player_bob
-->torch
->enemy_1
-->extravagant_hat
-->enemies_cat_jess 
--->fleas
--->fur_ball
->loot

etc., etc.

Each element must contain a collection of game objects to represent its children.

For example, the list of enemies1 contains a cat and a hat, and the list of cats contains fleas and a fur ball

Therefore, I plan to make them all inheritable from the class containing the collection that describes these objects.

Now to my question: Should I drop child objects in GameObject and store them in the list of the base class "GameObject" OR create a discriminatory union, for example

type SceneObject = 
        |Character of Character //e.g player, enemy, cat
        |Item of Item //e.g hat, torch, furball

"SceneObjects", / .. , , / : , ..

+ - ; , FP, , , , .

,

JD

+3
2

.

type SceneObject = 
    | Character of <characterData> * (SceneObject list) 
    | Item      of <itemData>      * (SceneObject list)

let root = [Character("Bob", [Item("Torch", []); ...]); ...]
+9

, , (, ) , . :

type SceneObject =
  | Character of <characterData>
  | Item of <itemData>

type ObjectWithChildren = 
  { Object : SceneObject
    Children : ObjectWithChildren list }

- , , . , , - :

let rec traverse tree = 
  // Do something with tree.Object
  for child in tree.Children do traverse child

SceneObject, (, Item Character).

+7

Source: https://habr.com/ru/post/1762133/


All Articles