Skip to content

Is it dangerous to share references between states? #71

@symbiont-sean-lyons

Description

@symbiont-sean-lyons

When transitioning a model using Command.NextState -> commands.State, is it dangerous to share pointers or reference types between model states?

For example,

type State struct {
  index map[string]string
}

func (writeFooBarCommand) NextState(s commands.State) commands.State {
  current := s.(State)
  next := current
  next.index["foo"] = "bar"
  return next
}

I believe that the existing model state and the new model state will share a reference to the underlying map, meaning manipulations via the old state instance or the new state instance will reflect in both objects. I currently make it a habit to deep copy my model states when transitioning, but my question is whether or not the deep copy is necessary.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions