W3cubDocs

/Crystal

struct Union(*T)

Overview

A union type represents the possibility of a variable or an expression having more than one possible type at compile time.

When invoking a method on a union type, the language checks that the method exists and can be resolved (typed) for each type in the union. For this reason, adding instance methods to Union makes no sense and has no effect. However, adding class method to Union is possible and can be useful. One example is parsing JSON into one of many possible types.

Union is special in that it is a generic type but instantiating it might not return a union type:

Union(Int32 | String)      # => (Int32 | String)
Union(Int32)               # => Int32
Union(Int32, Int32, Int32) # => Int32

Defined in:

union.cr

Constructors

Class Method Summary

  • .nilable?

    Returns true if this union includes the Nil type.

Constructor Detail

def self.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node)Source

def self.new(pull : JSON::PullParser)Source

def self.newSource

Class Method Detail

def self.nilable?Source

Returns true if this union includes the Nil type.

(Int32 | String).nilable? # => false
(Int32 | Nil).nilable?    # => true

© 2012–2020 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/0.35.1/Union.html