This repository was archived by the owner on Sep 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Adding scala.io.Target #2
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
||
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. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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