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

Why can I serialize scala.collection.immutable.List ?

2 replies
Henry Goldwire
Joined: 2012-01-11,
User offline. Last seen 42 years 45 weeks ago.
The current API does not indicate that scala.collection.immutable.List has the Serializable trait:http://www.scala-lang.org/api/current/index.html#scala.collection.immutable.List

bizarrely, this works fine for me:

import java.io._
val il = scala.collection.immutable.List("1","hi","yo")
val store = new ObjectOutputStream(new FileOutputStream("store.dat")) store.writeObject(il) store.close     

val in = new ObjectInputStream(new FileInputStream("store.dat")) val copy = in.readObject() println( copy)  println(copy.getClass)

Shouldn't it give me a java.io.NotSerializableException ?

Thanks,Henry
Derek Williams 3
Joined: 2011-08-12,
User offline. Last seen 42 years 45 weeks ago.
Re: Why can I serialize scala.collection.immutable.List ?
On Wed, Jan 11, 2012 at 3:48 PM, Henry Goldwire <hgoldwire [at] manaproducts [dot] com> wrote:
The current API does not indicate that scala.collection.immutable.List has the Serializable trait:http://www.scala-lang.org/api/current/index.html#scala.collection.immutable.List


 Probably because :: and Nil are serializable:
http://www.scala-lang.org/api/current/index.html#scala.collection.immutable.$colon$colon  http://www.scala-lang.org/api/current/index.html#scala.collection.immutable.Nil$ 

--
Derek Williams
Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
Re: Why can I serialize scala.collection.immutable.List ?
scala> List(1,2).isInstanceOf[Serializable]res0: Boolean = true
Cheers,√
On Wed, Jan 11, 2012 at 11:48 PM, Henry Goldwire <hgoldwire [at] manaproducts [dot] com> wrote:
The current API does not indicate that scala.collection.immutable.List has the Serializable trait:http://www.scala-lang.org/api/current/index.html#scala.collection.immutable.List

bizarrely, this works fine for me:

import java.io._
val il = scala.collection.immutable.List("1","hi","yo")
val store = new ObjectOutputStream(new FileOutputStream("store.dat")) store.writeObject(il) store.close     

val in = new ObjectInputStream(new FileInputStream("store.dat"))  val copy = in.readObject() println( copy)  println(copy.getClass)

Shouldn't it give me a java.io.NotSerializableException ?

Thanks,Henry



--
Viktor Klang

Akka Tech LeadTypesafe - Enterprise-Grade Scala from the Experts

Twitter: @viktorklang

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