Skip to content
This repository was archived by the owner on Sep 5, 2018. It is now read-only.

Adding scala.io.Target #2

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions text/0000-io-target.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# SLIP-NNN - IO Target

**By: Omid Bakhshandeh**
This is a proposal for adding `scala.io.Target` to Scala std.

## History

| Date | Version |
|---------------|---------------|
| Jun 18th 2015 | Initial Draft |

## Motivation

`scala.io.Source` is one of the most used part of `Scala std`. For many people that are new
to the language and programming IO play an important role.
Scala std doesn't have any support for generating output. Scala programmers use `Java.io`
or `Java.nio` for writing on disk. On the other hand, the incopatibility between Java File
and Scala File makes it hard to have nice Scala code.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you show a code snippet that uses Java nio to write to a file, and contrast with a snippet that uses this API?

http://docs.oracle.com/javase/tutorial/essential/io/file.html has a few examples.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done! let me know if it's enough or if I have to add anything else

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From SLIP committee meeting,

Concerns: Source is somewhat discouraged in its current form, either needs to be deprecated or reworked.

If you add Target with the same flaws it is unlikely to be accepted, however the idea of reworking Source in addition has some favor.

Needs to find a "slipherd".

Maybe a good target for an IO expert group

Jon Pretty - suggestion
Jesse Eichar - suggestion
Haoyi Li - suggestion

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throwing my hat in here. I wrote a library to make I/O in Scala intuitive and I got a lot of positive response to it:
https://github.com/pathikrit/better-files


Here, I propose `Scala.io.Target` to be added to std. This part of library could implement the
output version `Scala.io.Source`.


### Examples

An example could be something like this:

```scala
def fromFile(file: File, bufferSize: Int)(implicit codec: Codec): BufferedSource

def toFile(file: File, bufferSize: Int)(implicit codec: Codec): BufferedTarget

```
It's important to investigate the parts that are a little bit tricky to implement or even impossible like:

```scala
fromURL(url: URL)(implicit codec: Codec): BufferedSource

toURL(url: URL)(implicit codec: Codec): BufferedTarget // is it possible to start a simple webserver
//or something like that?
```

### Example in Java

Here is a simple example from java:

```java
impor java.io.*
Charset charset = Charset.forName("US-ASCII");
File file = new File(...);
String s = ...;
try (BufferedWriter writer = Files.newBufferedWriter(file, charset)) {
writer.write(s, 0, s.length());
} catch (IOException x) {
System.err.format("IOException: %s%n", x);
}
```
and scala version could be something like:

```scala
impor scala.io.Target._
String s = ...
Target.toFile(new File(...), s) // File is Scala file, not Java file
```

## Drawbacks

The only reason that this library shouldn't be implemented is the richness of Java IO library. If this library
is not good enough that people use it and cannot provide all Java.IO functionality like `ByteWriter` or `Channels`,
then implemeting this library could be a waste of time.

## Alternatives
There are some alternatives like `Scalaz` and others, but I think this should be really part of Scala std.