Questions tagged [variant]

A variant data type is a tagged union that holds other data types. It is a standard data type in [ocaml], and typically used for interop calls between languages ([c++] and [vb6]) in classic Microsoft Windows [com] programming. It also exists in other languages using other names, such as [discriminated-unions] or the more general concept of [algebraic-data-types]

In , the variant type is a basic building block of the language. Custom variants are defined with a set of constructors that restrict which types it can hold, and are used to inspect it in and to warn if a pattern match does not cover all constructors.

Questions that should have this tag would include ones involving how to pass data between Windows processes or within the same process using and classic function calls. Often the calls occur between languages (e.g. to ), but they are also used in single language programs (e.g. ) to achieve a plug-in, modular architecture with DLL files.

A variant is technically a structure holding a union of data types along with a VARTYPE that indicates which member of the structure is valid. It can hold any other data type, including primitive types (BYTE, LONG), pointers to primitive types (LONG*, FLOAT*), IDispatch pointers, and even pointers to other variants.

There are a couple of helper classes Microsoft has created to help with the details of dealing with raw variants:

These classes relieve much of the grunt work of interop programming with variants such as doing deep destroys on contained SAFEARRAYs.

Definitions

  • Tagged union: A data structure that can be used to hold a number of different data types, only one of which is valid at a time. There is a field (VARTYPE) which indicates the valid data member (https://en.wikipedia.org/wiki/Tagged_union)

Examples

OCaml

(* Definition of the built-in option type *)
type 'a option =
| Some of 'a
| None

(* Construction *)
let var = Some "hello"

(* Inspection through pattern matching *)
let greeting =
  match var with
  | Some str -> str
  | None -> "howdy"

Visual Basic

Dim var as Variant

var = "hello"   'Variant holds a string

var = 7         'Variant holds an Integer

C++

_variant_t var;

var = "hello";  //Variant holds a string

var = 7;        //Variant holds an int

Resources

Related tags

1152 questions
6
votes
3 answers

OCaml function over polymorphic variants not sufficiently polymorphic?

OCaml gives function `A -> 1 | _ -> 0 the type [> `A] -> int, but why isn't that [> ] -> int? This is my reasoning: function `B -> 0 has type [<`B] -> int. Adding a `A -> 0 branch to make it function `A -> 1 | `B -> 0 loosens that to [<`A|`B] ->…
sin3.14
  • 353
  • 3
  • 6
6
votes
1 answer

Bad conversion from EndOfTheMonth(date) to Variant value

I have a TDateTime value (that I get as result from EndOfTheMonth(date)) to a variant type. The result is wrongly rounded. Let's have a look at example: data := EndOfTheMonth(date); V := data; ShowMessage(DateTimeToStr(data) + ' vs ' +…
JustMe
  • 2,329
  • 3
  • 23
  • 43
5
votes
1 answer

Type definitions with open unions

1) I have an open union defined as follows: type 'a choice = [> `One | `Other ] as 'a I then try to define a type choice_list: type choice_list = choice list which does not work. How does one define types where one or more of the components are…
ZJanes
  • 101
  • 1
5
votes
2 answers

Using open unions outside the module they were defined in

Why does this work fine? module Account = struct type account_type = Current of float | Savings of float end let sarah = Account.Current 100.0;; While the final line in the following produces an Error: syntax error? module Account = struct …
ZJanes
  • 101
  • 1
5
votes
5 answers

Access Query Error - Null & Variant Data Types - How do I fix this error?

All, This error is driving me insane. I've spent 2 hours trying to figure it out and/or work around it with no luck. Here's the error: "You tried to assign the NULL value to a variable that is not a Variant data type." Here's my SQL: SELECT…
Noah
5
votes
2 answers

Variant like in C++ for Julia

How can I use use something like variant (which is in C++) in Julia? In C++, I can do something like this variant value; How can I do the same thing in Julia for a parameter of a function? function func(??) # something end Sorry if…
pooooky
  • 490
  • 2
  • 12
5
votes
3 answers

C++: Can std::variant hold vector, map, and other containers?

According to cppreference, variant is not allowed to allocate dynamic memory. This suggests that variant should not have dynamically-allocated containers as template, like vectors and maps. Yet, some say that it is possible to have a vector as a…
Dave N.
  • 138
  • 1
  • 8
5
votes
2 answers

Update a mixed and nested object in Snowflake

I have a Snowflake table with one variant column (raw). Every row in this table is complex (both dictionaries and arrays) and nested (multiple hierarchies). What I want to do is to be able to update a specific item in some array. It will be easier…
5
votes
1 answer

Is it possible to use std::visit without lambdas (just a simple class)?

I'm trying to experiment with making a visitor with just a simple class, with overloads of operator() for each type in the variant. I expected this to work (as it's how I assumed it worked for lamdba functions): #include #include…
user3818491
  • 498
  • 1
  • 6
  • 16
5
votes
2 answers

How to prune a Java program

Let's me start from what I want to do then raising some questions I have. I want to develop a general Java program which is a superset of a number of programs (let's call them program variants). In particular, the general program has methods which…
tuan
  • 433
  • 8
  • 17
5
votes
2 answers

Get type of a std::variant member at a given index during compile time

I have defined the following variant: std::variant myVar; The type of the member at index 2 is MyType. How to get this during compile time? (so that I can use it in a constexpr context etc..)
Anubis
  • 6,995
  • 14
  • 56
  • 87
5
votes
2 answers

"No matching function for call" error when creating a vector of function variants

I'm trying to create a std::vector that can hold std::function objects of different signatures using std::variant. Why does the following code not compile: #include #include #include int main() { …
Alex
  • 3,316
  • 4
  • 26
  • 52
5
votes
2 answers

Remove_if with vector containing variants

I have two different objects: struct TypeA { std::size_t no; std::string data; std::string data2; }; struct TypeB { std::size_t no; std::string data; std::string data2; std::string data3; }; They are stored in a…
Sandro4912
  • 313
  • 1
  • 9
  • 29
5
votes
1 answer

VBA Handling multiple custom datatype possibilities

I have done some research and haven't found any similar question. I have a VBA macro that imports a .CSV file containing telegrams sent by a device. In the end of this macro, I want to create a graph with the time elapsed on the x-axis and the value…
mickael
  • 73
  • 5
5
votes
1 answer

Make VarToDoubleAsString use Delphi settings (not OS settings)

When one assigns a Variant containing a string value to a floating point variable Delphi calls VarToDoubleAsString to do the conversion, which in turn uses the OS settings for decimal and thousand separator (via VarR8FromStr). This is problematic if…
Uli Gerhardt
  • 13,748
  • 1
  • 45
  • 83