Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
joshgoebel opened this issue May 13, 2021 · 5 comments

Comments

@joshgoebel
Copy link
Contributor

joshgoebel commented May 13, 2021

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))
}
@PureFox48
Copy link

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.

@joshgoebel
Copy link
Contributor Author

joshgoebel commented May 14, 2021

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.

@PureFox48
Copy link

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.

@joshgoebel
Copy link
Contributor Author

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

@PureFox48
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants