instances

instances(T::Type)

Return a collection of all instances of the given type, if applicable. Mostly used for enumerated types (see @enum).

Examples

  1. Get instances of an enumerated type:

    julia> @enum Fruit apple banana orange
    julia> instances(Fruit)
    3-element Array{Fruit,1}:
    apple
    banana
    orange

    This example demonstrates how to retrieve all instances of the enumerated type Fruit.

  2. Get instances of a custom type:

    julia> struct Person
              name::String
              age::Int
           end
    julia> instances(Person)
    0-element Array{Person,1}

    Here, the instances function is used to get all instances of the custom type Person. Since no instances have been created yet, the returned array is empty.

  3. Handle unsupported types:
    julia> instances(Int)
    ERROR: TypeError: in instances, expected Type, got Type{Int64}

    The instances function is typically used for enumerated types and may not work with other types. In this example, it shows an error when attempting to get instances of the built-in type Int.

Common mistake example:

julia> instances("string")
ERROR: MethodError: no method matching instances(::String)

In this case, the instances function is being called with a non-type argument. The function expects a type as an argument, not a value. Make sure to provide a valid type to the instances function.

See Also

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field
Details

Checking you are not a robot: