wren-cli icon indicating copy to clipboard operation
wren-cli copied to clipboard

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

Open joshgoebel opened this issue 4 years ago • 5 comments
trafficstars

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))
}

joshgoebel avatar May 13 '21 00:05 joshgoebel

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.

PureFox48 avatar May 13 '21 17:05 PureFox48

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.

joshgoebel avatar May 14 '21 21:05 joshgoebel

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.

PureFox48 avatar May 15 '21 17:05 PureFox48

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

joshgoebel avatar May 15 '21 17:05 joshgoebel

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.

PureFox48 avatar May 15 '21 17:05 PureFox48