Permalink
Browse files

final comments to the tutorial

  • Loading branch information...
1 parent 9e6f03a commit bc44f14855c9f17fb75e731a3032ac5ed48148d7 @dgruntz dgruntz committed Apr 24, 2012
@@ -13,7 +13,7 @@ Scala permite pasar a un método como parámetro funciones que no reciban parám
Para aclarar un poco esto aquí se muestra un ejemplo:
- object TargetTest1 extends Application {
+ object TargetTest1 extends App {
def whileLoop(cond: => Boolean)(body: => Unit): Unit =
if (cond) {
body
@@ -32,7 +32,7 @@ Es posible combinar el uso de [operadores de infijo y postfijo (infix/postfix)](
Aquí mostramos la implementación de una declaración tipo repetir-a-menos-que (repetir el bucle a no ser que se cumpla X condición):
- object TargetTest2 extends Application {
+ object TargetTest2 extends App {
def loop(body: => Unit): LoopUnlessCond =
new LoopUnlessCond(body)
protected class LoopUnlessCond(body: => Unit) {
@@ -9,7 +9,7 @@ num: 31
language: es
---
-Scala soporta anotaciones de varianza para parámetros de tipo para [clases genéricas.](generic-classes.html). A diferencia de Java 5 (es decir: [JDK 1.5](http://java.sun.com/j2se/1.5/)), las anotaciones de varianza pueden ser agregadas cuando una abstracción de clase es definidia, mientras que en Java 5, las anotaciones de varianza son dadas por los clientes cuando una albstracción de clase es usada.
+Scala soporta anotaciones de varianza para parámetros de tipo para [clases genéricas](generic-classes.html). A diferencia de Java 5 (es decir: [JDK 1.5](http://java.sun.com/j2se/1.5/)), las anotaciones de varianza pueden ser agregadas cuando una abstracción de clase es definidia, mientras que en Java 5, las anotaciones de varianza son dadas por los clientes cuando una albstracción de clase es usada.
In the page about generic classes an example for a mutable stack was given. We explained that the type defined by the class `Stack[T]` is subject to invariant subtyping regarding the type parameter. This can restrict the reuse of the class abstraction. We now derive a functional (i.e. immutable) implementation for stacks which does not have this restriction. Please note that this is an advanced example which combines the use of [polymorphic methods](polymorphic-methods.html), [lower type bounds](lower-type-bounds.html), and covariant type parameter annotations in a non-trivial fashion. Furthermore we make use of [inner classes](inner-classes.html) to chain the stack elements without explicit links.
@@ -22,16 +22,16 @@ En el artículo sobre clases genéricas dimos un ejemplo de una pila mutable. Ex
override def toString() = elem.toString() + " " +
Stack.this.toString()
}
- def top: A = error("no element on stack")
- def pop: Stack[A] = error("no element on stack")
+ def top: A = sys.error("no element on stack")
+ def pop: Stack[A] = sys.error("no element on stack")
override def toString() = ""
}
- object VariancesTest extends Application {
+ object VariancesTest extends App {
var s: Stack[Any] = new Stack().push("hello");
s = s.push(new Object())
s = s.push(7)
- Console.println(s)
+ println(s)
}
La anotación `+T` declara que el tipo `T` sea utilizado solamente en posiciones covariantes. De forma similar, `-T` declara que `T` sea usado en posiciones contravariantes. Para parámetros de tipo covariantes obtenemos una relación de subtipo covariante con respecto al parámetro de tipo. Para nuestro ejemplo, esto significa que `Stack[T]` es un subtipo de `Stack[S]` si `T` es un subtipo de `S`. Lo contrario se cumple para parámetros de tipo que son etiquetados con un signo `-`.
@@ -9,13 +9,14 @@ num: 32
language: es
---
-[Parámetros implícitos](implicit-parameters.html) y métodos también pueden definir conversiones implícitas llamadas _vistas_. Una vista de tipo `S` a `T` es definida por un valor implícito que tiene una función del tipo `S => T`, o por un método convertible a un valor de tal tipo.
+[Parámetros implícitos](implicit-parameters.html) y métodos también pueden definir conversiones implícitas llamadas _vistas_. Una vista de tipo `S` a `T` es definida por un valor implícito que tiene una función del tipo `S => T`, o por un método implícito convertible a un valor de tal tipo.
Las vistas son aplicadas en dos situaciones:
-* Si una expresión `e` es de tipo `T`, y `T` no se ajusta al tipo esperado de la expresión `pt`.
+* Si una expresión `e` es de tipo `S`, y `S` no se ajusta al tipo esperado de la expresión `T`.
* En una selección `e.m` con `e` de tipo `T`, si el selector `m` no es un miembro de `T`.
-En el primer caso, una vista `v` es buscada la cual sea aplicable a `e` y cuyo tipo resultado se ajusta a `pt`. En el segundo caso, una vista `v` es buscada para la cual sea aplicable a `e` y cuyor resultado contenga un miembro llamado `m`.
+En el primer caso, una vista `v` es buscada la cual sea aplicable a `e` y cuyo tipo resultado se ajusta a `T`.
+En el segundo caso, una vista `v` es buscada para la cual sea aplicable a `e` y cuyor resultado contenga un miembro llamado `m`.
La siguiente operación entre las dos listas `xs` y `ys` de tipo `List[Int]` es legal:
@@ -36,7 +37,7 @@ La función `list2ordered` puede ser también expresada con el uso de un _límit
El compilador de Scala que genera después genera el código equivalente a la definición de `list2ordered` vista anteriormente.
-El objeto `scala.Predef` importado implicitamente declara varios tipos predefinidos (ej. `Pair`) and métodos (ej. `error`) pero también varias vistas. El siguiente ejemplo muestra una idea de la vista predefinida `charWrapper`:
+El objeto `scala.Predef` importado implicitamente declara varios tipos predefinidos (ej. `Pair`) and métodos (ej. `assert`) pero también varias vistas. El siguiente ejemplo muestra una idea de la vista predefinida `charWrapper`:
final class RichChar(c: Char) {
def isDigit: Boolean = Character.isDigit(c)
@@ -12,7 +12,7 @@ Scala allows parameterless function names as parameters of methods. When such a
The following code demonstrates this mechanism:
- object TargetTest1 extends Application {
+ object TargetTest1 extends App {
def whileLoop(cond: => Boolean)(body: => Unit): Unit =
if (cond) {
body
@@ -25,13 +25,13 @@ The following code demonstrates this mechanism:
}
}
-The function whileLoop takes two parameters `cond` and `body`. When the function is applied, the actual parameters do not get evaluated. But whenever the formal parameters are used in the body of `whileLoop`, the implicitly created nullary functions will be evaluated instead. Thus, our method `whileLoop` implements a Java-like while-loop with a recursive implementation scheme.
+The function `whileLoop` takes two parameters `cond` and `body`. When the function is applied, the actual parameters do not get evaluated. But whenever the formal parameters are used in the body of `whileLoop`, the implicitly created nullary functions will be evaluated instead. Thus, our method `whileLoop` implements a Java-like while-loop with a recursive implementation scheme.
We can combine the use of [infix/postfix operators](operators.html) with this mechanism to create more complex statements (with a nice syntax).
Here is the implementation of a loop-unless statement:
- object TargetTest2 extends Application {
+ object TargetTest2 extends App {
def loop(body: => Unit): LoopUnlessCond =
new LoopUnlessCond(body)
protected class LoopUnlessCond(body: => Unit) {
@@ -10,7 +10,7 @@ num: 31
Scala supports variance annotations of type parameters of [generic classes](generic-classes.html). In contrast to Java 5 (aka. [JDK 1.5](http://java.sun.com/j2se/1.5/)), variance annotations may be added when a class abstraction is defined, whereas in Java 5, variance annotations are given by clients when a class abstraction is used.
-In the page about generic classes an example for a mutable stack was given. We explained that the type defined by the class `Stack[T]` is subject to invariant subtyping regarding the type parameter. This can restrict the reuse of the class abstraction. We now derive a functional (i.e. immutable) implementation for stacks which does not have this restriction. Please note that this is an advanced example which combines the use of [polymorphic methods](polymorphic-methods.html), [lower type bounds](lower-type-bounds.html), and covariant type parameter annotations in a non-trivial fashion. Furthermore we make use of [inner classes](inner-classes.html) to chain the stack elements without explicit links.
+In the page about [generic classes](generic-classes.html) an example for a mutable stack was given. We explained that the type defined by the class `Stack[T]` is subject to invariant subtyping regarding the type parameter. This can restrict the reuse of the class abstraction. We now derive a functional (i.e. immutable) implementation for stacks which does not have this restriction. Please note that this is an advanced example which combines the use of [polymorphic methods](polymorphic-methods.html), [lower type bounds](lower-type-bounds.html), and covariant type parameter annotations in a non-trivial fashion. Furthermore we make use of [inner classes](inner-classes.html) to chain the stack elements without explicit links.
class Stack[+A] {
def push[B >: A](elem: B): Stack[B] = new Stack[B] {
@@ -19,16 +19,16 @@ In the page about generic classes an example for a mutable stack was given. We e
override def toString() = elem.toString() + " " +
Stack.this.toString()
}
- def top: A = error("no element on stack")
- def pop: Stack[A] = error("no element on stack")
+ def top: A = sys.error("no element on stack")
+ def pop: Stack[A] = sys.error("no element on stack")
override def toString() = ""
}
- object VariancesTest extends Application {
+ object VariancesTest extends App {
var s: Stack[Any] = new Stack().push("hello");
s = s.push(new Object())
s = s.push(7)
- Console.println(s)
+ println(s)
}
The annotation `+T` declares type `T` to be used only in covariant positions. Similarly, `-T` would declare `T` to be used only in contravariant positions. For covariant type parameters we get a covariant subtype relationship regarding this type parameter. For our example this means `Stack[T]` is a subtype of `Stack[S]` if `T` is a subtype of `S`. The opposite holds for type parameters that are tagged with a `-`.
View
@@ -8,13 +8,14 @@ tutorial: scala-tour
num: 32
---
-[Implicit parameters](implicit-parameters.html) and methods can also define implicit conversions called _views_. A view from type `S` to type `T` is defined by an implicit value which has function type `S => T`, or by a method convertible to a value of that type.
+[Implicit parameters](implicit-parameters.html) and methods can also define implicit conversions called _views_. A view from type `S` to type `T` is defined by an implicit value which has function type `S => T`, or by an implicit method convertible to a value of that type.
Views are applied in two situations:
-* If an expression `e` is of type `T`, and `T` does not conform to the expression's expected type `pt`.
+* If an expression `e` is of type `S`, and `S` does not conform to the expression's expected type `T`.
* In a selection `e.m` with `e` of type `T`, if the selector `m` does not denote a member of `T`.
-In the first case, a view `v` is searched which is applicable to `e` and whose result type conforms to `pt`. In the second case, a view `v` is searched which is applicable to `e` and whose result contains a member named `m`.
+In the first case, a view `v` is searched which is applicable to `e` and whose result type conforms to `T`.
+In the second case, a view `v` is searched which is applicable to `e` and whose result contains a member named `m`.
The following operation on the two lists xs and ys of type `List[Int]` is legal:
@@ -35,7 +36,7 @@ The `list2ordered` function can also be expressed with the use of a _view bound_
The Scala compiler then generates code equivalent to the definition of `list2ordered` given above.
-The implicitly imported object `scala.Predef` declares several predefined types (e.g. `Pair`) and methods (e.g. `error`) but also several views. The following example gives an idea of the predefined view `charWrapper`:
+The implicitly imported object `scala.Predef` declares several predefined types (e.g. `Pair`) and methods (e.g. `assert`) but also several views. The following example gives an idea of the predefined view `charWrapper`:
final class RichChar(c: Char) {
def isDigit: Boolean = Character.isDigit(c)

0 comments on commit bc44f14

Please sign in to comment.