Skip to content

Commit 68b8bba

Browse files
committed
Merge branch 'gh-pages' of github.com:scala/scala.github.com into gh-pages
2 parents 84eb046 + 9d085b2 commit 68b8bba

File tree

7 files changed

+202
-2
lines changed

7 files changed

+202
-2
lines changed

sips.new/sip-submission.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,11 @@ The committee will decide for each SIP whether it should be accepted, rejected,
3636

3737
## Who is on the SIP committee ##
3838

39-
TBD
39+
Right Now:
40+
41+
* Martin Odersky
42+
* Paul Philips
43+
* Josh Suereth
44+
* Adriaan Moors
45+
46+
We will ask new members to join from time to time. The committee decides collectively, but Martin reserves the final say if there is a disagreement.

sips.new/sip-tutorial.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ class Foo
3333

3434
Testing changes requires installing [Jekyll](https://github.com/mojombo/jekyll/wiki/Install).
3535

36-
Use the `run-server.sh` script locally to ensure your SIP looks correct in the website.
36+
Use the `jekyll --server` command to start up a local server. You can then view your changes at [http://localhost:4000](http://localhost:4000).

tour/classes.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
layout: overview
3+
title: Classes
4+
---
5+
6+
Classes in Scala are static templates that can be instantiated into many objects at runtime.
7+
Here is a class definition which defines a class `Point`:
8+
9+
class Point(xc: Int, yc: Int) {
10+
var x: Int = xc
11+
var y: Int = yc
12+
def move(dx: Int, dy: Int) {
13+
x = x + dx
14+
y = y + dy
15+
}
16+
override def toString(): String = "(" + x + ", " + y + ")";
17+
}
18+
19+
The class defines two variables `x` and `y` and two methods: `move` and `toString`. `move` takes two integer arguments but does not return a value (the implicit return type `Unit` corresponds to `void` in Java-like languages). `toString`, on the other hand, does not take any parameters but returns a `String` value. Since `toString` overrides the pre-defined `toString` method, it has to be tagged with the `override` flag.
20+
21+
Classes in Scala are parameterized with constructor arguments. The code above defines two constructor arguments, `xc` and `yc`; they are both visible in the whole body of the class. In our example they are used to initialize the variables `x` and `y`.
22+
23+
Classes are instantiated with the new primitive, as the following example will show:
24+
25+
object Classes {
26+
def main(args: Array[String]) {
27+
val pt = new Point(1, 2)
28+
println(pt)
29+
pt.move(10, 10)
30+
println(pt)
31+
}
32+
}
33+
34+
The program defines an executable application Classes in form of a top-level singleton object with a `main` method. The `main` method creates a new `Point` and stores it in value `pt`. _Note that values defined with the `val` construct are different from variables defined with the `var` construct (see class `Point` above) in that they do not allow updates; i.e. the value is constant._
35+
36+
Here is the output of the program:
37+
38+
(1, 2)
39+
(11, 12)

tour/index.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
layout: overview
3+
title: Tour of Scala
4+
---
5+
6+
Scala is a modern multi-paradigm programming language designed to express common programming patterns in a concise, elegant, and type-safe way. It smoothly integrates features of object-oriented and functional languages.
7+
8+
## Scala is object-oriented ##
9+
Scala is a pure object-oriented language in the sense that [every value is an object](unified_types.html). Types and behavior of objects are described by [classes](classes.html) and [traits](traits.html). Classes are extended by subclassing and a flexible [mixin-based composition](mixin-class-composition.html) mechanism as a clean replacement for multiple inheritance.
10+
11+
## Scala is functional ##
12+
Scala is also a functional language in the sense that every function is a value. Scala provides a lightweight syntax for defining anonymous functions, it supports higher-order functions, it allows functions to be nested, and supports currying. Scala's case classes and its built-in support for pattern matching model algebraic types used in many functional programming languages.
13+
14+
Furthermore, Scala's notion of pattern matching naturally extends to the processing of XML data with the help of right-ignoring sequence patterns. In this context, sequence comprehensions are useful for formulating queries. These features make Scala ideal for developing applications like web services.
15+
16+
## Scala is statically typed ##
17+
Scala is equipped with an expressive type system that enforces statically that abstractions are used in a safe and coherent manner. In particular, the type system supports:
18+
* generic classes
19+
* variance annotations
20+
* upper and lower type bounds,
21+
* inner classes and abstract types as object members
22+
* compound types
23+
* explicitly typed self references
24+
* views
25+
* polymorphic methods
26+
27+
A local type inference mechanism takes care that the user is not required to annotate the program with redundant type information. In combination, these features provide a powerful basis for the safe reuse of programming abstractions and for the type-safe extension of software.
28+
29+
## Scala is extensible ##
30+
31+
In practice, the development of domain-specific applications often requires domain-specific language extensions. Scala provides a unique combination of language mechanisms that make it easy to smoothly add new language constructs in form of libraries:
32+
* any method may be used as an infix or postfix operator
33+
* closures are constructed automatically depending on the expected type (target typing).
34+
35+
A joint use of both features facilitates the definition of new statements without extending the syntax and without using macro-like meta-programming facilities.
36+
Scala interoperates with Java and .NET
37+
38+
Scala is designed to interoperate well with the popular Java 2 Runtime Environment (JRE). In particular, the interaction with the mainstream object-oriented Java programming language is as smooth as possible. Scala has the same compilation model (separate compilation, dynamic class loading) like Java and allows access to thousands of existing high-quality libraries. Support for the .NET Framework (CLR) is also available.
39+
40+
Please continue to the next page to read more.

tour/mixin-class-composition.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
layout: overview
3+
title: Mixin Class Composition
4+
---
5+
6+
As opposed to languages that only support _single inheritance_, Scala has a more general notion of class reuse. Scala makes it possible to reuse the _new member definitions of a class_ (i.e. the delta in relationship to the superclass) in the definition of a new class. This is expressed as a _mixin-class composition_. Consider the following abstraction for iterators.
7+
8+
abstract class AbsIterator {
9+
type T
10+
def hasNext: Boolean
11+
def next: T
12+
}
13+
14+
Next, consider a mixin class which extends `AbsIterator` with a method `foreach` which applies a given function to every element returned by the iterator. To define a class that can be used as a mixin we use the keyword `trait`.
15+
16+
trait RichIterator extends AbsIterator {
17+
def foreach(f: T => Unit) { while (hasNext) f(next) }
18+
}
19+
20+
Here is a concrete iterator class, which returns successive characters of a given string:
21+
22+
class StringIterator(s: String) extends AbsIterator {
23+
type T = Char
24+
private var i = 0
25+
def hasNext = i < s.length()
26+
def next = { val ch = s charAt i; i += 1; ch }
27+
}
28+
29+
We would like to combine the functionality of `StringIterator` and `RichIterator` into a single class. With single inheritance and interfaces alone this is impossible, as both classes contain member impementations with code. Scala comes to help with its _mixin-class composition_. It allows the programmers to reuse the delta of a class definition, i.e., all new definitions that are not inherited. This mechanism makes it possible to combine `StringIterator` with `RichIterator`, as is done in the following test program which prints a column of all the characters of a given string.
30+
31+
object StringIteratorTest {
32+
def main(args: Array[String]) {
33+
class Iter extends StringIterator(args(0)) with RichIterator
34+
val iter = new Iter
35+
iter foreach println
36+
}
37+
}
38+
39+
The `Iter` class in function `main` is constructed from a mixin composition of the parents `StringIterator` and `RichIterator` with the keyword `with`. The first parent is called the _superclass_ of `Iter`, whereas the second (and every other, if present) parent is called a _mixin_.

tour/traits.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
layout: overview
3+
title: Traits
4+
---
5+
6+
Similar to interfaces in Java, traits are used to define object types by specifying the signature of the supported methods. Unlike Java, Scala allows traits to be partially implemented; i.e. it is possible to define default implementations for some methods. In contrast to classes, traits may not have constructor parameters.
7+
Here is an example:
8+
9+
trait Similarity {
10+
def isSimilar(x: Any): Boolean
11+
def isNotSimilar(x: Any): Boolean = !isSimilar(x)
12+
}
13+
14+
This trait consists of two methods `isSimilar` and `isNotSimilar`. While `isSimilar` does not provide a concrete method implementation (it is abstract in the terminology of Java), method `isNotSimilar` defines a concrete implementation. Consequently, classes that integrate this trait only have to provide a concrete implementation for `isSimilar`. The behavior for `isNotSimilar` gets inherited directly from the trait. Traits are typically integrated into a [class](classes.html) (or other traits) with a [mixin class composition](mixin-class-composition.html):
15+
16+
class Point(xc: Int, yc: Int) extends Similarity {
17+
var x: Int = xc
18+
var y: Int = yc
19+
def isSimilar(obj: Any) =
20+
obj.isInstanceOf[Point] &&
21+
obj.asInstanceOf[Point].x == x
22+
}
23+
object TraitsTest extends Application {
24+
val p1 = new Point(2, 3)
25+
val p2 = new Point(2, 4)
26+
val p3 = new Point(3, 3)
27+
println(p1.isNotSimilar(p2))
28+
println(p1.isNotSimilar(p3))
29+
println(p1.isNotSimilar(2))
30+
}
31+
32+
Here is the output of the program:
33+
34+
false
35+
true
36+
true

tour/unified_types.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
layout: overview
3+
title: Unified Types
4+
---
5+
6+
In contrast to Java, all values in Scala are objects (including numerical values and functions). Since Scala is class-based, all values are instances of a class. The diagram below illustrates the class hierarchy.
7+
8+
![Scala Type Hierarchy](classhierarchy.img_assist_custom.png)
9+
10+
## Scala Class Hierarchy ##
11+
12+
The superclass of all classes scala.Any has two direct subclasses scala.AnyVal and scala.AnyRef representing two different class worlds: value classes and reference classes. All value classes are predefined; they correspond to the primitive types of Java-like languages. All other classes define reference types. User-defined classes define reference types by default; i.e. they always (indirectly) subclass scala.AnyRef. Every user-defined class in Scala implicitly extends the trait scala.ScalaObject. Classes from the infrastructure on which Scala is running (e.g. the Java runtime environment) do not extend scala.ScalaObject. If Scala is used in the context of a Java runtime environment, then scala.AnyRef corresponds to java.lang.Object.
13+
Please note that the diagram above also shows implicit conversions called views between the value classs.
14+
Here is an example that demonstrates that both numbers, characters, boolean values, and functions are objects just like every other object:
15+
16+
object UnifiedTypes {
17+
def main(args: Array[String]) {
18+
val set = new scala.collection.mutable.HashSet[Any]
19+
set += "This is a string" // add a string
20+
set += 732 // add a number
21+
set += 'c' // add a character
22+
set += true // add a boolean value
23+
set += main _ // add the main function
24+
val iter: Iterator[Any] = set.elements
25+
while (iter.hasNext) {
26+
println(iter.next.toString())
27+
}
28+
}
29+
}
30+
31+
The program declares an application UnifiedTypes in form of a top-level singleton object with a main method. Themain method defines a local variable set which refers to an instance of class `HashSet[Any]`. The program adds various elements to this set. The elements have to conform to the declared set element type Any. In the end, string representations of all elements are printed out.
32+
33+
Here is the output of the program:
34+
35+
c
36+
true
37+
<function>
38+
732
39+
This is a string

0 commit comments

Comments
 (0)