# Anyone implementing the interpreter using a different pattern than Visitor?

**URL:** <https://forum.codecrafters.io/t/anyone-implementing-the-interpreter-using-a-different-pattern-than-visitor/2112>\
**Category:** Challenges\
**Tags:** challenge:interpreter\
**Created:** [October 1, 2024, 8:34pm UTC](https://forum.codecrafters.io/t/anyone-implementing-the-interpreter-using-a-different-pattern-than-visitor/2112 "2024-10-01T20:34:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gabrielvincent](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/gabrielvincent/32/1085_2.png) [@gabrielvincent](https://forum.codecrafters.io/u/gabrielvincent)\
**Post date:** [October 1, 2024, 8:34pm UTC](https://forum.codecrafters.io/t/anyone-implementing-the-interpreter-using-a-different-pattern-than-visitor/2112/1 "2024-10-01T20:34:10Z")

</div>

In the book, the author chooses the Visitor Pattern for their implementation, which makes a lot of sense for an object-oriented language like Java. I know it’s possible to use this pattern in Go as well, but because we don’t have classes, I find it a little unergonomic to use it. I decided to just create recursive functions that do type switches.

It’s been interesting to do this challenge in Go because I have to kind of “translate” the deeply object-oriented approach to the book’s implementation into more idiomatic Go code.

---

<div class="post-metadata">

**Author:** ![ThomasBollmeier](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/thomasbollmeier/32/2060_2.png) [@ThomasBollmeier](https://forum.codecrafters.io/u/ThomasBollmeier)\
**Post date:** [October 1, 2024, 8:43pm UTC](https://forum.codecrafters.io/t/anyone-implementing-the-interpreter-using-a-different-pattern-than-visitor/2112/2 "2024-10-01T20:43:44Z")

</div>

I implement the interpreter to learn Go along the way. Even though Go is not an OO-language in the strict sense I found the availability of interfaces, structs and methods sufficient to use the visitor pattern for my interpreter. But - as I said - I am a Go newbie. so this might not be an idiomatic approach in Go.

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [October 2, 2024, 3:02am UTC](https://forum.codecrafters.io/t/anyone-implementing-the-interpreter-using-a-different-pattern-than-visitor/2112/3 "2024-10-02T03:02:35Z")

</div>

I found the Visitor Pattern cumbersome as well, especially in languages that don’t have to use class for everything.

It’s arguably cleaner to use **pattern matching** with **destructuring** , for example:

 ![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/4/4422b0ca88e4bb3b6920ac877568fd1d0ed10fcc.png)

---

<div class="post-metadata">

**Author:** ![gabrielvincent](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/gabrielvincent/32/1085_2.png) [@gabrielvincent](https://forum.codecrafters.io/u/gabrielvincent)\
**Post date:** [October 2, 2024, 8:56am UTC](https://forum.codecrafters.io/t/anyone-implementing-the-interpreter-using-a-different-pattern-than-visitor/2112/4 "2024-10-02T08:56:52Z")

</div>

Is this Python? I didn’t know it had a `match` statement! This is what I did for Go:

 ![carbon](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/f/f1fa18c697eaf93fdf72126a064eb2c96da3747f.jpeg)

Each one of these functions end up recursively calling `Evaluate(expr)` until the expression passed is a Literal, in which case its value is returned. Simple and clear recursion.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/7/0/700657133935c15703e22c7c8870394f6e6dc27d.svg) [@system](https://forum.codecrafters.io/u/system)\
**Post date:** [October 9, 2024, 7:55pm UTC](https://forum.codecrafters.io/t/anyone-implementing-the-interpreter-using-a-different-pattern-than-visitor/2112/5 "2024-10-09T19:55:38Z")

</div>

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.
