Partial parametric methods?

Not sure if the title of the topic accurately reflects what I’m looking for, but this is what I have in mind:

  • I have a parametric type, HashMap{T,R} (it’s basically a custom Dict{T,R} type).

  • I want to define methods such as delete!:

delete!(hm::HashMap, key)
  • for delete! I do not have the value (and its type), but I do have the key - and I would like to constrain the type of key to T, producing something like
delete!(hm::HashMap{T,_}, key::T) where {T}

Is this possible? Or is there any other way to do it? Is it at all useful or is it best to stick to non-parametric methods instead?

Does this work for your use case:

julia> foo(x::HashMap{T, X}, key::T) where {T, X} = ...

?

2 Likes

If you want to use the _ notation just do

delete!(hm::HashMap{T,_}, key::T) where {T, _}

@rdeits Ah, OK, I understand - I was looking at the problem all wrong. The method is parametrized by both T and X because of the HashMap instance, and that’s where it gets the types from. Sure, makes total sense, thank you.

@Mason Thanks - it’s not so much about the name of the type parameters. I was just under the impression that the second type would be missing when invoking the method, not realising that it’s in fact provided by the HashMap instance.

1 Like

you don’t need to specify the second one at all, delete!(hm::HashMap{T}, key::T) where T is equivalent to delete!(hm::HashMap{T,X} where X, key::T) where T.

2 Likes

Oh, lovely, this is actually what my query was about! I think such a definition much better captures the intent of the author (X is irrelevant in this context).