[Beginner] How do I see the methods on a type I'm extending?


struct Thing
  name::AbstractString)
end

struct ThingHolder
  holding::Thing
end

function sayer(t)
  println(x.name)
end

function sayer(t_h)
  print(t_h.holding.name)
end

my_holder = ThingHolder(Thing("hayden"))
sayer(my_holder)
methods(my_holder)

I’m trying out 1.5 and this says that my_holder has 0 methods. I’m not sure if I understand what methods() is doing, I tried the ?julia menu, and I’ve also tried methods(my_holder, ThingHolder) and that didn’t help either.

I am curious if Julia is able to distinguish at runtime what methods an object could be called with in this manner.

Thanks!

You have a typo in Thing’s name field, as it has name::AbstractString) instead of name::AbstractString. Also, your second sayer definition will overwrite the first, because you’re missing the type to make it distinct.

It should be like this:

function sayer(t::Thing)
  println(t.name)
end

function sayer(t_h::ThingHolder)
  print(t_h.holding.name)
end

You can also remove print and println here, because the result will be displayed nevertheless.

There is no such list of all methods that accept a specific type, for a very simple reason: there are too many of them (i.e. many methods are type generic).

A few things can help you:

  1. check methods to construct a specific type
julia> methods(ThingHolder)
# 2 methods for type constructor:
[1] ThingHolder(holding::Thing) in Main at REPL[14]:2
[2] ThingHolder(holding) in Main at REPL[14]:2
  1. check methods of a function which uses multiple dispatch
julia> methods(sayer)
# 2 methods for generic function "sayer":
[1] sayer(t_h::ThingHolder) in Main at REPL[23]:1
[2] sayer(t::Thing) in Main at REPL[22]:1
  1. check which method gets called for specific argument types
julia> which(sayer, [ThingHolder])
sayer(t_h::ThingHolder) in Main at REPL[23]:1

julia> which(sayer, [Thing])
sayer(t::Thing) in Main at REPL[22]:1 
2 Likes

Are you perhaps looking for methodswith?

4 Likes

Oh!

Okay, this was very helpful. It makes sense that it can tell me the types if I annotate them properly, I guess I was not really thinking it through that it wouldn’t tell me from inference alone, and I hadn’t realized I was overwriting the definition with an untyped argument, but that makes sense too. I hadn’t considered the point you made about there being too many members in those method tables, but again, it makes perfect sense now that I think about it. Like the + operator probably has dozens if not hundreds of type-dispatched definitions for all sorts of different types, of course.

I didn’t see that typo, it’s not in my code, I was messing around with formatting and it got in there. Thanks though. :slight_smile:

Thanks for such an in-depth reply to such a silly mistake on my part. I read some articles about designing programs with type-dispatch in mind, and I’ve been just practicing and trying to teach my brain to think that way, it’s difficult coming from so much time spent writing classes. For me, anyways.

1 Like