Skip to content

Added singleton-object page in Korean #672

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 3 commits into from
Feb 20, 2017
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
2 changes: 1 addition & 1 deletion ko/tutorials/tour/_posts/2017-02-13-unified-types.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
layout: tutorial
title: 통합 타입
title: 통합된 타입

disqus: true

Expand Down
57 changes: 0 additions & 57 deletions ko/tutorials/tour/_posts/2017-02-13-views.md

This file was deleted.

58 changes: 58 additions & 0 deletions ko/tutorials/tour/implicit-conversions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
layout: tutorial
title: 암시적 변환

disqus: true

tutorial: scala-tour
num: 26
language: ko
---

타입 `S`로부터 타입 `T`로의 암시적 변환는 함수 타입 `S => T`의 암시적 값이나 해당 타입으로 변환 가능한 암시적 메소드로 정의된다.

암시적 변환은 두 가지 상황에 적용된다.

* 표현식 `e`의 타입이 `S`이고, `S`는 표현식의 기대 타입 `T`를 따르지 않을 때.
* `e`의 타입이 `T`인 `e.m`를 선택한 상황에서, 선택자 `m`이 `T`의 멤버가 아닐 때.


첫 번째 경우에서 변환 `c`가 `e`에 적용되며, 결과 타입이 `T`를 따르는지 탐색한다.
두 번째 경우에선 변환 `c`가 `e`에 적용되며, 결과가 `m`이라는 이름의 멤버를 포함하고 있는지 탐색한다.

타입이 `List[Int]`인 두 리스트 xs와 ys의 아래 연산은 허용된다:

xs <= ys

아래에 정의된 암시적 메소드 `list2ordered`와 `int2ordered`가 범위 안에 있다고 가정한다.

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] { /* .. */ }

암시적으로 임포트되는 오브젝트 `scala.Predef`는 미리 정의된 여러 타입(예: `Pair`)과 메소드(예: `assert`)뿐만 아니라 여러 뷰도 함께 선언한다.

예를들면, `java.lang.Integer`를 기대하는 자바 메서드를 호출할때, `scala.Int`를 대신 넘겨도 무방하다. 그 이유는 Predef가 아래 암시적 변환들을 포함하기 때문이다.

```tut
import scala.language.implicitConversions

implicit def int2Integer(x: Int) =
java.lang.Integer.valueOf(x)
```

암시적 변환이 무분별하게 사용될 경우 잠재적인 위험을 가질 수 있기 때문에, 컴파일러는 암시적 변환의 선언을 컴파일할때 경고한다.

To turn off the warnings take either of these actions:
경고를 끄기 위해서는 아래 중 하나를 선택해야 한다.

* `scala.language.implicitConversions` 를 암시적 변환의 선언이 있는 범위로 import
* `-language:implicitConversions` 옵션으로 컴파일러 실행

변환이 컴팡일러에 의해 적용될때 경고가 발생하지 않는다.


윤창석, 이한욱 옮김, 고광현 업데이트
76 changes: 76 additions & 0 deletions ko/tutorials/tour/singleton-objects.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
---
layout: tutorial
title: 싱글톤 객체

disqus: true

tutorial: scala-tour
num: 12
language: ko

next-page: xml-processing
previous-page: pattern-matching
---

[클래스](classes.html)의 각 인스턴스와 연관되지 않은 메서드와 값들은 싱글톤 객체에 속하며, `class`대신에 `object`를 사용해 표시된다.

```
package test

object Blah {
def sum(l: List[Int]): Int = l.sum
}
```

위 `sum`메서드는 전역적으로 접근가능하고 참조될수 있으며, `test.Blah.sum`로 import될수 있다.

싱글턴 객체는 직접 인스턴스화 될 수 없는 싱글턴 클래스를 정의하기 위한 축약형 같은 것이며, `object`의 정의 시점의 같은 이름을 가진 `val` 멤버 같은 것이다. 사실 `val`과 같이, 싱글턴 객체는 변칙적이긴 하지만 [트레잇](traits.html)이나 클래스의 멤버로서 정의될수 있다.

하나의 싱글턴 객체는 클래스와 트레잇으로 확장할수 있다. 사실, [타입 파라미터](generic-classes.html)가 없는 [케이스 클래스](case-classes.html)는 기본적으로 같은 이름의 싱글턴 객체를 생성하며, 구현된 [`Function*`](http://www.scala-lang.org/api/current/scala/Function1.html)을 가진다.

## 동반자(Companions) ##

대부분의 싱글턴 객체는 독립적이지 않으며, 대신에 같은 이름의 클래스와 연관되어있다. 위에서 언급한 클래스의 "같은 이름의 싱글턴 객체"는 이 예이다. 이 현상이 발생할 때, 싱글턴 객체는 클래스의 *동반자 객체* 라고 하며, 그 클래스는 객체의 *동반자 클래스*라고 한다.

[스칼라 문서](https://wiki.scala-lang.org/display/SW/Introduction)는 클래스와 그 동반자 사이의 이동을 위한 특별한 지원을 가지고 있다. 만약 큰 "C"나 "O" 원이 아래에서 위로 접힌 경계를 가진다면, 여러분은 동반자로 이동하기 위해 해당 원을 클릭할수 있다.

하나의 클래스와 그 동반자 객체는 어떤 경우라도, 아래와 같이 *같은* 소스파일에 정의되어야 한다.

```tut
class IntPair(val x: Int, val y: Int)

object IntPair {
import math.Ordering

implicit def ipord: Ordering[IntPair] =
Ordering.by(ip => (ip.x, ip.y))
}
```

타입클래스 패턴을 따를때, 일반적으로 타입클래스 인스턴스들을 동반자 안에 정의된 `ipord`와 같은 [암시적 값들](implicit-parameters.html)로 생각한다.

## 자바 프로그래머들이 주의할 점 ##

`static`은 스칼라에서 키워드가 아니다. 대신에 class를 포함한 static일 수 있는 모든 멤버는 싱글턴 객체에 있어야 한다. 그것들은 부분적으로 또는 그룹 등등으로 import될수 있으며, 같은 문법으로 참조될수 있다.

빈번하게, 자바프로그래머들은 그 인스턴스 멤버를 목적으로 구현 할때 `private`을 사용해 static 멤버를 정의한다. 이것들은 또한 동반자(companion)으로 이동되었다. 일반적인 패턴은 아래와 같이 동반자 객체(object)의 멤버들을 클래스 안으로 import하는 것이다.

```
class X {
import X._

def blah = foo
}

object X {
private def foo = 42
}
```

이것은 또 다른 특징을 설명한다. `private`의 문맥에서 클래스와 그 동반자는 친구다. `객체 X`는 `클래스 X`의 private 멤버들에 접근할수 있다. 하나의 멤버를 *정말로* private하게 만들고 싶다면 `private[this]`를 사용하라.

For Java convenience, methods, including `var`s and `val`s, defined directly in a singleton object also have a static method defined in the companion class, called a *static forwarder*. Other members are accessible via the `X$.MODULE$` static field for `object X`.

자바 편의를 위해서, `var`와 `val`의 것 모두, 싱글턴 객체에 정의된 메서드들은 *static forwarder* 라고 불리는 동반자 클래스안에 정의된 static메서드를 가진다. 다른 멤버들은 `객체 X`를 위한 static 필드 `X$.MODULE$`를 통해 접근할수 있다.

만약 당신이 모든 것을 동반자 객체에 옮기고, 당신이 남겨놓은 모든것이 인스턴스화가 되길 바라지 않는 하나의 클래스라면, 간단하게 그 클래스를 삭제하라. Static forwarder는 여전히 생성된다.