# Multiple Dispatch: Field Access Problem

**URL:** <https://forum.crystal-lang.org/t/multiple-dispatch-field-access-problem/1926>\
**Category:** Help & Support\
**Created:** [April 10, 2020, 12:04pm UTC](https://forum.crystal-lang.org/t/multiple-dispatch-field-access-problem/1926 "2020-04-10T12:04:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![appcypher](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/appcypher/32/788_2.png) [@appcypher](https://forum.crystal-lang.org/u/appcypher)\
**Post date:** [April 10, 2020, 12:04pm UTC](https://forum.crystal-lang.org/t/multiple-dispatch-field-access-problem/1926/1 "2020-04-10T12:04:14Z")

</div>

I’m sure questions like this have been asked before, but I couldn’t find them. I was testing out Crystal’s multiple dispatch (comparing it to Julia’s) with the following piece of code, but I’m getting `undefined method 'name' for Dog` for the `encounter` function.

Is Crystal unable to do multiple dispatch when fields are involved?

```crystal
abstract class Pet; end

class Dog < Pet
    def initialize(@name : String); end 
end 

class Cat < Pet
    def initialize(@name : String); end 
end 

def meets(a : Dog, b : Dog) 
    return "licks"
end 

def meets(a : Dog, b : Cat) 
    return "chases"
end 

def meets(a : Cat, b : Dog) 
    return "hisses"
end 

def meets(a : Cat, b : Cat) 
    return "slinks"
end 

def encounter(a : Pet, b : Pet)
    p "#{a.name} meets #{b.name} and #{meets(a, b)}"
end

fido = Dog.new "Fido"
bingo = Dog.new "Bingo"
sam = Cat.new "Sam"

encounter(fido, bingo)
encounter(fido, sam)

```

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [April 10, 2020, 12:08pm UTC](https://forum.crystal-lang.org/t/multiple-dispatch-field-access-problem/1926/2 "2020-04-10T12:08:29Z")

</div>

You just haven’d defined a `name` method. The initializer only assigns a value to the instance variable `@name`, but that does not automatically generate a method by that name (you can access the ivar as `dog.@name` though).  
Use `getter name` for this, which expands to `def name; @name; end` and exposes the ivar as a method.

---

<div class="post-metadata">

**Author:** ![appcypher](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/appcypher/32/788_2.png) [@appcypher](https://forum.crystal-lang.org/u/appcypher)\
**Post date:** [April 10, 2020, 1:02pm UTC](https://forum.crystal-lang.org/t/multiple-dispatch-field-access-problem/1926/3 "2020-04-10T13:02:09Z")

</div>

Just noticed that. Thanks. I’m still learning the syntax.

---

<div class="post-metadata">

**Author:** ![IEatReturnValues](https://avatars.discourse-cdn.com/v4/letter/i/a587f6/32.png) [@IEatReturnValues](https://forum.crystal-lang.org/u/IEatReturnValues)\
**Post date:** [April 11, 2020, 3:24pm UTC](https://forum.crystal-lang.org/t/multiple-dispatch-field-access-problem/1926/4 "2020-04-11T15:24:46Z")

</div>

This is actually a great example of how `abstract` works in Crystal, thanks.
