How to create a vector containing many data types without using "Any" and without creating a new abstract type

I would like to create a vector in Motoko that can contain different data types symultaneously without creating an abstract type. For instance:

let v : [Any] = ["1",2,"3",4];

It seems that I can do that, but I would like to further constrain it so instead of Any I am only allowed to use Float and Text. Additionally I would like to avoid creating a new abstract type like:

type newtype = {
    #number : Float; // variant 
    #symbol : Text; 
  };
vv: [newtype] ...

Is it possible to avoid creating the abstract type and still constrain vector content?

You might try “Float or Text”, but his is going to complicate your programming later because of the lack of reflection. Unless you have a really good reason I’d use variants.

I’d suggest an array of candy values with a. Small class around it that validates that the values going in are the types you want.

1 Like