Skip to content

collapse two redundant tour sections into one #990

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 0 additions & 72 deletions _ba/tour/local-type-inference.md

This file was deleted.

2 changes: 1 addition & 1 deletion _ba/tour/operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ partof: scala-tour

num: 30
next-page: by-name-parameters
previous-page: local-type-inference
previous-page: type-inference
prerequisite-knowledge: case-classes

---
Expand Down
2 changes: 1 addition & 1 deletion _ba/tour/polymorphic-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ partof: scala-tour

num: 28

next-page: local-type-inference
next-page: type-inference
previous-page: implicit-conversions
prerequisite-knowledge: unified-types

Expand Down
2 changes: 1 addition & 1 deletion _ba/tour/tour-of-scala.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Konkretno, sistem tipova podržava sljedeće:
* implicitne [parametre](implicit-parameters.html) i [konverzije](implicit-conversions.html)
* [polimorfne metode](polymorphic-methods.html)

Mehanizam za [lokalno zaključivanje tipova](local-type-inference.html) se brine da korisnik ne mora pisati tipove varijabli
Mehanizam za [lokalno zaključivanje tipova](type-inference.html) se brine da korisnik ne mora pisati tipove varijabli
više nego što je potrebno.
U kombinaciji, ove mogućnosti su jaka podloga za bezbjedno ponovno iskorištenje programskih apstrakcija
i za tipski bezbjedno proširenje softvera.
Expand Down
67 changes: 62 additions & 5 deletions _ba/tour/type-inference.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,72 @@
---
layout: tour
title: Type Inference
title: Lokalno zaključivanje tipova (type inference)
language: ba

discourse: true

partof: scala-tour

num: 36
previous-page: packages-and-imports
num: 29
next-page: operators
previous-page: polymorphic-methods

---
Scala ima ugrađen mehanizam zaključivanja tipova koji dozvoljava programeru da izostavi određene anotacije tipova.
Često nije potrebno specificirati tip varijable u Scali,
jer kompajler može sam zaključiti tip iz inicijalizacijskog izraza varijable.
Povratni tipovi metoda također mogu biti izostavljeni jer oni odgovaraju tipu tijela (zadnji izraz u tijelu), koje kompajler sam zaključi.

Slijedi jedan primjer:

```tut
object InferenceTest1 extends App {
val x = 1 + 2 * 3 // the type of x is Int
val y = x.toString() // the type of y is String
def succ(x: Int) = x + 1 // method succ returns Int values
}
```

Za rekurzivne metode, kompajler nije u mogućnosti da zaključi tip rezultata.
Ovo je program koji se ne može kompajlirati iz ovog razloga:

```tut:fail
object InferenceTest2 {
def fac(n: Int) = if (n == 0) 1 else n * fac(n - 1)
}
```

Također nije obavezno specificirati tipske parametre kada se pozivaju [polimorfne metode](polymorphic-methods.html)
ili kada se [generičke klase](generic-classes.html) instanciraju.
Scala kompajler će zaključiti nedostajuće tipske parametre iz konteksta i iz tipova stvarnih parametara metoda/konstruktora.

Ovo je primjer koji to ilustrira:

```
case class MyPair[A, B](x: A, y: B);
object InferenceTest3 extends App {
def id[T](x: T) = x
val p = MyPair(1, "scala") // type: MyPair[Int, String]
val q = id(1) // type: Int
}
```


Zadnje dvije linije ovog programa su ekvivalentne sljedećem kodu gdje su svi zaključeni tipovi eksplicitno napisani:

```
val x: MyPair[Int, String] = MyPair[Int, String](1, "scala")
val y: Int = id[Int](1)
```

U nekim situacijama može biti vrlo opasno osloniti se na Scalin mehanizam zaključivanja tipova:

```tut:fail
object InferenceTest4 {
var obj = null
obj = new Object()
}
```

(this section of the tour has not been translated yet. pull request
with translation welcome!)
Ovaj program se ne može kompajlirati jer je zaključeni tip varijable `obj` tip `Null`.
Pošto je jedina vrijednost tog tipa `null`, nemoguće je dodijeliti ovoj varijabli neku drugu vrijednost.
55 changes: 0 additions & 55 deletions _es/tour/local-type-inference.md

This file was deleted.

2 changes: 1 addition & 1 deletion _es/tour/self-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ partof: scala-tour
num: 27
language: es

