This page is no longer maintained — Please continue to the home page at www.scala-lang.org

IO

9 replies
Ken Faulkner
Joined: 2009-03-23,
User offline. Last seen 42 years 45 weeks ago.
Hi
Does anyone know if its planned that Scala will always simply depend on the various Java IO libs that exist out there, or if something "Scala-ish" will eventually be incorporated into the language/standard library?
I'm only in the process of learning it, and I have to admit up front to NOT being a Java fan. Scala the language I think definitely looks like something I want to use more and more but having all these "please go and use standard Java lib" is a bit jarring. For example, something as simple as reading/writing some binary file screams (to me at least) as something ugly and non-Scala looking when FileOutputStreams combined with ObjectOutputStreams from Java.io are imported and used about the place. Considering I'm fairly hostile to Java maybe I'm in a minority (my background is C/C++, Java and more recently Python).
Will we ever get to the state (as in Python) where something like:
import scala.io.binaryfiles
var fd = binaryfiles.open("foo.img", "w") fd.write( myBinaryData )fd.close()

or
var fd = binaryfiles.open("foo.img")var data = fd.read()fd.close()

Or am I just not advanced enough in my Scala learning yet?
Ken


Matt Shannon
Joined: 2009-04-05,
User offline. Last seen 42 years 45 weeks ago.
Re: IO

Hi Ken,

Ken Faulkner wrote:
> Will we ever get to the state (as in Python) where something like:
>
> import scala.io.binaryfiles
>
> var fd = binaryfiles.open("foo.img", "w")
> fd.write( myBinaryData )
> fd.close()
>
>
> or
>
> var fd = binaryfiles.open("foo.img")
> var data = fd.read()
> fd.close()
>
Firstly, that's screaming for a closure! If such a more scalaish
library was written I'd assume the use would look something like:

var data =
BinaryFiles.using("foo.img") { fd =>
fd.read()
}

or even:

var data =
for (fd <- BinaryFiles.using("foo.img")) {
fd.read()
}

The library code for "using" would make sure the file got closed even if
an exception was thrown during the execution of the block, similar to
RAII in C++.

