TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Design Patterns Are Temporary, Language Features Are Forever

110 点作者 delifue8 个月前

19 条评论

dgreensp8 个月前
You don’t come across visitors every day, and they aren’t something to reach for instead of a simple switch, but if you are writing a Webpack or ESLint plugin or something like that, you might encounter them for traversing an AST. It’s not just an OO thing, either.<p>I learned the visitor pattern in pretty ideal circumstances: from a classmate, back in college, when we were writing a compiler together for a class project, and it was just what we needed.<p>The right conditions for a visitor are when you have a data structure with many different “node” types, and the traversal logic is not simple. In addition, you will be doing many different traversals. You might also be doing transformations on the structure. You might have a library running the traversals&#x2F;transformations provided by the code that uses the library. There could be correctness or memory management considerations involved that you don’t want the client to have to worry about. You might want to “pipeline” multiple transformations, doing several manipulations in one traversal, in an efficient way. Common traversals might care about only certain types of nodes, like a particular lint rule might just be looking at string literals or references to global variables, but it needs to walk the entire syntax tree of a file of source code.
评论 #41490060 未加载
评论 #41492921 未加载
评论 #41490751 未加载
michielderhaeg8 个月前
This reminds me of when I was still in university. During our compilers course we used the &quot;Modern Compiler Implementation in Java&quot; book. Because I was really into FP at the time, I instead used the &quot;Modern Compiler Implementation in ML&quot; version of this book (which was the original, the Java and C versions were made to make it more accessible). We noticed during the course that my version was missing a whole chapter on visitors, likely because ML&#x27;s pattern matching made this kind off trivial.<p>One of the other students made a similar remark, that software design patterns are sometimes failures of the language when they have no concise way of expressing these kinds of concepts.
评论 #41493045 未加载
评论 #41492281 未加载
评论 #41492328 未加载
评论 #41498750 未加载
smallnamespace8 个月前
I disagree with the main thrust of the article, that the Visitor pattern is primarily a primitive way to do pattern matching.<p>For one, the Visitor pattern (as written in the article) fails to fulfill a core feature of Rust pattern matching, which is having a compact language to describe when something matches based on values, as opposed to the concrete type.<p>For another, you could equally well say that if-else blocks or ternary statements are also primitive pattern matching, if you&#x27;re willing to stretch things that far.<p>In my view, the core reason people reached for the Visitor pattern in the past is that old Java didn&#x27;t have convenient lambdas (anonymous functions). Visitors let you create an interface that mimics functional map.<p>Newer versions of Java have made lambdas much more convenient, so there&#x27;s less motivation to reach for the Visitor pattern. You also saw this development in C#, where delegates were a language feature that often obviated rolling your own Visitors.
评论 #41528774 未加载
评论 #41491045 未加载
0823498723498728 个月前
I haven&#x27;t looked at patterns since the GOF days, so maybe they grew closer to Alexander&#x27;s as they matured, but the original ones were very much ways to speak about things so they&#x27;d be politically palatable in an everything-is-an-object world.<p>You just wanted some data? &quot;Flyweight&quot;. You just wanted some functions? &quot;Strategy&quot;. etc.
评论 #41489738 未加载
评论 #41488270 未加载
miningape8 个月前
I think the visitor mimics pattern matching, but it really behaves&#x2F;feels different. I think the visitor is really closer to a bridge from OOP land into FP land.<p>In FP its difficult&#x2F;impossible to add new types but simple to add new mappings&#x2F;pipelines for those types.<p>In OOP its easy to add new types but difficult&#x2F;impossible to add new mappings&#x2F;pipelines for those types (try transforming a POJO without lombok @Builder and @Value).<p>The visitor pattern (an OOP concept) allows us to <i>more</i> easily add a new mapping&#x2F;pipeline for a set of related types. Normally in OOP this would involve adding a new method to a base class, implementing these new &quot;mappings&quot; in each subclass. With the visitor instead you just implement the new visitor, this allows the logic for this particular &quot;kind&quot; of mapping (visitor) to be grouped together, rather than represented by a method on a base class.
评论 #41490819 未加载
评论 #41529842 未加载
vrnvu8 个月前
I always tell people who obsess over design patterns as clean code and good coding practices to remember that design patterns often indicate a lack of language features.<p>For example, are you using a Builder? Would you use the Builder pattern if the language had named variables in arguments?<p>My favorite reference on the topic is Peter Norvig&#x27;s &quot;Design Patterns in Dynamic Languages&quot; (1996!) <a href="https:&#x2F;&#x2F;www.norvig.com&#x2F;design-patterns&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.norvig.com&#x2F;design-patterns&#x2F;</a>
评论 #41493471 未加载
评论 #41492407 未加载
评论 #41495535 未加载
segmondy8 个月前
Whenever design patterns come up, folks start talking about language features. First of all, it&#x27;s about natural language to describe a way of doing something. If you don&#x27;t have a language to describe a design, then folks could and often would implement incompatible pieces. By having a common language, you solve for that. If I gave you two piece of wood and asked you to join them together. How would you? Does it matter how you join them? It absolute does matter. You might use nail, you might use screw, you might do glue. In carpentry there are design pattern for joints. You can tell someone to use a butt joint, a box joint, a pocket-hole joint, etc. Without these higher level language of design. You will have to explain and you might find out that explanation is not enough, you would need diagrams or models. The same with programming design patterns, without it, you will waste time explaining or drawing diagram. For the professional, these are the colloquial phrases of programming. If you&#x27;re an enterprise application developer, or a game programmer or a cloud dev, your languages and often used patterns would vary. I say this as someone that learned lisp as a 2nd language a long time and a fan of functional languages. DPs are worth knowing and understanding.
svieira8 个月前
Anyone who is interested in pattern matching and the visitor pattern (and the benefits of various encodings of state and behavior) who hasn&#x27;t seen them should check out:<p>* Li Haoyi&#x27;s <i>Zero-Overhead Tree Processing with the Visitor Pattern</i> (<a href="https:&#x2F;&#x2F;www.lihaoyi.com&#x2F;post&#x2F;ZeroOverheadTreeProcessingwiththeVisitorPattern.html" rel="nofollow">https:&#x2F;&#x2F;www.lihaoyi.com&#x2F;post&#x2F;ZeroOverheadTreeProcessingwitht...</a>)<p>* Noel Welsh&#x27;s <i>Uniting Church and State: FP and OO Together</i> (<a href="https:&#x2F;&#x2F;noelwelsh.com&#x2F;posts&#x2F;uniting-church-and-state&#x2F;" rel="nofollow">https:&#x2F;&#x2F;noelwelsh.com&#x2F;posts&#x2F;uniting-church-and-state&#x2F;</a> or <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=IO5MD62dQbI" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=IO5MD62dQbI</a> is you like video)<p>* Bruno C. d. S. Oliveira and William R. Cook&#x27;s <i>Extensibility for the Masses Practical Extensibility with Object Algebras</i> (<a href="https:&#x2F;&#x2F;www.cs.utexas.edu&#x2F;~wcook&#x2F;Drafts&#x2F;2012&#x2F;ecoop2012.pdf" rel="nofollow">https:&#x2F;&#x2F;www.cs.utexas.edu&#x2F;~wcook&#x2F;Drafts&#x2F;2012&#x2F;ecoop2012.pdf</a>)
syncsynchalt8 个月前
The author took the thesis in one direction, but there&#x27;s another interesting way to think of it.<p>C++ is an epitaph for every discarded concept in OO design. Diamond inheritance, generics to a fault, operator overloading to a fault, the richness of STL types. Those &quot;patterns&quot; are dead, but the language features must continue to be supported.
kazinator8 个月前
Language features are not forever.<p>Language features can be deprecated as obsolescent and after a period of obsolescence, removed.<p>&quot;implicit int&quot; declarations are no longer a C language feature. The (void) parameter list is now obsolescent; as of C23, () means the same thing as (void), like in C++.<p>Design patterns can end up in libraries. But library and language features are the same. A popular library which everyone uses cannot be thrown out overnight any more than a language feature.
评论 #41492108 未加载
bad_user8 个月前
&gt; <i>Now knowing about pattern matching, this is how I came to realise that the visitor pattern was pattern matching in an OO way.</i><p>The programming language needs to be very expressive to replace the visitor pattern with pattern matching. For example, you need GADTs. The cool thing about static languages with OOP is that OOP can hide a lot of type-level complexity. Also, in languages with runtimes optimized for virtual calls (e.g., the JVM, V8), pattern matching can have less performance than the visitor pattern, despite pattern matching now being a reality on the JVM at least (e.g., Java, Scala have pattern matching).<p>The difference between pattern matching and the visitor pattern is the difference between tagless-initial and tagless-final encodings, or data versus Church-encodings. And as a software engineer, it&#x27;s good to be aware of their strengths and weaknesses.<p>People making this claim (that design patterns are just missing language features) always mention Visitor, but stop short of mentioning other design patterns, such as Observable&#x2F;Listener, but also, the even less obvious: Monad or the Free Monad (i.e., still a design pattern, despite being able to comfortably express it in your language).
Robin_Message8 个月前
The visitor pattern is a way of getting multiple dispatch in a language with single dispatch.<p>It can also be seen as a way of encoding a functional solution to the expression problem in an OO language, which is useful when you have a set of objects and want to make it easy to add new operations to them, not create new types of objects.
gorkempacaci8 个月前
If I may nitpick, the call to children’s accept methods should be in the visitor, not the parent. Imagine you’re writing an XMLGeneratorVisiter. The visit method for the parent would print &lt;parent&gt;, call child accepts, and then print &lt;&#x2F;parent&gt;. If you do it the way it is done here you lose the control on when the children are visited.<p>Also, the point of the visitor pattern is not just pattern matching&#x2F;polymorphism. Of course you could do it with polymorphism or conditionals or whatever. But the visitor pattern reduces coupling and increases cohesion. So you get a more maintainable, testable code base. Pattern matching with a candied switch doesn’t give you that.
mhh__8 个月前
I&#x27;ve seen it said that Alexander working on this stuff just at the moment of peak OOP-timism is a great shame&#x2F;tragedy. I&#x27;m inclined to agree.<p>Functional purists like to glibly say that their patterns are discovered rather than invented. I used to Pooh Pooh this a bit. Then I had to write a class just to have a function to call. They&#x27;re right.
评论 #41491750 未加载
Mikhail_Edoshin8 个月前
Design patterns are the real language though. A feature of a real language is that you can present a new concept. For example, a promise in an async programming is such a concept and it is not tied to a particular notation. Once you get that concept working in a notation that does not have it, then it can be fused into a notation, but then it becomes sort of frozen and ceases to evolve.
digging8 个月前
I got about halfway before completely losing the thread as I had no idea what was being said, unfortunately.<p>Is &quot;visitor&quot; pattern ever defined in this article?<p>Is there anything in this article explaining the assertion &quot;Design Patterns Are Temporary, Language Features Are Forever&quot;? I couldn&#x27;t penetrate the specific example being discussed.
评论 #41492135 未加载
weinzierl8 个月前
Unfortunately, I can&#x27;t remember where I heard that bon mot: <i>&quot;Every design pattern is a bug report against your compiler&quot;</i>
andybak8 个月前
&gt; because that&#x27;s a major L<p>What&#x27;s &quot;L&quot;? Am I getting old?
评论 #41491212 未加载
评论 #41491509 未加载
estebarb8 个月前
I have a bug&#x2F;hate relationship with the visitor pattern: how do I implement it without causing a stack overflow in languages without tail recursion optimization? It always ends up in a loop with a lot of ifs inside.
评论 #41489672 未加载
评论 #41501727 未加载
评论 #41489885 未加载