From 6f84bab0a4f16002530dbe39eea912c74a299526 Mon Sep 17 00:00:00 2001 From: Seth Tisue Date: Fri, 6 Nov 2015 15:18:38 -0500 Subject: [PATCH] Scala Tour: call them "implicit conversions" not "views" --- .gitignore | 1 + es/tutorials/tour/implicit-conversions.md | 12 +++++ es/tutorials/tour/tour-of-scala.md | 2 +- es/tutorials/tour/views.md | 51 --------------------- tutorials/tour/implicit-conversions.md | 43 ++++++++++++++++++ tutorials/tour/implicit-parameters.md | 2 +- tutorials/tour/polymorphic-methods.md | 2 +- tutorials/tour/tour-of-scala.md | 2 +- tutorials/tour/unified-types.md | 2 +- tutorials/tour/views.md | 54 ----------------------- 10 files changed, 61 insertions(+), 110 deletions(-) create mode 100644 es/tutorials/tour/implicit-conversions.md delete mode 100644 es/tutorials/tour/views.md create mode 100644 tutorials/tour/implicit-conversions.md delete mode 100644 tutorials/tour/views.md diff --git a/.gitignore b/.gitignore index 5762a98226..b12ed768ca 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,6 @@ _site .DS_Store .project .settings +/.jekyll-metadata *~ vendor/bundle diff --git a/es/tutorials/tour/implicit-conversions.md b/es/tutorials/tour/implicit-conversions.md new file mode 100644 index 0000000000..88779374e6 --- /dev/null +++ b/es/tutorials/tour/implicit-conversions.md @@ -0,0 +1,12 @@ +--- +layout: tutorial +title: Implicit Conversions + +disqus: true + +tutorial: scala-tour +num: 32 +language: es +--- + +(this page has not been translated into Spanish) diff --git a/es/tutorials/tour/tour-of-scala.md b/es/tutorials/tour/tour-of-scala.md index 0fa48516b9..fcfeb51abc 100644 --- a/es/tutorials/tour/tour-of-scala.md +++ b/es/tutorials/tour/tour-of-scala.md @@ -27,7 +27,7 @@ Scala cuenta con un expresivo sistema de tipado que fuerza estáticamente las ab * [clases internas](inner-classes.html) y [tipos abstractos](abstract-types.html) como miembros de objetos, * [tipos compuestos](compound-types.html) * [auto-referencias explicitamente tipadas](explicitly-typed-self-references.html) -* [vistas](views.html) +* [implicit conversions](implicit-conversions.html) * [métodos polimórficos](polymorphic-methods.html) El [mecanismo de inferencia de tipos locales](local-type-inference.html) se encarga de que el usuario no tengan que anotar el programa con información redundante de tipado. Combinadas, estas características proveen una base poderosa para el reuso seguro de abstracciones de programación y para la extensión segura (en cuanto a tipos) de software. diff --git a/es/tutorials/tour/views.md b/es/tutorials/tour/views.md deleted file mode 100644 index acb1a876de..0000000000 --- a/es/tutorials/tour/views.md +++ /dev/null @@ -1,51 +0,0 @@ ---- -layout: tutorial -title: Vistas - -disqus: true - -tutorial: scala-tour -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 implícito convertible a un valor de tal tipo. - -Las vistas son aplicadas en dos situaciones: -* 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 `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: - - xs <= ys - -asumiendo que los métodos implícitos `list2ordered` e `int2ordered` definidos abajo estén en el alcance de la operación: - - implicit def list2ordered[A](x: List[A]) - (implicit elem2ordered: a => Ordered[A]): Ordered[List[A]] = - new Ordered[List[A]] { /* .. */ } - - implicit def int2ordered(x: Int): Ordered[Int] = - new Ordered[Int] { /* .. */ } - -La función `list2ordered` puede ser también expresada con el uso de un _límite de vista_ por un parámetro de tipo: - - implicit def list2ordered[A <% Ordered[A]](x: List[A]): Ordered[List[A]] = ... - -El compilador de Scala genera código equivalente a la definición de `list2ordered` vista anteriormente. - -El objeto `scala.Predef` importado implicitamente declara varios tipos predefinidos (ej. `Pair`) y 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) - // isLetter, isWhitespace, etc. - } - object RichCharTest { - implicit def charWrapper(c: char) = new RichChar(c) - def main(args: Array[String]) { - println('0'.isDigit) - } - } diff --git a/tutorials/tour/implicit-conversions.md b/tutorials/tour/implicit-conversions.md new file mode 100644 index 0000000000..84c48bba41 --- /dev/null +++ b/tutorials/tour/implicit-conversions.md @@ -0,0 +1,43 @@ +--- +layout: tutorial +title: Implicit Conversions + +disqus: true + +tutorial: scala-tour +num: 26 +tutorial-next: polymorphic-methods +tutorial-previous: implicit-parameters +--- + +An implicit conversion 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. + +Implicit conversions are applied in two situations: + +* 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 conversion `c` is searched for which is applicable to `e` and whose result type conforms to `T`. +In the second case, a conversion `c` is searched for 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: + + xs <= ys + +assuming the implicit methods `list2ordered` and `int2ordered` defined below are in scope: + + implicit def list2ordered[A](x: List[A]) + (implicit elem2ordered: A => Ordered[A]): Ordered[List[A]] = + new Ordered[List[A]] { /* .. */ } + + implicit def int2ordered(x: Int): Ordered[Int] = + new Ordered[Int] { /* .. */ } + +The implicitly imported object `scala.Predef` declares several predefined types (e.g. `Pair`) and methods (e.g. `assert`) but also several implicit conversions. + +For example, when calling a Java method that expects a `java.lang.Integer`, you are free to pass it a `scala.Int` instead. That's because Predef includes the following implicit conversions: + + implicit def int2Integer(x: Int) = + java.lang.Integer.valueOf(x) + +To define your own implicit conversions, you must first `import scala.language.implicitConversions` (or invoke the compiler with `-language:implicitConversions`). The feature must be explicitly enabled because it has pitfalls if used indiscriminately. diff --git a/tutorials/tour/implicit-parameters.md b/tutorials/tour/implicit-parameters.md index 06cf58b0cd..88abecaadd 100644 --- a/tutorials/tour/implicit-parameters.md +++ b/tutorials/tour/implicit-parameters.md @@ -6,7 +6,7 @@ disqus: true tutorial: scala-tour num: 25 -tutorial-next: views +tutorial-next: implicit-conversions tutorial-previous: explicitly-typed-self-references --- diff --git a/tutorials/tour/polymorphic-methods.md b/tutorials/tour/polymorphic-methods.md index e1ad6058f2..7f3aadf42d 100644 --- a/tutorials/tour/polymorphic-methods.md +++ b/tutorials/tour/polymorphic-methods.md @@ -8,7 +8,7 @@ tutorial: scala-tour num: 27 tutorial-next: local-type-inference -tutorial-previous: views +tutorial-previous: implicit-conversions --- Methods in Scala can be parameterized with both values and types. Like on the class level, value parameters are enclosed in a pair of parentheses, while type parameters are declared within a pair of brackets. diff --git a/tutorials/tour/tour-of-scala.md b/tutorials/tour/tour-of-scala.md index 3c31301109..19469fd5ca 100644 --- a/tutorials/tour/tour-of-scala.md +++ b/tutorials/tour/tour-of-scala.md @@ -28,7 +28,7 @@ Scala is equipped with an expressive type system that enforces statically that a * [inner classes](inner-classes.html) and [abstract types](abstract-types.html) as object members * [compound types](compound-types.html) * [explicitly typed self references](explicitly-typed-self-references.html) -* [implicit parameters](implicit-parameters.html) and [views](views.html) +* [implicit parameters](implicit-parameters.html) and [conversions](implicit-conversions.html) * [polymorphic methods](polymorphic-methods.html) A [local type inference mechanism](local-type-inference.html) 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. diff --git a/tutorials/tour/unified-types.md b/tutorials/tour/unified-types.md index 9b9cec85db..323511941a 100644 --- a/tutorials/tour/unified-types.md +++ b/tutorials/tour/unified-types.md @@ -17,7 +17,7 @@ In contrast to Java, all values in Scala are objects (including numerical values ## Scala Class Hierarchy ## 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`. -Please note that the diagram above also shows implicit conversions called views between the value classs. +Please note that the diagram above also shows implicit conversions between the value classes. Here is an example that demonstrates that both numbers, characters, boolean values, and functions are objects just like every other object: object UnifiedTypes extends App { diff --git a/tutorials/tour/views.md b/tutorials/tour/views.md deleted file mode 100644 index b1767f3b17..0000000000 --- a/tutorials/tour/views.md +++ /dev/null @@ -1,54 +0,0 @@ ---- -layout: tutorial -title: Views - -disqus: true - -tutorial: scala-tour -num: 26 -tutorial-next: polymorphic-methods -tutorial-previous: implicit-parameters ---- - -[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 `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 `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: - - xs <= ys - -assuming the implicit methods `list2ordered` and `int2ordered` defined below are in scope: - - implicit def list2ordered[A](x: List[A]) - (implicit elem2ordered: A => Ordered[A]): Ordered[List[A]] = - new Ordered[List[A]] { /* .. */ } - - implicit def int2ordered(x: Int): Ordered[Int] = - new Ordered[Int] { /* .. */ } - -The `list2ordered` function can also be expressed with the use of a _view bound_ for a type parameter: - - implicit def list2ordered[A <% Ordered[A]](x: List[A]): Ordered[List[A]] = ... - -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. `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) - // isLetter, isWhitespace, etc. - } - object RichCharTest { - implicit def charWrapper(c: char) = new RichChar(c) - def main(args: Array[String]) { - println('0'.isDigit) - } - } -