Some stuff to this end is in scalax (http://scalax.scalaforge.org/).
Not sure about binary files, but for example this allows you to write
something much more scalaish to read lines from a text file:

val lines : List[String] = testFile.lines.toList

or indeed:

val words =
for (line <- testFile.lines.toList; word <-
line.split("\\s+").toList) yield {
word
}

I don't believe anything like this is in the scala standard library, but
it's been a while since I used it for file IO.

Thanks,

Matt

DRMacIver
Joined: 2008-09-02,
User offline. Last seen 42 years 45 weeks ago.
Re: IO

2009/4/5 Ken Faulkner :
> Hi
> Does anyone know if its planned that Scala will always simply depend on the
> various Java IO libs that exist out there, or if something "Scala-ish" will
> eventually be incorporated into the language/standard library?

I don't think there are formal plans for a decently complete scala.io,
but it's definitely be discussed. I'm pretty certain that there will
be one at some point, but I don't know when.

Stepan Koltsov
Joined: 2008-12-20,
User offline. Last seen 42 years 45 weeks ago.
Re: IO

Yes, yes! Lack of handy IO utilities — is a big drawback of the
standard scala library (compared to python's one for example).

There are classes in the scalax library:

http://scalax.scalaforge.org/api/

That allow something like this:

InputStreamResource.url("http://server/file.txt")
.reader("ch1251").lines.map(_.toInt).foreach { ... }

or

for (r <- ReaderResource.file("/etc/passwd")) {
// r is a Reader
}

Hope, these or similar implementations will be added to the scala
standard library one day. IO utilities are really needed for everyday
programming.

S.

On Sun, Apr 5, 2009 at 15:32, Ken Faulkner wrote:
> Hi
> Does anyone know if its planned that Scala will always simply depend on the
> various Java IO libs that exist out there, or if something "Scala-ish" will
> eventually be incorporated into the language/standard library?
> I'm only in the process of learning it, and I have to admit up front to NOT
> being a Java fan. Scala the language I think definitely looks like something
> I want to use more and more but having all these "please go and use standard
> Java lib" is a bit jarring. For example, something as simple as
> reading/writing some binary file screams (to me at least) as something ugly
> and non-Scala looking when FileOutputStreams combined with
> ObjectOutputStreams from Java.io are imported and used about the place.
> Considering I'm fairly hostile to Java maybe I'm in a minority (my
> background is C/C++, Java and more recently Python).
> Will we ever get to the state (as in Python) where something like:
> import scala.io.binaryfiles
> var fd = binaryfiles.open("foo.img", "w")
> fd.write( myBinaryData )
> fd.close()
>
> or
> var fd = binaryfiles.open("foo.img")
> var data = fd.read()
> fd.close()
>
> Or am I just not advanced enough in my Scala learning yet?
> Ken
>
>
>

Ricky Clarkson
Joined: 2008-12-19,
User offline. Last seen 3 years 2 weeks ago.
Re: IO
Until something is standardised on, you can easily write something yourself that wraps the Java libs.  Probably most of us have a small set of IO utilities.  How does Python manage to ensure the file is closed if writing fails somehow?

2009/4/5 Ken Faulkner <ken.faulkner@gmail.com>
Hi
Does anyone know if its planned that Scala will always simply depend on the various Java IO libs that exist out there, or if something "Scala-ish" will eventually be incorporated into the language/standard library?
I'm only in the process of learning it, and I have to admit up front to NOT being a Java fan. Scala the language I think definitely looks like something I want to use more and more but having all these "please go and use standard Java lib" is a bit jarring. For example, something as simple as reading/writing some binary file screams (to me at least) as something ugly and non-Scala looking when FileOutputStreams combined with ObjectOutputStreams from Java.io are imported and used about the place. Considering I'm fairly hostile to Java maybe I'm in a minority (my background is C/C++, Java and more recently Python).
Will we ever get to the state (as in Python) where something like:
import scala.io.binaryfiles
var fd = binaryfiles.open("foo.img", "w") fd.write( myBinaryData )fd.close()

or
var fd = binaryfiles.open("foo.img")var data = fd.read()fd.close()

Or am I just not advanced enough in my Scala learning yet?
Ken



Randall R Schulz
Joined: 2008-12-16,
User offline. Last seen 1 year 29 weeks ago.
Re: IO

On Sunday April 5 2009, Ricky Clarkson wrote:
> Until something is standardised on, you can easily write something
> yourself that wraps the Java libs. Probably most of us have a small
> set of IO utilities. How does Python manage to ensure the file is
> closed if writing fails somehow?

Now you've done it.

(This came up a couple of weeks ago, if you'll recall. As I recall it
described, they have shut-down hooks that iterate over all the open
file handles and flush and close them.)

Randall Schulz

Ricky Clarkson
Joined: 2008-12-19,
User offline. Last seen 3 years 2 weeks ago.
Re: IO
So how does that work for a long-running application rather than one that terminates before all resources are consumed?

2009/4/5 Randall R Schulz <rschulz@sonic.net>
On Sunday April 5 2009, Ricky Clarkson wrote:
> Until something is standardised on, you can easily write something
> yourself that wraps the Java libs.  Probably most of us have a small
> set of IO utilities.  How does Python manage to ensure the file is
> closed if writing fails somehow?

Now you've done it.

(This came up a couple of weeks ago, if you'll recall. As I recall it
described, they have shut-down hooks that iterate over all the open
file handles and flush and close them.)


Randall Schulz

Randall R Schulz
Joined: 2008-12-16,
User offline. Last seen 1 year 29 weeks ago.
Re: IO

On Sunday April 5 2009, Ricky Clarkson wrote:
> So how does that work for a long-running application rather than one
> that terminates before all resources are consumed?

I don't know. Perhaps Mr. Russ Paielli will inform us.

RRS

Ken Faulkner
Joined: 2009-03-23,
User offline. Last seen 42 years 45 weeks ago.
Re: IO
Hi
I was looking at scalax and was thinking about tinkering about with that a little, but honestly wasn't sure if it was still being maintained (I was only going by the status page, which is 6 months old)...
Once I get to grips with Scala more, I might start snooping through the scalax source and see if I can contribute anything.
Ken

On Sun, Apr 5, 2009 at 10:25 PM, Stepan Koltsov <stepan.koltsov@gmail.com> wrote:
Yes, yes! Lack of handy IO utilities — is a big drawback of the
standard scala library (compared to python's one for example).

There are classes in the scalax library:

http://scalax.scalaforge.org/api/

That allow something like this:

InputStreamResource.url("http://server/file.txt")
 .reader("ch1251").lines.map(_.toInt).foreach { ... }

or

for (r <- ReaderResource.file("/etc/passwd")) {
 // r is a Reader
}

Hope, these or similar implementations will be added to the scala
standard library one day. IO utilities are really needed for everyday
programming.


S.

On Sun, Apr 5, 2009 at 15:32, Ken Faulkner <ken.faulkner@gmail.com> wrote:
> Hi
> Does anyone know if its planned that Scala will always simply depend on the
> various Java IO libs that exist out there, or if something "Scala-ish" will
> eventually be incorporated into the language/standard library?
> I'm only in the process of learning it, and I have to admit up front to NOT
> being a Java fan. Scala the language I think definitely looks like something
> I want to use more and more but having all these "please go and use standard
> Java lib" is a bit jarring. For example, something as simple as
> reading/writing some binary file screams (to me at least) as something ugly
> and non-Scala looking when FileOutputStreams combined with
> ObjectOutputStreams from Java.io are imported and used about the place.
> Considering I'm fairly hostile to Java maybe I'm in a minority (my
> background is C/C++, Java and more recently Python).
> Will we ever get to the state (as in Python) where something like:
> import scala.io.binaryfiles
> var fd = binaryfiles.open("foo.img", "w")
> fd.write( myBinaryData )
> fd.close()
>
> or
> var fd = binaryfiles.open("foo.img")
> var data = fd.read()
> fd.close()
>
> Or am I just not advanced enough in my Scala learning yet?
> Ken
>
>
>

Ken Faulkner
Joined: 2009-03-23,
User offline. Last seen 42 years 45 weeks ago.
Re: IO
Hi
Afaik Python just makes sure FD's are closed during the GC process. So if the VM is killed before the GC happens, then you'll be left with dangling FD's about the place. Probably a rare problem, but I think it could definitely happen.
Ken

On Mon, Apr 6, 2009 at 1:06 AM, Ricky Clarkson <ricky.clarkson@gmail.com> wrote:
Until something is standardised on, you can easily write something yourself that wraps the Java libs.  Probably most of us have a small set of IO utilities.  How does Python manage to ensure the file is closed if writing fails somehow?

2009/4/5 Ken Faulkner <ken.faulkner@gmail.com>
Hi
Does anyone know if its planned that Scala will always simply depend on the various Java IO libs that exist out there, or if something "Scala-ish" will eventually be incorporated into the language/standard library?
I'm only in the process of learning it, and I have to admit up front to NOT being a Java fan. Scala the language I think definitely looks like something I want to use more and more but having all these "please go and use standard Java lib" is a bit jarring. For example, something as simple as reading/writing some binary file screams (to me at least) as something ugly and non-Scala looking when FileOutputStreams combined with ObjectOutputStreams from Java.io are imported and used about the place. Considering I'm fairly hostile to Java maybe I'm in a minority (my background is C/C++, Java and more recently Python).
Will we ever get to the state (as in Python) where something like:
import scala.io.binaryfiles
var fd = binaryfiles.open("foo.img", "w") fd.write( myBinaryData )fd.close()

or
var fd = binaryfiles.open("foo.img")var data = fd.read()fd.close()

Or am I just not advanced enough in my Scala learning yet?
Ken




Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland