Enum type vs. enum like parameter name

I have the following behavior (contrived example for demo purposes)

@enum Fruit apple orange kiwi
f(apple::String, fruit::Array{Fruit}) = fruit[1] == apple::Fruit
f("not apple", [apple])

ERROR: TypeError: in typeassert, expected Fruit, got a value of type String

On the other hand this works as expected

f(param1::String, fruit::Array{Fruit}) = fruit[1] == apple::Fruit
f("not apple", [apple])

true

It looks like the name of a parmeter (apple) clashes with the name of an enum (apple) and wins. Is there a way to specify within the function body that “apple” refers to the enum type?

In the REPL case it could be:

f(apple::String, fruit::Array{Fruit}) = fruit[1] == Main.apple

You are right - how would a non REPL solution look like?

Depends on your setup. If the enum is defined in a module X, just use X.apple.

2 Likes

Right so if the enum is defined in module X then it is X.enumname otherwise (REPL or not) it is Main.enumname

Also, turns out that expression apple::Fruit is a type assertion - a runtime check on the actual type, not a type specification.

Yes, but there may be more possibilities I can’t imagine right now.