News for hackers. Stuff that matters.
Re:Crafting Interpreters by jokoon on Tuesday July 10, 2018 @05:38AM (#17496569)
I've started reading it, until it used the visitor pattern for its parser. It sort of generates Java code with some hacky way of doing things. It felt complex for no good reason.

I have nothing against patterns, but it made it harder to follow, so I stopped reading it.

Re:Crafting Interpreters by _old_dude_ on Tuesday July 10, 2018 @06:07AM (#17496750)
There is no pattern matching in Java (yet).

If you do the whole process in one pass like in C you can use the parser evaluation but even a simple feature like lambdas requires more than one pass.

So either you use a Visitor or you develop pattern matching in your language and bootstrap it rapidly to be able to use you language to write the backend of your language.

Re:Crafting Interpreters by antpls on Tuesday July 10, 2018 @06:07AM (#17496751)
Did you have a previous experience of something easier? May you share about it?
Re:Crafting Interpreters by criddell on Tuesday July 10, 2018 @08:49AM (#17497552)
What is the actual problem? Is it multiple dispatch? I think some languages (Lisp may be one) handle it natively.

Visitor is the only way I know of dealing with it in C++. Once you do it enough, Visitor starts to feel like a pretty natural way to extend a class without modifying the class.

Re:Crafting Interpreters by jcelerier on Tuesday July 10, 2018 @10:05AM (#17498176)
> Visitor is the only way I know of dealing with it in C++. Once you do it enough, Visitor starts to feel like a pretty natural way to extend a class without modifying the class.

Are you talking about traditonal OOP visitor pattern ? It's pretty terrible, when compared to std::variant ; imagine for instance that you want to have a way to draw the rect that contains a given shape without changing your shape class: https://godbolt.org/g/34KyMw

Re:Crafting Interpreters by criddell on Tuesday July 10, 2018 @10:19AM (#17498303)
If your interface is that consistent, why not just make the bounding box drawing function a template?

Visitor is nice when some kind of transformation needs to be applied to the visited objects.

Re:Crafting Interpreters by chubot on Tuesday July 10, 2018 @08:24AM (#17497383)
Not the OP, but I took a survey of the /r/programminglanguages reddit, where basically everybody has to deal with the "visitor issue".

https://www.reddit.com/r/ProgrammingLanguages/comments/7w3oe...

The top comment supported the use of the visitor pattern, but I would say that MOST people in the thread did NOT use visitors.

You only really need a visitor if you have more than one traversal. And even then you don't necessarily need it.

Re:Crafting Interpreters by magicalhippo on Tuesday July 10, 2018 @06:36AM (#17496869)
I'm curious about this as well. When writing my own interpreter it didn't take me long before I opted to use the visitor pattern, and once I did it felt like a natural fit.
Re:Crafting Interpreters by isaachier on Tuesday July 10, 2018 @09:20AM (#17497788)
If using C++ I'd usually just opt for a type enum and switch on it. Visitor is a better way of using type to dispatch, but harder to see control flow.