From d527b975afe85d1b67870074da165e733293768c Mon Sep 17 00:00:00 2001 From: fResult Date: Mon, 3 Jun 2024 00:37:36 +0700 Subject: [PATCH 1/3] add `/th/tour/named-arguments` page --- _th/tour/named-arguments.md | 52 +++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/_th/tour/named-arguments.md b/_th/tour/named-arguments.md index 161b53b50e..1def9a6705 100644 --- a/_th/tour/named-arguments.md +++ b/_th/tour/named-arguments.md @@ -10,3 +10,55 @@ language: th next-page: packages-and-imports previous-page: default-parameter-values --- + +เมื่อเราเรียกใช้ method แล้วเราสามารถระบุชื่อ argument (label the argument) สำหรับ parameter ใดๆ ได้ดังนี้: + +{% tabs named-arguments-when-good %} + +{% tab 'Scala 2 and 3' for=named-arguments-when-good %} + +```scala mdoc +def printName(first: String, last: String): Unit = + println(s"$first $last") + +printName("John", "Public") // แสดงค่า "John Public" +printName(first = "John", last = "Public") // แสดงค่า "John Public" +printName(last = "Public", first = "John") // แสดงค่า "John Public" +printName("Elton", last = "John") // แสดงค่า "Elton John" +``` + +{% endtab %} + +{% endtabs %} + +named argument นั้นมีประโยชน์เมื่อ parameter 2 ตัวมี type เดียวกัน\ +ทำให้ argument ที่เราส่งไปให้ function อาจถูกสลับกันโดยไม่ได้ตั้งใจ + +สังเกตว่าเราจะเขียน argument ที่ระบุชื่อในลำดับใดก็ได้\ +แต่ถ้า argument ไม่ได้อยู่ในลำดับของ parameter ใน function จากซ้ายไปขวา แล้ว argument ที่เหลือจะต้องระบุชื่อทั้งหมด + +ในตัวอย่างข้างล่างนี้ named argument ทำให้เราสามารถเว้น parameter `middle` ได้\ +แต่ในกรณีที่เกิด error เนื่องจาก argument ตัวแรกอยู่นอกลำดับของ parameter (ตัวแรกไม่ใช่ parameter `first`)\ +ดังนั้น เราจะต้องระบุชื่อ argument ตั้งแต่ตัวที่ 2 เป็นต้นไป + +{% tabs named-arguments-when-error %} + +{% tab 'Scala 2 and 3' for=named-arguments-when-error %} + +```scala mdoc:fail +def printFullName(first: String, middle: String = "Q.", last: String): Unit = + println(s"$first $middle $last") + +printFullName(first = "John", last = "Public") // แสดงค่า "John Q. Public" +printFullName("John", last = "Public") // แสดงค่า "John Q. Public" +printFullName("John", middle = "Quincy", "Public") // แสดงค่า "John Quincy Public" +printFullName(last = "Public", first = "John") // แสดงค่า "John Q. Public" +printFullName(last = "Public", "John") // error: positional after named argument +``` + +{% endtab %} + +{% endtabs %} + +เราสามารถใช้ Named Argument กับการเรียกใช้ method ของ Java ได้\ +แต่ทำได้เฉพาะในกรณีที่ Java library นั้นถูกคอมไพล์ด้วยออพชั่น `-parameters` เท่านั้น From f0ecf82b33d2a5df485a9fd379ae9fd25b2f88e0 Mon Sep 17 00:00:00 2001 From: fResult Date: Tue, 4 Jun 2024 00:53:52 +0700 Subject: [PATCH 2/3] update named-arguments --- Gemfile.lock | 3 +++ _th/tour/named-arguments.md | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Gemfile.lock b/Gemfile.lock index 06dccdc0b6..f425195841 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -233,6 +233,8 @@ GEM mutex_m (0.2.0) net-http (0.4.1) uri + nokogiri (1.16.5-aarch64-linux) + racc (~> 1.4) nokogiri (1.16.5-arm64-darwin) racc (~> 1.4) nokogiri (1.16.5-x64-mingw-ucrt) @@ -285,6 +287,7 @@ GEM zeitwerk (2.6.7) PLATFORMS + aarch64-linux arm64-darwin-22 arm64-darwin-23 x64-mingw-ucrt diff --git a/_th/tour/named-arguments.md b/_th/tour/named-arguments.md index 1def9a6705..0257732c5d 100644 --- a/_th/tour/named-arguments.md +++ b/_th/tour/named-arguments.md @@ -38,7 +38,8 @@ named argument นั้นมีประโยชน์เมื่อ paramet แต่ถ้า argument ไม่ได้อยู่ในลำดับของ parameter ใน function จากซ้ายไปขวา แล้ว argument ที่เหลือจะต้องระบุชื่อทั้งหมด ในตัวอย่างข้างล่างนี้ named argument ทำให้เราสามารถเว้น parameter `middle` ได้\ -แต่ในกรณีที่เกิด error เนื่องจาก argument ตัวแรกอยู่นอกลำดับของ parameter (ตัวแรกไม่ใช่ parameter `first`)\ +แต่ในกรณีที่เกิด `error: positional after named argument`\ +เนื่องจาก argument ตัวแรกไม่ได้เรียงตามลำดับของ parameter (ตัวแรกไม่ใช่ parameter `first` และ argument ตัวที่ 2 เป็นต้นไปก็ไม่ได้ระบุชื่อด้วย)\ ดังนั้น เราจะต้องระบุชื่อ argument ตั้งแต่ตัวที่ 2 เป็นต้นไป {% tabs named-arguments-when-error %} From cd18be7c6dbfe59d79da8d24cfe9ed65b162075b Mon Sep 17 00:00:00 2001 From: Seth Tisue Date: Sat, 26 Oct 2024 12:59:05 -0700 Subject: [PATCH 3/3] leave ruby stuff alone for now --- Gemfile.lock | 3 --- 1 file changed, 3 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index f425195841..06dccdc0b6 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -233,8 +233,6 @@ GEM mutex_m (0.2.0) net-http (0.4.1) uri - nokogiri (1.16.5-aarch64-linux) - racc (~> 1.4) nokogiri (1.16.5-arm64-darwin) racc (~> 1.4) nokogiri (1.16.5-x64-mingw-ucrt) @@ -287,7 +285,6 @@ GEM zeitwerk (2.6.7) PLATFORMS - aarch64-linux arm64-darwin-22 arm64-darwin-23 x64-mingw-ucrt