From 250899202c102c1559eb019803475bbe386b1174 Mon Sep 17 00:00:00 2001 From: Philippus Date: Wed, 15 Aug 2018 14:09:40 +0200 Subject: [PATCH 1/2] Update tut to 0.6.7 --- _ba/tour/basics.md | 6 ++---- _ba/tour/upper-type-bounds.md | 6 ++++-- _pl/tour/case-classes.md | 23 ++++++----------------- _pl/tour/named-arguments.md | 26 +++++++++++--------------- _pt-br/tour/case-classes.md | 25 +++++++------------------ _pt-br/tour/named-arguments.md | 26 +++++++++++--------------- _tour/basics.md | 6 ++---- _tour/named-arguments.md | 4 ++-- _tour/upper-type-bounds.md | 6 ++++-- _zh-cn/tour/basics.md | 6 ++---- scripts/run-tut.sh | 4 +--- 11 files changed, 52 insertions(+), 86 deletions(-) diff --git a/_ba/tour/basics.md b/_ba/tour/basics.md index 24f006f791..65830e2ced 100644 --- a/_ba/tour/basics.md +++ b/_ba/tour/basics.md @@ -216,15 +216,13 @@ if (point == anotherPoint) { println(point + " and " + anotherPoint + " are the same.") } else { println(point + " and " + anotherPoint + " are different.") -} -// Point(1,2) i Point(1,2) su iste. +} // Point(1,2) i Point(1,2) su iste. if (point == yetAnotherPoint) { println(point + " and " + yetAnotherPoint + " are the same.") } else { println(point + " and " + yetAnotherPoint + " are different.") -} -// Point(1,2) su Point(2,2) različite. +} // Point(1,2) su Point(2,2) različite. ``` Ima još mnogo stvari vezanih za case klase koje bismo voljeli objasniti, i ubijeđeni smo da ćete se zaljubiti u njih! 0 diff --git a/_ba/tour/upper-type-bounds.md b/_ba/tour/upper-type-bounds.md index b25b6347b3..b8afa51ee2 100644 --- a/_ba/tour/upper-type-bounds.md +++ b/_ba/tour/upper-type-bounds.md @@ -43,8 +43,10 @@ class PetContainer[P <: Pet](p: P) { val dogContainer = new PetContainer[Dog](new Dog) val catContainer = new PetContainer[Cat](new Cat) -// val lionContainer = new PetContainer[Lion](new Lion) -// ^this would not compile +``` + +```tut:fail +val lionContainer = new PetContainer[Lion](new Lion) // this would not compile ``` Klasa `PetContainer` prima tipski parametar `P` koji mora biti podtip od `Pet`. `Dog` i `Cat` su podtipovi `Pet` tako da možemo kreirati novi `PetContainer[Dog]` i `PetContainer[Cat]`. diff --git a/_pl/tour/case-classes.md b/_pl/tour/case-classes.md index f218595b71..48c6683418 100644 --- a/_pl/tour/case-classes.md +++ b/_pl/tour/case-classes.md @@ -93,12 +93,8 @@ def showNotification(notification: Notification): String = { val someSms = SMS("12345", "Are you there?") val someVoiceRecording = VoiceRecording("Tom", "voicerecording.org/id/123") -println(showNotification(someSms)) -println(showNotification(someVoiceRecording)) - -// wypisuje: -// 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 +println(showNotification(someSms)) // Wypisuje "You got an SMS from 12345! Message: Are you there?" +println(showNotification(someVoiceRecording)) // Wypisuje "you received a Voice Recording from Tom! Click the link to hear it: voicerecording.org/id/123" ``` Poniżej bardziej skomplikowany przykład używający `if` w celu określenia dodatkowych warunków dopasowania: @@ -122,17 +118,10 @@ 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)) - -// wypisuje: -// 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! - +println(showNotificationSpecial(someSms, SPECIAL_EMAIL, SPECIAL_NUMBER)) // Wypisuje "You got an SMS from 12345! Message: Are you there?" +println(showNotificationSpecial(someVoiceRecording, SPECIAL_EMAIL, SPECIAL_NUMBER)) // Wypisuje "you received a Voice Recording from Tom! Click the link to hear it: voicerecording.org/id/123" +println(showNotificationSpecial(specialEmail, SPECIAL_EMAIL, SPECIAL_NUMBER)) // Wypisuje "You got an email from special someone!" +println(showNotificationSpecial(specialSms, SPECIAL_EMAIL, SPECIAL_NUMBER)) // Wypisuje "You got an SMS from special someone!" ``` Programując w Scali zachęca się, abyś jak najszerzej używał klas przypadków do modelowania danych, jako że kod, który je wykorzystuje, jest bardziej ekspresywny i łatwiejszy do utrzymania: diff --git a/_pl/tour/named-arguments.md b/_pl/tour/named-arguments.md index 29b8bc8c36..2fe636808c 100644 --- a/_pl/tour/named-arguments.md +++ b/_pl/tour/named-arguments.md @@ -14,25 +14,21 @@ previous-page: default-parameter-values Wywołując metody i funkcje, możesz użyć nazwy parametru jawnie podczas wywołania: ```tut - def printName(first:String, last:String) = { - println(first + " " + last) - } - - printName("John", "Smith") - // Wypisuje "John Smith" - printName(first = "John", last = "Smith") - // Wypisuje "John Smith" - printName(last = "Smith", first = "John") - // Wypisuje "John Smith" +def printName(first:String, last:String) = { + println(first + " " + last) +} + +printName("John", "Smith") // Wypisuje "John Smith" +printName(first = "John", last = "Smith") // Wypisuje "John Smith" +printName(last = "Smith", first = "John") // Wypisuje "John Smith" ``` Warto zwrócić uwagę na to, że kolejność wyboru parametrów podczas wywołania nie ma znaczenia, dopóki wszystkie parametry są nazwane. Ta funkcjonalność jest dobrze zintegrowana z [domyślnymi wartościami parametrów](default-parameter-values.html): ```tut - def printName(first: String = "John", last: String = "Smith") = { - println(first + " " + last) - } +def printName(first: String = "John", last: String = "Smith") = { + println(first + " " + last) +} - printName(last = "Jones") - // Wypisuje "John Jones" +printName(last = "Jones") // Wypisuje "John Jones" ``` diff --git a/_pt-br/tour/case-classes.md b/_pt-br/tour/case-classes.md index 238197cf70..67d98d6d6d 100644 --- a/_pt-br/tour/case-classes.md +++ b/_pt-br/tour/case-classes.md @@ -93,12 +93,8 @@ def mostrarNotificacao(notificacao: Notification): String = { val algumSMS = SMS("12345", "Você está aí?") val algumaMsgVoz = VoiceRecording("Tom", "voicerecording.org/id/123") -println(mostrarNotificacao(algumSMS)) -println(mostrarNotificacao(algumaMsgVoz)) - -// Saída: -// Você recebeu um SMS de 12345! Mensagem: Você está aí? -// Você recebeu uma Mensagem de Voz de Tom! Clique no link para ouvir: voicerecording.org/id/123 +println(mostrarNotificacao(algumSMS)) // Saída "Você recebeu um SMS de 12345! Mensagem: Você está aí?" +println(mostrarNotificacao(algumaMsgVoz)) // Saída "Você recebeu uma Mensagem de Voz de Tom! Clique no link para ouvir: voicerecording.org/id/123" ``` Aqui um exemplo mais elaborado utilizando a proteção `if`. Com a proteção `if`, o correspondência de padrão irá falhar se a condição de proteção retorna falso. @@ -122,21 +118,14 @@ val algumaMsgVoz = VoiceRecording("Tom", "voicerecording.org/id/123") val emailEspecial = Email("jane@mail.com", "Beber hoje a noite?", "Estou livre depois das 5!") val smsEspecial = SMS("55555", "Estou aqui! Onde está você?") -println(mostrarNotificacaoEspecial(algumSMS, EmailEspecial, NumeroEspecial)) -println(mostrarNotificacaoEspecial(algumaMsgVoz, EmailEspecial, NumeroEspecial)) -println(mostrarNotificacaoEspecial(smsEspecial, EmailEspecial, NumeroEspecial)) -println(mostrarNotificacaoEspecial(smsEspecial, EmailEspecial, NumeroEspecial)) - -// Saída: -// Você recebeu um SMS de 12345! Mensagem: Você está aí? -// Você recebeu uma Mensagem de Voz de Tom! Clique no link para ouvir: voicerecording.org/id/123 -// Você recebeu um email de alguém especial! -// Você recebeu um SMS de alguém especial! - +println(mostrarNotificacaoEspecial(algumSMS, EmailEspecial, NumeroEspecial)) // Saída "Você recebeu um SMS de 12345! Mensagem: Você está aí?" +println(mostrarNotificacaoEspecial(algumaMsgVoz, EmailEspecial, NumeroEspecial)) // Saída "Você recebeu uma Mensagem de Voz de Tom! Clique no link para ouvir: voicerecording.org/id/123" +println(mostrarNotificacaoEspecial(smsEspecial, EmailEspecial, NumeroEspecial)) // Saída "Você recebeu um email de alguém especial!" +println(mostrarNotificacaoEspecial(smsEspecial, EmailEspecial, NumeroEspecial)) // Saída "Você recebeu um SMS de alguém especial!" ``` Ao programar em Scala, recomenda-se que você use classes case de forma pervasiva para modelar / agrupar dados, pois elas ajudam você a escrever código mais expressivo e passível de manutenção: * Imutabilidade libera você de precisar acompanhar onde e quando as coisas são mutadas * Comparação por valor permite comparar instâncias como se fossem valores primitivos - não há mais incerteza sobre se as instâncias de uma classe é comparada por valor ou referência -* Correspondência de padrões simplifica a lógica de ramificação, o que leva a menos bugs e códigos mais legíveis. \ No newline at end of file +* Correspondência de padrões simplifica a lógica de ramificação, o que leva a menos bugs e códigos mais legíveis. diff --git a/_pt-br/tour/named-arguments.md b/_pt-br/tour/named-arguments.md index a481e2babe..f4008c9ca9 100644 --- a/_pt-br/tour/named-arguments.md +++ b/_pt-br/tour/named-arguments.md @@ -14,27 +14,23 @@ language: pt-br Ao chamar métodos e funções, você pode utilizar explicitamente o nome das variáveis nas chamadas, por exemplo: ```tut - def imprimeNome(nome:String, sobrenome:String) = { - println(nome + " " + sobrenome) - } - - imprimeNome("John","Smith") - // Imprime "John Smith" - imprimeNome(nome = "John",sobrenome = "Smith") - // Imprime "John Smith" - imprimeNome(sobrenome = "Smith",nome = "John") - // Imprime "John Smith" +def imprimeNome(nome:String, sobrenome:String) = { + println(nome + " " + sobrenome) +} + +imprimeNome("John","Smith") // Imprime "John Smith" +imprimeNome(nome = "John",sobrenome = "Smith") // Imprime "John Smith" +imprimeNome(sobrenome = "Smith",nome = "John") // Imprime "John Smith" ``` Perceba que a ordem não importa quando você utiliza parâmetros nomeados nas chamadas de métodos e funções, desde que todos os parâmetros sejam declarados. Essa funcionalidade pode ser combinada com [parâmetros com valor padrão](default-parameter-values.html): ```tut - def imprimeNome(nome:String = "John", sobrenome:String = "Smith") = { - println(nome + " " + sobrenome) - } +def imprimeNome(nome:String = "John", sobrenome:String = "Smith") = { + println(nome + " " + sobrenome) +} - imprimeNome(sobrenome = "Forbeck") - // Imprime "John Forbeck" +imprimeNome(sobrenome = "Forbeck") // Imprime "John Forbeck" ``` Dado que é permitido declarar os parâmetros em qualquer ordem, você pode utilizar o valor padrão para parâmetros que aparecem primeiro na lista de parâmetros da função. diff --git a/_tour/basics.md b/_tour/basics.md index 8f35dc17ee..2b4d364e57 100644 --- a/_tour/basics.md +++ b/_tour/basics.md @@ -225,15 +225,13 @@ if (point == anotherPoint) { println(point + " and " + anotherPoint + " are the same.") } else { println(point + " and " + anotherPoint + " are different.") -} -// Point(1,2) and Point(1,2) are the same. +} // Point(1,2) and Point(1,2) are the same. if (point == yetAnotherPoint) { println(point + " and " + yetAnotherPoint + " are the same.") } else { println(point + " and " + yetAnotherPoint + " are different.") -} -// Point(1,2) and Point(2,2) are different. +} // Point(1,2) and Point(2,2) are different. ``` There is a lot more to case classes that we'd like to introduce, and we are convinced you will fall in love with them! We will cover them in depth [later](case-classes.html). diff --git a/_tour/named-arguments.md b/_tour/named-arguments.md index b4ce3f8539..2aa8159adc 100644 --- a/_tour/named-arguments.md +++ b/_tour/named-arguments.md @@ -27,12 +27,12 @@ printName(last = "Smith", first = "John") // Prints "John Smith" ``` Notice how the order of named arguments can be rearranged. However, if some arguments are named and others are not, the unnamed arguments must come first and in the order of their parameters in the method signature. -``` +```tut:nofail def printName(first: String, last: String): Unit = { println(first + " " + last) } -printName(last = "Smith", "john") // Does not compile +printName(last = "Smith", "john") // error: positional after named argument ``` Note that named arguments do not work with calls to Java methods. diff --git a/_tour/upper-type-bounds.md b/_tour/upper-type-bounds.md index 589d29e132..5ab82eca74 100644 --- a/_tour/upper-type-bounds.md +++ b/_tour/upper-type-bounds.md @@ -41,8 +41,10 @@ class PetContainer[P <: Pet](p: P) { val dogContainer = new PetContainer[Dog](new Dog) val catContainer = new PetContainer[Cat](new Cat) -// val lionContainer = new PetContainer[Lion](new Lion) -// ^this would not compile +``` + +```tut:fail +val lionContainer = new PetContainer[Lion](new Lion) // this would not compile ``` The `class PetContainer` take a type parameter `P` which must be a subtype of `Pet`. `Dog` and `Cat` are subtypes of `Pet` so we can create a new `PetContainer[Dog]` and `PetContainer[Cat]`. However, if we tried to create a `PetContainer[Lion]`, we would get the following Error: diff --git a/_zh-cn/tour/basics.md b/_zh-cn/tour/basics.md index b138b38fb0..f13481adf7 100644 --- a/_zh-cn/tour/basics.md +++ b/_zh-cn/tour/basics.md @@ -225,15 +225,13 @@ if (point == anotherPoint) { println(point + " and " + anotherPoint + " are the same.") } else { println(point + " and " + anotherPoint + " are different.") -} -// Point(1,2) and Point(1,2) are the same. +} // Point(1,2) and Point(1,2) are the same. if (point == yetAnotherPoint) { println(point + " and " + yetAnotherPoint + " are the same.") } else { println(point + " and " + yetAnotherPoint + " are different.") -} -// Point(1,2) and Point(2,2) are different. +} // Point(1,2) and Point(2,2) are different. ``` 关于样例类,还有不少内容我们乐于介绍,并且我们确信你会爱上它们。我们会在[后面](case-classes.html)深入介绍它们。 diff --git a/scripts/run-tut.sh b/scripts/run-tut.sh index c8094071bd..51a87057da 100755 --- a/scripts/run-tut.sh +++ b/scripts/run-tut.sh @@ -2,8 +2,6 @@ set -eux -COURSIER_CLASSPATH="$(coursier fetch -p org.scala-lang.modules::scala-xml:1.0.3)" - -coursier launch -r "https://dl.bintray.com/tpolecat/maven/" org.tpolecat:tut-core_2.11:0.4.4 -- . tut-tmp '.*\.md$' -classpath "$COURSIER_CLASSPATH" -Xfatal-warnings -feature +coursier launch -r "https://dl.bintray.com/tpolecat/maven/" org.tpolecat:tut-core_2.12:0.6.7 -- . tut-tmp '.*\.md$' -classpath $(coursier fetch -p com.chuusai:shapeless_2.12:2.3.3) -Xfatal-warnings -feature exit 0 From 5946edc0335100a3f9185c56ab05233b906dc958 Mon Sep 17 00:00:00 2001 From: Philippus Date: Wed, 15 Aug 2018 16:37:17 +0200 Subject: [PATCH 2/2] Replace `tut:nofail` blocks with `tut:fail` single-line statements see https://github.com/tpolecat/tut/issues/160 --- _ba/tour/basics.md | 3 +-- _tour/basics.md | 3 +-- _tour/named-arguments.md | 6 +----- _zh-cn/tour/basics.md | 3 +-- 4 files changed, 4 insertions(+), 11 deletions(-) diff --git a/_ba/tour/basics.md b/_ba/tour/basics.md index 65830e2ced..ae47a88d38 100644 --- a/_ba/tour/basics.md +++ b/_ba/tour/basics.md @@ -54,8 +54,7 @@ Referenciranje vrijednosti ne okida njeno ponovno izračunavanje. Vrijednosti se ne mogu mijenjati. -```tut:nofail -val x = 1 + 1 +```tut:fail x = 3 // Ovo se ne kompajlira. ``` diff --git a/_tour/basics.md b/_tour/basics.md index 2b4d364e57..c9a0486c27 100644 --- a/_tour/basics.md +++ b/_tour/basics.md @@ -59,8 +59,7 @@ a value does not re-compute it. Values cannot be re-assigned. -```tut:nofail -val x = 1 + 1 +```tut:fail x = 3 // This does not compile. ``` diff --git a/_tour/named-arguments.md b/_tour/named-arguments.md index 2aa8159adc..d8d1d7a88e 100644 --- a/_tour/named-arguments.md +++ b/_tour/named-arguments.md @@ -27,11 +27,7 @@ printName(last = "Smith", first = "John") // Prints "John Smith" ``` Notice how the order of named arguments can be rearranged. However, if some arguments are named and others are not, the unnamed arguments must come first and in the order of their parameters in the method signature. -```tut:nofail -def printName(first: String, last: String): Unit = { - println(first + " " + last) -} - +```tut:fail printName(last = "Smith", "john") // error: positional after named argument ``` diff --git a/_zh-cn/tour/basics.md b/_zh-cn/tour/basics.md index f13481adf7..dc6a06b0be 100644 --- a/_zh-cn/tour/basics.md +++ b/_zh-cn/tour/basics.md @@ -59,8 +59,7 @@ println(x) // 2 常量(`values`)不能重新被赋值。 -```tut:nofail -val x = 1 + 1 +```tut:fail x = 3 // This does not compile. ```