Sorry for the title being vague, but as it often happens to me, I really don't
know how to summarize this...
I'm trying to make a messaging/notification system for ui elements to tell the
data what to do. To make those notifications, I'm using that trick with enum
and case/of.
type
ActionKind = enum
LayerToggleVisible #, (...)
Action = ref object
layer:int # <-- more on this below
case kind:ActionKind
of LayerToggleVisible: vis:bool
# (...)
# and then elsewhere I build a ui notification like you would expect
Action(
kind:LayerToggleVisible,
layer:index,
vis:false
)
Run
There's a few problems: not all kinds care about the **layer** index. I know I
could do this...
# (...)
of LayerAddRemove, LayerToggleVisible, LayerRename, LayerChangeAlgo:
layer:int
Run
... but some of those kinds have to contain other values of their own, so afaik
I can use that. So I just made the **layer** index exist for all kinds, and
made different identifiers for each case. It would be handy to have overlapping
fields, but like the layer field, they don't all converge in the same kinds.
case kind:ActionKind
of LayerAddRemove: add:bool # bool ( these two could
of LayerToggleVisible: vis:bool # bool go together )
of LayerRename: new_name:string # string
of LayerChangeAlgo: algo:int # int
Run
In python I would use dictionaries, because they allow creating arbitrary
fields of arbitrary types in them on the fly. I wonder if something can be done
to make this mimic that behavior more closely.
Ultimately I could just define a type with a whole bunch of fields and use them
or ignore them as needed. Something like:
Action = ref object
name:string #<-- name would be used in a case/of, to check what
action was requested and the respective value to check
layer:int
b:bool
i:int
s:string
pt:tuple[x:int,y:int]
new_name:string
algo:Algorithm
add:bool
vis:bool
Run
Would this be inadvisable?