Starting March 27, 2025, we recommend using android-latest-release
instead of aosp-main
to build and contribute to AOSP. For more information, see Changes to AOSP.
Safe union
Stay organized with collections
Save and categorize content based on your preferences.
safe_union
in HIDL represents an explicitly tagged union type.
This is similar to a union
except safe_union
keeps
track of the underlying type and is compatible with Java. The
safe_union
type is available in Android 10
and higher for new and upgraded devices.
Syntax
A safe_union
is expressed in HIDL exactly like a
union
or struct
.
safe_union MySafeUnion {
TypeA a;
TypeB b;
...
};
Usage
At runtime, a safe_union
is only ever one type. By default, it's
the first type in the union. For instance, above,
MySafeUnion
is by default TypeA
.
hidl-gen
generates a custom class or struct for a
safe_union
in both C++ and Java. This class includes a
discriminator for each member (in hidl_discriminator
), a method to
get the current discriminator (getDiscriminator
), and setters and
getters for each member. Each setter and getter is named exactly as its member.
For instance, the getter for TypeA a
is called "a", and it
returns something of TypeA
. The corresponding setter is also
be called "a" and takes a parameter of TypeA
. Setting the value in
a safe_union
updates the value of the discriminator as
returned by getDiscriminator
. Accessing a value from a
discriminator that isn't the current discriminator aborts the program. For
instance, if calling getDiscriminator
on an instance of
MySafeUnion
returns hidl_discriminator::b
, then
trying to retrieve a
aborts the program.
Monostate
A safe_union
always has a value, but if it is desired to not
have a value, use android.hidl.safe_union@1.0::Monostate
as a
placeholder. For instance, the following union can either be
noinit
(empty) or foo
:
import android.hidl.safe_union@1.0::Monostate;
safe_union OptionalFoo {
Monostate noinit;
Foo foo;
};
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-06-12 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-06-12 UTC."],[],[],null,["# Safe union\n\n`safe_union` in HIDL represents an explicitly tagged union type.\nThis is similar to a `union` except `safe_union` keeps\ntrack of the underlying type and is compatible with Java. The\n`safe_union` type is available in Android 10\nand higher for new and upgraded devices.\n\nSyntax\n------\n\nA `safe_union` is expressed in HIDL exactly like a\n`union` or `struct`. \n\n```scdoc\nsafe_union MySafeUnion {\n TypeA a;\n TypeB b;\n ...\n};\n```\n\nUsage\n-----\n\nAt runtime, a `safe_union` is only ever one type. By default, it's\nthe first type in the union. For instance, above,\n`MySafeUnion` is by default `TypeA`.\n\n`hidl-gen` generates a custom class or struct for a\n`safe_union` in both C++ and Java. This class includes a\ndiscriminator for each member (in `hidl_discriminator`), a method to\nget the current discriminator (`getDiscriminator`), and setters and\ngetters for each member. Each setter and getter is named exactly as its member.\nFor instance, the getter for `TypeA a` is called \"a\", and it\nreturns something of `TypeA`. The corresponding setter is also\nbe called \"a\" and takes a parameter of `TypeA`. Setting the value in\na `safe_union` updates the value of the discriminator as\nreturned by `getDiscriminator`. Accessing a value from a\ndiscriminator that isn't the current discriminator aborts the program. For\ninstance, if calling `getDiscriminator` on an instance of\n`MySafeUnion` returns `hidl_discriminator::b`, then\ntrying to retrieve `a` aborts the program.\n\nMonostate\n---------\n\nA `safe_union` always has a value, but if it is desired to not\nhave a value, use `android.hidl.safe_union@1.0::Monostate` as a\nplaceholder. For instance, the following union can either be\n`noinit` (empty) or `foo`: \n\n```python\nimport android.hidl.safe_union@1.0::Monostate;\n\nsafe_union OptionalFoo {\n Monostate noinit;\n Foo foo;\n};\n```"]]