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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Duck typing is safe (2020)

58 点作者 benhoyt将近 3 年前

15 条评论

ezrast将近 3 年前
&gt; If you &quot;accidentally&quot; implement Write([]byte) (int, error), odds are it&#x27;ll still be doing something at least semi-sensible if it is accidentally used as an io.Writer.<p>Funny, not too long ago I encountered a logging library with a writer that expected each write call to pass in a fully-formed log message, which happened to be in a JSON format. That meant that if you passed the writer to anything that expected a properly byte-oriented stream - like, say, a JSON serialization library - the writing would typically be done in chunks, and each chunk would be sent in isolation to a remote server that was expecting fully-formed JSON, and that server would silently drop the malformed data. It was weeks before I figured out what was going wrong.<p>This would be a great refutation of the article if it had happened in Go, but no, this was Rust, and the library authors had explicitly marked their type as `impl std::io::Write` without understanding why that wasn&#x27;t appropriate.<p>I guess the moral is that semi-sensible isn&#x27;t good enough: the real danger isn&#x27;t that you end up shooting a physical gun; it&#x27;s that you shoot your video game gun in a subtly wrong way that takes ages to track down. Failing to compile is loads better.
评论 #31750299 未加载
评论 #31751509 未加载
评论 #31750420 未加载
swatcoder将近 3 年前
&gt; In my 23 years of programming, I have never experienced this bug. I&#x27;ve also never heard of anyone else experiencing this. I&#x27;ve even said this in fora where I should have had someone pop up to correct me<p>This is a joke I’m not getting, right?<p>Somebody is forgetting that comparison and arithmetic operators are an interface on a type, and that almost every popular language (even many that are ostensibly “statically typed”) have ubiquitous nightmare scenarios surrounding implicit casts.<p>And of course, it’s also common where data comes from an i&#x2F;o source and then is magically assigned a type by an ORM or deserializer or framework helper.<p>Or when working with large projects with lots of modularity and abstract interfaces, where the names of parameters, methods, and members become increasingly generic.<p>People work around these issues and learn how to avoid them with good practice, but they happen every day.
评论 #31750231 未加载
评论 #31750800 未加载
评论 #31764175 未加载
评论 #31749835 未加载
评论 #31749572 未加载
notepalf将近 3 年前
The major argument is that &quot;if it&#x27;s wrong, nothing bad will happen, because it will break in runtime&quot;. Isn&#x27;t it better to break before things are in production, like at compile time?
评论 #31749121 未加载
评论 #31749289 未加载
hn_throwaway_99将近 3 年前
As someone who did the first ~2&#x2F;3 of my career in Java, but have been in Node&#x2F;Typescript for the past several years, can&#x27;t say enough how much I think the structural typing of Typescript allows be to have much higher productivity than the nominal typing of Java.<p>Primarily it has to do with making refactorings much, much easier.
评论 #31749238 未加载
评论 #31749561 未加载
patrick451将近 3 年前
&gt; In my 23 years of programming, I have never experienced this bug. I&#x27;ve also never heard of anyone else experiencing this. I&#x27;ve even said this in fora where I should have had someone pop up to correct me; a corollary to Cunningham&#x27;s Law is that if you say something that should be controversial but nobody pops up to correct you, it must not be controversial after all.<p>This is such an odd claim. Mixing up a string for a list of strings, which both satisfy the iterable interface, happens all the time in python. Does the author, and his acclaimed fora simply not use python?
评论 #31751356 未加载
评论 #31749955 未加载
anonymoushn将近 3 年前
&gt; In my 23 years of programming, I have never experienced this bug. I&#x27;ve also never heard of anyone else experiencing this. I&#x27;ve even said this in fora where I should have had someone pop up to correct me<p>I cannot today find the bug, but as I recall an early version of Golang&#x27;s stdlib included some HTTP handler that would &quot;interface upgrade&quot; your Reader to a ReadCloser and close it.<p>This could be bad if you expected functions that accept Reader to not call close!<p>We experienced this bug at Twitch and found it quite troublesome.
mamcx将近 3 年前
Duck typing and structural are not the same thing (for safety), IMHO.<p>I think Structural is safer and better overall, and the best exponent is the relational&#x2F;sql model.<p>What make it good is that structural types are more about the data itself, and if the code is around that concept, is very productive and safe in practique.<p>What make Duck typing unsafe is that is easy to &quot;break&quot; the type in runtime (ie: monkey patching, adding or removing things) and this is what I expect structural to <i>retain</i> it safety better...
anothernewdude将近 3 年前
I get that issue all the time, recursing on list structures in Python. You always have to test for types, because strings, dictionaries and a whole ton of objects are iterable too.
jolux将近 3 年前
The general case this article describes is a type being confused for another type. This happens to me all the time when programming with Python and Pandas, and somewhat less often in Elixir. The `+` operator in Python will concatenate sequences as well as add numbers together, and I have definitely forgotten to parse strings before adding them together before. This stuff happens all the time, people who prefer dynamic languages just don&#x27;t see it as a problem.
dataflow将近 3 年前
I&#x27;m not gonna claim the conclusion is wrong (or right), but Shoot() is probably not the most compelling example one could try to, well, shoot down.<p>How about a function like add()? It might mean something like BigInteger.add, or something like List.add, or Set.add, and these could all satisfy the same interface while behaving very differently.
评论 #31749174 未加载
mikebenfield将近 3 年前
I&#x27;m not a massive fan of duck typing, but it&#x27;s literally never even occurred to me to worry about the concern addressed in this article (that an object accidentally conforms to an interface and is used as such).
评论 #31749282 未加载
nfoz将近 3 年前
With interface-typing, is there a right way to use the type system to declare &quot;this function takes a (statically-certifiable) positive integer&quot;, not because it would fail to output a value for other numbers, but because its output would be incorrect or meaningless?<p>In scientific programming I&#x27;ve often wanted that, to push asserting particular constraints on values to the caller and&#x2F;or the type system (whether that&#x27;s compile-time or runtime type checks). But those constrained types would always match the interface of their unconstrained forms.
评论 #31750077 未加载
评论 #31750317 未加载
评论 #31783371 未加载
评论 #31795594 未加载
car_analogy将近 3 年前
In addition to all the problems others have already noted, duck typing also loses the machine-checked documentation of explicit typing. With types, I don&#x27;t have to guess what a function accepts or returns, and when I refactor, I can count on the compiler to find many issues for me.<p>This is not a tradeoff, where I sacrifice development speed for safety. I <i>gain</i> speed, because types help guide me, and catch errors I would otherwise need tests for.
nfoz将近 3 年前
I&#x27;m not sure if this counts, but where I run into this sort of problem it&#x27;s with conventional methods&#x2F;operators, like `operator+`. I&#x27;ve had lots of bugs over the years from something doing string-concat or list-append instead of integer+, or vice-versas.
chris_armstrong将近 3 年前
this is such annoying clickbait, of course structural typing can have unsafe behaviours: <a href="https:&#x2F;&#x2F;twitter.com&#x2F;ckarmstrong&#x2F;status&#x2F;1536931168654073856?s=20&amp;t=8X6ACml2OAQxsU2O-kfVQA" rel="nofollow">https:&#x2F;&#x2F;twitter.com&#x2F;ckarmstrong&#x2F;status&#x2F;1536931168654073856?s...</a><p>I&#x27;ve used JavaScript and TypeScript professionally for more than 7 years now and I&#x27;m still discovering new ways structural typing can break systems in novel ways. It&#x27;s not like you don&#x27;t benefit from the reduced need to type everything everywhere, but there are still holes that can leave subtle bugs in your system.