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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Stringly Typed

39 点作者 todsacerdoti4 天前

15 条评论

cartoffal大约 23 小时前
The idea of &quot;type safety over the network&quot; is a fiction.<p>When it comes down to it, what is being sent over the network is 1s and 0s. At some point, some layer (probably multiple layers) are going to have to interpret that sequence of 1s and 0s into a particular shape. There are two main ways of doing that:<p>- Know what format the data is in, but not the content (self-describing formats) - in which case the data you end up with might be of an arbitrary shape, and casting&#x2F;interpreting the data is left to the application<p>- Know what format the data is in, AND know the content (non-self-describing formats) - in which case the runtime will parse out the data for you from the raw bits and you get to use it in its structured form.<p>The fact that this conversion happens doesn&#x27;t depend on the language; JS is no more unsafe than any other language in this regard, and JSON is no better or worse of a data serialisation format for it. The boundary already exists, and <i>someone</i> has to handle what happens when the data is the wrong shape. Where that boundary ends up influences the shape of the application but what is preferable will depend on the application and developer.
评论 #43919214 未加载
评论 #43922202 未加载
评论 #43919814 未加载
评论 #43922170 未加载
评论 #43924194 未加载
bqmjjx0kac大约 23 小时前
Not trying to be mean, but there&#x27;s not much content here. It&#x27;s a definition of the term &quot;stringly typed&quot; (from another blog) followed by the idea of using appropriate types.
评论 #43919040 未加载
titzer大约 23 小时前
&gt; JavaScript is a weakly typed language without much type safety<p>This is a little inaccurate. JavaScript is <i>dynamically</i> typed. Values carry strong, unforgeable type information (tags), though tags for numbers are extremely cheap and optimized away whenever possible. It is not possible that JavaScript &quot;forgets&quot; that 1.0 is a number and allows a program to use it as a &quot;pointer&quot;.
评论 #43919318 未加载
评论 #43919267 未加载
zzo38computer大约 12 小时前
I think that JSON is overused (DER is better), but even without JSON, string data is also overused, in cases where numbers or other types would do better. (Unicode string types are also overused, but if a different type other than strings is better anyways, then Unicode is not the main issue here anyways.)<p>&gt; If you&#x27;re using a strongly typed language like TypeScript, receiving the user object as any or unknown type is unfortunate. You&#x27;ll lose all the type safety and you can only regain it with manual type checking.<p>This is not specific to &quot;stringly typed&quot; stuff or to JSON, but is just the case when you transfer data that may use multiple types. In strongly typed programming languages, your program can parse it as the data that it expects and use an error handler when it is not what it is expected. (If you do expect that it may have any type, then you might be able to pass the unparsed value if appropriate; for example, I have a ASN1_Value structure in some of my C programs for this purpose.)
评论 #43924293 未加载
t-writescode大约 21 小时前
I have such an addiction to types that the first thing I do to anything as it comes in and out of systems I own (even across systems I own) is put it back into types and error all the things if it’s invalid.<p>I assumed this was the regular action because it seems so much safer to me.
somat大约 21 小时前
Openbsd went with a stringly typed system for their pledge api.<p>I think it was for simplicity of use. But i find it a very strange interface from a bunch of C programmers.<p><a href="https:&#x2F;&#x2F;man.openbsd.org&#x2F;pledge" rel="nofollow">https:&#x2F;&#x2F;man.openbsd.org&#x2F;pledge</a><p>update: Found the commit message<p><pre><code> Move to next tame() API. The flags are now passed as a very simple string, which results in tame() code placements being much more recognizeable. tame() can be moved to unistd.h and does not need cpp symbols to turn the bits on and off. The resulting API is a bit unexpected, but simplifies the mapping to enabling bits in the kernel substantially.</code></pre>
valorzard大约 23 小时前
I know JSON is the standard now, but are there “better” serialization formats out there? Especially since JSON doesn’t know what an integer is in the spec
评论 #43919231 未加载
评论 #43923129 未加载
评论 #43919003 未加载
评论 #43923149 未加载
评论 #43919470 未加载
评论 #43919046 未加载
评论 #43918875 未加载
评论 #43918893 未加载
TeaVMFan大约 22 小时前
This is one of my favorite things about the Flavour framework: strongly-typed web service calls:<p><a href="https:&#x2F;&#x2F;frequal.com&#x2F;Flavour&#x2F;book.html#org44d6b49" rel="nofollow">https:&#x2F;&#x2F;frequal.com&#x2F;Flavour&#x2F;book.html#org44d6b49</a><p>Your single-page app code calls your backend API using strong types. Your code is clean and the framework handles marshaling and unmarshalling JSON.
评论 #43923431 未加载
Ciantic大约 23 小时前
When Anders Hejlsberg did a lot of those talks to sell TypeScript, he described a lot of JavaScript as &quot;stringly typed&quot;, which is very obvious with all the addEventListener(&quot;click&quot;, ...) and so on depending on certain strings. The term itself is not compliment, if you describe someone else&#x27;s API as such, it&#x27;s not well taken.
评论 #43919145 未加载
dleeftink大约 21 小时前
Op might like Structurae&#x27;s Binary Protocol, type safe from door-to-door[0]. There are lot more interesting use-cases there!<p>[0]: <a href="https:&#x2F;&#x2F;github.com&#x2F;zandaqo&#x2F;structurae">https:&#x2F;&#x2F;github.com&#x2F;zandaqo&#x2F;structurae</a>
teo_zero大约 12 小时前
An example of &quot;stringly typed&quot; in C is when you have to pass &quot;r+&quot; to fopen().
评论 #43923199 未加载
GauntletWizard大约 23 小时前
An API that uses JSON isn&#x27;t &quot;Stringly Typed&quot;. An API that lacks any validation on the JSON you pass to it is. Under their definition, nearly everything is stringly typed if if passes a system boundry, because serialization transforms everything into a string - Sometimes a byte string, sure, but you end up with transport-neutral single object whose interpretation is understood by metadata, and that&#x27;s a good thing, because you don&#x27;t need to waste time interpreting it at every layer it passes through.<p>The modern advice to &quot;Use a serialization library&quot; is actually encoding several hard pieces of learning into one. There was a time when save files for most games were just memory dumps of large sections of memory. You dumped raw C-objects, including pointers to other objects, directly. You ended up with a tangled mess of references, but it was simple to write code for, cheap to write to disk, cheap to read from disk, and easy to break. Basically every update to a game broke all of the save files, because the most minor of tweaks could change the object layout generated by the compiler. The first change was to put magic strings at the beginning to inform the version - So at least you displayed an error message rather that executing some unexpected part of the save file as code.<p>This lesson was hard learned as we entered the networked age, where you couldn&#x27;t trust the incoming messages weren&#x27;t malicious - And you certainly couldn&#x27;t trust, with all of the terribly-behaving middleware, that they were well-formed. Writing serialization&#x2F;deserialization code is not hard, but it&#x27;s annoyingly rote, and you would need it for dozens upon dozens of classes. So instead we switched to standardized libraries for serialization and deserialization.<p>Java and Python both had serialization libraries where whole objects could be serialized - Along with everything they referenced. This lead to <i>massive</i> security holes, because it was easy for them to take a huge chunk of working memory with them, because circular references to root objects allowed them to grab parts of other operations, or even application secrets. Python was worse, as the pickle library allowed serializing whole bytecode; Meaning every load was an arbitrary code execution.<p>Modern serialization libraries have come to a compromise. They serialize data only in primitives. You have to rebuild the tangled web of cross object references yourself. This often sucks, but it&#x27;s far better than the alternatives we&#x27;ve found.<p>GraphQL is popular for precisely this reason. You can avoid most of the serialization and deserialization steps and query what you want directly, allowing you to access deeply-linked properties of deeply linked objects without the expensive round trips and security barriers being checked and rechecked; But the expressiveness comes at a distinct cost in terms of getting those barriers on the server side really right, because the default allow permissions make it easy to leak.
lock1大约 23 小时前
Unfortunately you can&#x27;t escape stringly-typed (and other mess) in language with structural type system.
turnsout大约 23 小时前
Yes, once you start noticing the (mis)use of strings, it&#x27;s everywhere. I set my IDE to make strings a bright orange color so they&#x27;re very noticeable in the code.<p>With that said, you can overdo it. For example, if you&#x27;re constructing a URL in an internal method that will never be seen by the caller, it&#x27;s okay to just use a bare &quot;https&quot; without turning it into an enum like Scheme.HTTPSecure.
strongly-typed大约 13 小时前
my nemesis