Quantcast
Channel: First steps - JuliaLang
Viewing all articles
Browse latest Browse all 2795

Why can't methods be defined inside a `let` block?

$
0
0

When one runs the following code, my expectation is that three methods would get defined.

# Default equal function:
preferred_equal_fn(x) = (===)

# Numbers use isequal
let x = isequal
    preferred_equal_fn(::Type{T}) where {T <: Number} = x
end

# So do strings.
begin
    local x = isequal
    preferred_equal_fn(::Type{T}) where {T <: AbstractString} = x
end

However, the let block seems to prevent the definition:

methods(preferred_equal_fn)
# [Out] :=  #2 methods for generic function preferred_equal_fn:
#                preferred_equal_fn(::Type{T}) where T<:AbstractString in Main at In[1]:12: 
#                preferred_equal_fn(x) in Main at In[1]:2

What’s going on here? Is this documented anywhere?

6 posts - 5 participants

Read full topic


Viewing all articles
Browse latest Browse all 2795

Trending Articles