next-page: local-type-inference
next-page: type-inference
previous-page: lower-type-bounds
---

Expand Down
2 changes: 1 addition & 1 deletion _es/tour/tour-of-scala.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Scala cuenta con un expresivo sistema de tipado que fuerza estáticamente las ab
* [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.
El [mecanismo de inferencia de tipos locales](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.

## Scala es extensible ##

Expand Down
54 changes: 47 additions & 7 deletions _es/tour/type-inference.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,55 @@
---
layout: tour
title: Type Inference
language: es
title: Inferencia de tipos Local

discourse: true
discourse: false

partof: scala-tour

num: 36
previous-page: packages-and-imports
num: 29
language: es

next-page: unified-types
previous-page: self-types
---

(this section of the tour has not been translated yet. pull request
with translation welcome!)
Scala tiene incorporado un mecanismo de inferencia de tipos el cual permite al programador omitir ciertos tipos de anotaciones. Por ejemplo, generalmente no es necesario especificar el tipo de una variable, ya que el compilador puede deducir el tipo mediante la expresión de inicialización de la variable. También puede generalmente omitirse los tipos de retorno de métodos ya que se corresponden con el tipo del cuerpo, que es inferido por el compilador.

Aquí hay un ejemplo:

object InferenceTest1 extends App {
val x = 1 + 2 * 3 // el tipo de x es Int
val y = x.toString() // el tipo de y es String
def succ(x: Int) = x + 1 // el método succ retorna valores Int
}

Para métodos recursivos, el compilador no es capaz de inferir el tipo resultado. A continuación mostramos un programa el cual falla por esa razón:

object InferenceTest2 {
def fac(n: Int) = if (n == 0) 1 else n * fac(n - 1)
}

Tampoco es obligatorio especificar el tipo de los parámetros cuando se trate de [métodos polimórficos](polymorphic-methods.html) o sean instanciadas [clases genéricas](generic-classes.html). El compilador de Scala inferirá esos tipos de parámetros faltantes mediante el contexto y de los tipos de los parámetros reales del método/constructor.

Aquí se muestra un ejemplo que ilustra esto:

case class MyPair[A, B](x: A, y: B);
object InferenceTest3 extends App {
def id[T](x: T) = x
val p = MyPair(1, "scala") // tipo: MyPair[Int, String]
val q = id(1) // tipo: Int
}

Las últimas dos lineas de este programa son equivalentes al siguiente código, donde todos los tipos inferidos son especificados explicitamente:

val x: MyPair[Int, String] = MyPair[Int, String](1, "scala")
val y: Int = id[Int](1)

En algunas situaciones puede ser bastante peligroso confiar en el mecanismo de inferencia de tipos de Scala, como se ilustra en el siguiente ejemplo:

object InferenceTest4 {
var obj = null
obj = new Object()
}

Este programa no compila porque el tipo inferido para la variable `obj` es `Null`. Ya que el único valor de ese tipo es `null`, es imposible hacer que esta variable refiera a otro valor.
2 changes: 1 addition & 1 deletion _es/tour/unified-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ num: 30
language: es

next-page: variances
previous-page: local-type-inference
previous-page: type-inference
---

A diferencia de Java, todos los valores en Scala son objetos (incluyendo valores numéricos y funciones). Dado que Scala está basado en clases, todos los valores son instancias de una clase. El diagrama siguiente ilustra esta jerarquía de clases:
Expand Down
57 changes: 0 additions & 57 deletions _ko/tour/local-type-inference.md

This file was deleted.

4 changes: 2 additions & 2 deletions _ko/tour/operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ num: 29
language: ko

next-page: automatic-closures
previous-page: local-type-inference
previous-page: type-inference
---

스칼라에선 단일 파라미터를 취하는 모든 메소드를 *중위 연산자*로 사용할 수 있다. 다음은 `and`와 `or`, `negate` 등의 세 가지 메소드를 정의하고 있는 클래스 `MyBool`의 정의다.
Expand All @@ -33,4 +33,4 @@ previous-page: local-type-inference
def not(x: MyBool) = x.negate; // 여기엔 세미콜론이 필요함
def xor(x: MyBool, y: MyBool) = x.or(y).and(x.and(y).negate)

윤창석, 이한욱 옮김
윤창석, 이한욱 옮김
Loading