TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

The Type of Sprintf

13 pointsby verdagonabout 1 year ago

4 comments

trealiraabout 1 year ago
Finding out about how, in a programming language with dependent types, types can be returned from functions like values, feels about the same way as when I learned functions can be returned as first-class values in functional programming. It went from &quot;wait, what?&quot; to &quot;of course, why wouldn&#x27;t you be allowed to do that?&quot; From seeming confusing to seeming perfectly natural, leaving you more confused of its abscence than its presence. Though admittedly, I don&#x27;t have much experience with using dependent types seriously.<p>This property of dependent types:<p>&gt; In fact, if you don&#x27;t mind writing a proof or two, our sprintf function can be called on format strings that aren&#x27;t fully known at compile-time! You only need to be able to prove that the string will have certain format specifiers, and then calling sprintf can typecheck.<p>It&#x27;s not new to me, but I also find it pretty cool. Proving things at compile-time that certain runtime conditions will hold using a programming language is so much cooler than just proving it to yourself in your head or on paper, and then doing a runtime assertion in the actual code. Or, not doing a check at all, and just having crashes or security vulnerabilities if you&#x27;re mistaken.
评论 #40362392 未加载
_nalplyabout 1 year ago
Sometimes I dream about a Rust-like programming language with optional garbage collection similar to Swift that runs in three stages: The first one is an idempotent interpreter so heavily sandboxed that it can only read the source code and output only diagnostics and the second stage: a more static programming language. Meta-programming!<p>This would allow simplifying the second stage massively because it is generated code and won&#x27;t need ergonomy. This programming language would parse the sprintf format string exactly as if the compiled program would do it at runtime. When I read the article I thought to myself, aha, dependent types, could it be that it is similar to a user-provided program that computes the types?<p>I have to disclaim that designing programming languages is not my expertise. I am only an amateur.<p>These two stages avoid the complexity of Rust compile-time handling. Instead of hard coding things like lifetimes, generics, const generics, return position impl trait in trait (RPITIT), async fn in trait (AFIT), and so on, have a first stage standard library handling all these things.<p>And the third stage? A sandboxed interpreter with the same or similar syntax as the rest of the thing. I think this would be useful as glue code or just for tiny throw-away scripts. All these three stages would profit from a common base, so they should be provided together.<p>Makes sense?
anonymousiamabout 1 year ago
I am at a loss to understand this. I learned C in 1989 and sprintf takes a minimum of two arguments, but typically takes three or more, the first one being the string for the output to be written to. Perhaps the author was writing about printf, which takes a minimum of one argument, but typically takes two or more?
评论 #40373898 未加载
评论 #40362930 未加载
评论 #40362505 未加载
jznsisabout 1 year ago
Nice writeup! It would be helpful to seen an actual invocation of the new sprintf function<p>I know it&#x27;s supposed to be pseudo code and most likely it&#x27;s something like sprintf &quot;% world %d&quot; &quot;hello&quot; 42 but I got a little confused by the type list being backwards from what I expected
评论 #40362764 未加载