layout | title | partof | num | language | next-page | previous-page |
---|---|---|---|---|---|---|
tour |
์ผ์ด์ค ํด๋์ค |
scala-tour |
11 |
ko |
pattern-matching |
multiple-parameter-lists |
์ค์นผ๋ผ๋ ์ผ์ด์ค ํด๋์ค ๊ฐ๋ ์ ์ง์ํ๋ค. ์ผ์ด์ค ํด๋์ค๋ ์๋์ ๊ฐ์ ํน์ง์ ๊ฐ์ง๋ ์ผ๋ฐ ํด๋์ค์ด๋ค.
- ๊ธฐ๋ณธ์ ์ผ๋ก ๋ถ๋ณ
- ํจํด ๋งค์นญ์ ํตํด ๋ถํด๊ฐ๋ฅ
- ๋ ํผ๋ฐ์ค๊ฐ ์๋ ๊ตฌ์กฐ์ ์ธ ๋๋ฑ์ฑ์ผ๋ก ๋น๊ต๋จ
- ์ด๊ธฐํ์ ์ด์์ด ๊ฐ๊ฒฐํจ
์ถ์ ์์ ํด๋์ค Notification๊ณผ ์ธ๊ฐ์ ํน์ Notification ํ์ ๋ค(์ผ์ด์ค ํด๋์ค Email, SMS, VoiceRecording์ผ๋ก ๊ตฌํ๋จ)๋ก ๊ตฌ์ฑ๋ Notificationํ์ ๊ณ์ธต๊ตฌ์กฐ๋ฅผ ์ํ ์์ ๊ฐ ํ๋ ์๋ค.
abstract class Notification
case class Email(sourceEmail: String, title: String, body: String) extends Notification
case class SMS(sourceNumber: String, message: String) extends Notification
case class VoiceRecording(contactName: String, link: String) extends Notification
์ผ์ด์คํด๋์ค๋ฅผ ์ธ์คํด์คํ ํ๋ ๊ฒ์ ์ฝ๋ค (new ํค์๋๋ฅผ ์ฌ์ฉํ ํ์๊ฐ ์์์ ์ฃผ๋ชฉํ์.)
val emailFromJohn = Email("john.doe@mail.com", "Greetings From John!", "Hello World!")
์ผ์ด์ค ํด๋์ค์ ์์ฑ์ ํ๋ผ๋ฏธํฐ๋ค์ public ๊ฐ์ผ๋ก ๋ค๋ค์ง๋ฉฐ, ์ง์ ์ ๊ทผ์ด ๊ฐ๋ฅํ๋ค.
val title = emailFromJohn.title
println(title) // prints "Greetings From John!"
์ฌ๋ฌ๋ถ์ ์ผ์ด์ค ํด๋์ค์ ํ๋๋ฅผ ์ง์ ์์ ํ ์ ์๋ค. (ํ๋ ์์ var๋ฅผ ๋ฃ์ผ๋ฉด ๊ฐ๋ฅํ์ง๋ง, ๊ถ์ฅ๋์ง๋ ์๋๋ค.)
emailFromJohn.title = "Goodbye From John!" // ์ด๊ฒ์ ์ปดํ์ผ์์ ์๋ฌ๊ฐ ๋๋ค. ์ฐ๋ฆฌ๋ val์ธ ํ๋์ ๋ค๋ฅธ ๊ฐ์ ํ ๋นํ ์ ์์ผ๋ฉฐ, ๋ชจ๋ ์ผ์ด์ค ํด๋์ค ํ๋๋ ๊ธฐ๋ณธ์ ์ผ๋ก val์ด๋ค.
๋์ ์, ๋น์ ์ copy๋ฉ์๋๋ฅผ ์ฌ์ฉํด์ ๋ณต์ฌ๋ณธ์ ๋ง๋ค์ ์๋ค. ์๋์์ ๋ณด๋ฏ, ๋น์ ์ ๋ช๋ช ํ๋๋ฅผ ๋์ฒดํ ์ ์๋ค.
val editedEmail = emailFromJohn.copy(title = "I am learning Scala!", body = "It's so cool!")
println(emailFromJohn) // prints "Email(john.doe@mail.com,Greetings From John!,Hello World!)"
println(editedEmail) // prints "Email(john.doe@mail.com,I am learning Scala,It's so cool!)"
๋ชจ๋ ์ผ์ด์ค ํด๋์ค์ ๋ํด์ ์ค์นผ๋ผ ์ปดํ์ผ๋ฌ๋ ๊ตฌ์กฐ์ ๋๋ฑ์ฑ์ ๊ตฌํํ equals ๋ฉ์๋์, toString ๋ฉ์๋๋ฅผ ์์ฑํ๋ค.
val firstSms = SMS("12345", "Hello!")
val secondSms = SMS("12345", "Hello!")
if (firstSms == secondSms) {
println("They are equal!")
}
println("SMS is: " + firstSms)
์ ์ฝ๋๋ ์๋๊ณผ ๊ฐ์ด ์ถ๋ ฅํ๋ค
They are equal!
SMS is: SMS(12345, Hello!)
์ผ์ด์ค ํด๋์ค๋ฅผ ํตํด, ๋ฐ์ดํฐ์ ํจ๊ป ๋์ํ๋ ํจํด๋งค์นญ์ ์ฌ์ฉํ ์ ์๋ค. ์ด๋คNotification ํ์ ์ ๋ฐ๋๋์ ๋ฐ๋ผ ๋ค๋ฅธ ๋ฉ์์ง๋ฅผ ์ถ๋ ฅํ๋ ํจ์๊ฐ ์๋ค.
def showNotification(notification: Notification): String = {
notification match {
case Email(email, title, _) =>
"You got an email from " + email + " with title: " + title
case SMS(number, message) =>
"You got an SMS from " + number + "! Message: " + message
case VoiceRecording(name, link) =>
"you received a Voice Recording from " + name + "! Click the link to hear it: " + link
}
}
val someSms = SMS("12345", "Are you there?")
val someVoiceRecording = VoiceRecording("Tom", "voicerecording.org/id/123")
println(showNotification(someSms))
println(showNotification(someVoiceRecording))
// prints:
// You got an SMS from 12345! Message: Are you there?
// you received a Voice Recording from Tom! Click the link to hear it: voicerecording.org/id/123
์๋์ if ๋ฐฉ์ด๊ตฌ๋ฌธ์ ์ฌ์ฉํ ๋ค๋ฅธ ์์ ๊ฐ ์๋ค. if ๋ฐฉ์ด๊ตฌ๋ฌธ์ ํตํด, ํจํด์ด ์ผ์นํ๋ ๋ถ๊ธฐ๋ฌธ์ ๋ฐฉ์ด๊ตฌ๋ฌธ์์ ์กฐ๊ฑด์ด false๋ฅผ ๋ฆฌํดํ๋ฉด ์คํจํ๋ค.
def showNotificationSpecial(notification: Notification, specialEmail: String, specialNumber: String): String = {
notification match {
case Email(email, _, _) if email == specialEmail =>
"You got an email from special someone!"
case SMS(number, _) if number == specialNumber =>
"You got an SMS from special someone!"
case other =>
showNotification(other) // nothing special, delegate to our original showNotification function
}
}
val SPECIAL_NUMBER = "55555"
val SPECIAL_EMAIL = "jane@mail.com"
val someSms = SMS("12345", "Are you there?")
val someVoiceRecording = VoiceRecording("Tom", "voicerecording.org/id/123")
val specialEmail = Email("jane@mail.com", "Drinks tonight?", "I'm free after 5!")
val specialSms = SMS("55555", "I'm here! Where are you?")
println(showNotificationSpecial(someSms, SPECIAL_EMAIL, SPECIAL_NUMBER))
println(showNotificationSpecial(someVoiceRecording, SPECIAL_EMAIL, SPECIAL_NUMBER))
println(showNotificationSpecial(specialEmail, SPECIAL_EMAIL, SPECIAL_NUMBER))
println(showNotificationSpecial(specialSms, SPECIAL_EMAIL, SPECIAL_NUMBER))
// prints:
// You got an SMS from 12345! Message: Are you there?
// you received a Voice Recording from Tom! Click the link to hear it: voicerecording.org/id/123
// You got an email from special someone!
// You got an SMS from special someone!
์ค์นผ๋ผ ํ๋ก๊ทธ๋๋ฐ์ ์์ด์, ๋ณดํต model/group ๋ฐ์ดํฐ์ ์ผ์ด์ค ํด๋์ค๋ฅผ ์ฌ์ฉํ๋๋ก ๊ถ์ฅ๋๋๋ฐ, ๋น์ ์ด ๋์ฑ ํํ์ ์ด๊ฑฐ๋ ์ ์ง๋ณด์๊ฐ๋ฅํ ์ฝ๋๋ฅผ ์์ฑํ ๋ ๋์์ด ๋๋ค.
- ๋ถ๋ณ์ฑ์ ๋น์ ์ด ์ธ์ ์ด๋์ ๊ทธ๊ฒ๋ค์ด ์์ ๋๋์ง ์ ๊ฒฝ์ธ ํ์ ์๊ฒ ๋ง๋ค์ด ์ค๋ค.
- ๊ฐ์ ํตํ ๋น๊ต๋ ์ฌ๋ฌ๋ถ์ด ์ธ์คํด์ค๋ค์ ์์ ๊ฐ๋ค์ธ ๊ฒ์ฒ๋ผ ๋น๊ตํ ์ ์๊ฒ ๋ง๋ค์ด ์ค๋ค - ํด๋์ค์ ์ธ์คํด์ค๋ค์ด ๊ฐ ๋๋ ์ฐธ์กฐ๋ฅผ ํตํด ๋น๊ต๋๋์ง์ ๊ฐ์ ๋ถํ์ค์ฑ์ ์ ๊ฑฐ
- ํจํด๋งค์นญ์ ๋ก์ง์ ๋ถ๊ธฐ๋ฅผ ์ฌํํ๊ฒ ๋ง๋ค์ด์ฃผ๋ฉฐ, ๊ฒฐ๊ตญ ์ ์ ๋ฒ๊ทธ์ ๊ฐ๋ ์ฑ ๋์ ์ฝ๋๋ก ์ด์ด์ง๋ค.
๊ณ ๊ดํ ์ฎ๊น