HNHacker News
TopNewBestAskShowJobs

jznsis

2 karma · joined May 15, 2024

submissionscomments
jznsis··on The Type of Sprintf
I seem to be missing something, thus thank you for taking your time

> Wouldn't that be like if in C, you had to write the arguments in reverse order of the format string, like this?

Exactly and that's not what we want

Let's go through this step by step:

sprintf "%s %d" returns a function, taking a string and returning a function taking an integer and returning a string, right?

sprintf "%s %d" "hello" returns a function taking an integer, returning a string, right?

sprintf "%s %d" "hello" 42 returns a string, right?

Thus my intuition of the return type of sprintf "%s %d" would be string -> (int -> string) not int -> (string -> string)

jznsis··on The Type of Sprintf
No
jznsis··on The Type of Sprintf
Yes, but the final result is out_str, not a forward to console, isn't it?

Either way, the format string discussion for the printf familiy of functions is very similar between them

jznsis··on The Type of Sprintf
I'm not that familiar with syntax of functional programming languages.

Isn't Int -> String -> String a shorthand for Int -> (String -> String)?

Thus I would have assumed the first argument to the result of sprintf "%s %d" would be an integer, then a string and the final result would be a string.

But the invocation would be sprintf "%s %d" "hello" 42, wouldn't it?

jznsis··on The Type of Sprintf
Nice writeup! It would be helpful to seen an actual invocation of the new sprintf function

I know it's supposed to be pseudo code and most likely it's something like sprintf "% world %d" "hello" 42 but I got a little confused by the type list being backwards from what I expected

jznsis··on The Type of Sprintf
This is just a choice of having to provide an output buffer to the function instead of returning a new buffer since it's easier to inject custom allocator behaviour that way