More "idiomatic" cat.jakt
One of my favourite Jakt features is named parameters. Heavy use of anonymous should be a code smell even when calling C or C++.
cat.jakt currently looks like this:
extern struct FILE {}
extern function fopen(anonymous str: raw c_char, anonymous mode: raw c_char) -> raw FILE
extern function fgetc(anonymous file: mutable raw FILE) -> c_int
extern function fclose(anonymous file: mutable raw FILE) -> c_int
extern function feof(anonymous file: mutable raw FILE) -> c_int
extern function putchar(anonymous ch: c_int) -> c_int
function main(args: [String]) {
if args.size() <= 1 {
eprintln("usage: cat <path>")
return 1
}
let filename = args[1]
let mutable file = fopen(filename.c_string(), "r".c_string())
defer fclose(file)
let mutable c = fgetc(file)
while feof(file) == 0 {
putchar(c)
c = fgetc(file)
}
}
It could be written like this:
extern struct FILE {}
extern function fopen(filename: raw c_char, mode: raw c_char) -> raw FILE
extern function fgetc(file: mutable raw FILE) -> c_int
extern function fclose(file: mutable raw FILE) -> c_int
extern function feof(file: mutable raw FILE) -> c_int
extern function putchar(character: c_int) -> c_int
function main(args: [String]) {
if args.size() <= 1 {
eprintln("usage: cat <path>")
return 1
}
let mutable file = fopen(filename: args[1].c_string(), mode: "r".c_string())
defer fclose(file)
let mutable character = fgetc(file)
while feof(file) == 0 {
putchar(character)
character = fgetc(file)
}
}
To my eye, the second version is much more self documenting in terms of making it obvious what is going on. The call to fopen in particular is much clearer, especially without going back to look at the function signature. I considered using byte instead of character as well.
I tried to create a pull request but I messed something up. I have never actually created a pull request in GitHub before so I am not sure yet what I did wrong. Ironically, the only thing I have ever released on GitHub is a toy compiler / transpiler.
https://github.com/SerenityOS/jakt/pull/450
This was good thinking at the time! We probably should have merged some version of your changes. However, since then the stdlib functions for file manipulation have been moved into the jakt:: standard library headers. If those files are still missing the named parameters, that might make a good PR for the current state of jakt