Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Programming_in_Scala,_2nd_edition.pdf
Скачиваний:
25
Добавлен:
24.03.2015
Размер:
22.09 Mб
Скачать

Section 24.19

Chapter 24 · The Scala Collections API

605

24.19Migrating from Scala 2.7

If you have existing applications written in Scala 2.7, porting them to use the new collections should be almost automatic. There are only a couple of possible issues to take care of.

Generally, the old functionality of Scala 2.7 collections has been left in place. Some features have been deprecated, which means they will removed in some future release. You will get a deprecation warning when you compile code that makes use of these features in Scala 2.8. In a few places deprecation was unfeasible, because the operation in question was retained in 2.8, but changed in meaning or performance characteristics. These cases will be flagged with migration warnings when compiled under 2.8. To get full deprecation and migration warnings with suggestions how to change your code, pass the -deprecation and -Xmigration flags to scalac.8 You can also pass the same options to the scala interpreter to get the warnings in an interactive session. Example:

>scala -deprecation -Xmigration Welcome to Scala version 2.8.1.

Type in expressions to have them evaluated.

Type :help for more information.

scala> val xs = List((1, 2), (3, 4))

xs: List[(Int, Int)] = List((1,2), (3,4))

scala> List.unzip(xs)

<console>:7: warning: method unzip in object List is deprecated: use xs.unzip instead of List.unzip(xs)

List.unzip(xs)

ˆ

res0: (List[Int], List[Int]) = (List(1, 3),List(2, 4))

scala> xs.unzip

res1: (List[Int], List[Int]) = (List(1, 3),List(2, 4))

scala> val m = xs.toMap

m: scala.collection.immutable.Map[Int,Int] = Map((1,2), (3,4))

scala> m.keys

<console>:8: warning: method keys in trait MapLike has

8Note that -Xmigration is an extended option, so it starts with an X.

Cover · Overview · Contents · Discuss · Suggest · Glossary · Index

Section 24.20

Chapter 24 · The Scala Collections API

606

changed semantics: As of 2.8, keys returns Iterable[A]

rather than Iterator[A].

m.keys

ˆ

res2: Iterable[Int] = Set(1, 3)

Two parts of the old libraries were replaced wholesale. For these deprecation warnings were not feasible.

1.The previous scala.collection.jcl package is gone. This package tried to mimic some of the Java collection library design in Scala, but in doing so broke many symmetries. Most people who wanted Java collections bypassed jcl and used java.util directly. Scala 2.8 offers automatic conversion mechanisms between both collection libraries in the JavaConversions object, described in Section 24.18, which replaces the jcl package.

2.Projections have been generalized and cleaned up and are now available as views. It seems that projections were used rarely, so not much code should be affected by this change.

So, if your code uses either jcl or projections there might be some minor rewriting to do.

24.20Conclusion

You’ve now seen how to use Scala’s collection in great detail. Scala’s collections take the approach of giving you powerful building blocks rather than a number of ad hoc utility methods. Putting together two or three such building blocks allows you to express an enormous number of useful computations. This style of library is especially effective due to Scala having a light syntax for function literals, and due to it providing many collection types that are persistent and immutable.

This chapter has shown collections from the point of view of a programmer using the collection library. The next chapter will show you how the collections are built and how you can add your own collection types.

Cover · Overview · Contents · Discuss · Suggest · Glossary · Index

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]