instances
instances(T::Type)
Return a collection of all instances of the given type, if applicable. Mostly used for enumerated types (see @enum).
Examples
-
Get instances of an enumerated type:
julia> @enum Fruit apple banana orange julia> instances(Fruit) 3-element Array{Fruit,1}: apple banana orangeThis example demonstrates how to retrieve all instances of the enumerated type
Fruit. -
Get instances of a custom type:
julia> struct Person name::String age::Int end julia> instances(Person) 0-element Array{Person,1}Here, the
instancesfunction is used to get all instances of the custom typePerson. Since no instances have been created yet, the returned array is empty. - Handle unsupported types:
julia> instances(Int) ERROR: TypeError: in instances, expected Type, got Type{Int64}The
instancesfunction 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 typeInt.
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.