Search code examples
variableslanguage-agnostic

can a variable have multiple values


In algebra if I make the statement x + y = 3, the variables I used will hold the values either 2 and 1 or 1 and 2. I know that assignment in programming is not the same thing, but I got to wondering. If I wanted to represent the value of, say, a quantumly weird particle, I would want my variable to have two values at the same time and to have it resolve into one or the other later. Or maybe I'm just dreaming?

Is it possible to say something like i = 3 or 2;?


Solution

  • You can't do this with native types, but there's nothing stopping you from creating a variable object (presuming you are using an OO language) which has a range of values or even a probability density function rather than an actual value.

    You will also need to define all the mathematical operators between your variables and your variables and native scalars. Same goes for the equality and assignment operators.

    numpy arrays do something similar for vectors and matrices.