wren-lang / wren-cli

A command line tool for the Wren programming language

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[RFC] Add `File.write(_,_)` to complete the pair, given we have `File.read(_)`

joshgoebel opened this issue · comments

To compliment read we should also have:

File.write(filename, content)

IE, a naive copy function could be written like:

copy(src, dest) {
  File.write(dest, File.read(src))
}

You're right, we should have such a function preferably with an option, if the file already exists, to either append to it or overwrite it.

Well now this is where our naming gets us into trouble... Ruby's API takes an offset as a 3rd parameter... if no offset is given the file is truncated.

But we also have create already (vs maybe append?)... So that begs the question of:

// not ambiguous, create a new file
File.create("output.json", JSON.encode(data))

Except we already have File.create(_,_) where the second argument is a function... (of course we could check the type of the argument, but I'm not sure we want to go down that road)


I tend to personally thing of read full file and write full file as the special "quick" cases... if I want to append to a file then I'd probably open it first... though that might just be me.

I opened this issue literally because of the exact example above (shove some JSON to an output file) and know in many languages this can be done with a single quick function.

It would probably be better to have two functions, File.write and File.append, though I agree that the first one is the more important.

I'm not convinced a static function is required for every file operation though, rather than merely super-common ones.

Well, having a static function (or an overload of an existing one) for appending is just a suggestion as I've seen these in other languages. C#, for example, has File.appendAllText.

However, I'm not bothered about it being in the CLI distribution personally as it's easy enough to write your own.