From f1b64fb6a67bf7fa5020a7effd1ace4edbb0a4b0 Mon Sep 17 00:00:00 2001 From: Micah Date: Sun, 25 Oct 2015 13:08:12 -0400 Subject: [scala/en] Added REPL basics to help people experiment as they learn. --- scala.html.markdown | 60 ++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 50 insertions(+), 10 deletions(-) (limited to 'scala.html.markdown') diff --git a/scala.html.markdown b/scala.html.markdown index 192e03d7..c1edbe6f 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -12,22 +12,62 @@ Scala - the scalable language ```scala +///////////////////////////////////////////////// +// 0. Basics +///////////////////////////////////////////////// /* - Set yourself up: + Setup Scala: 1) Download Scala - http://www.scala-lang.org/downloads 2) Unzip/untar to your favourite location and put the bin subdir in your `PATH` environment variable - 3) Start a Scala REPL by running `scala`. You should see the prompt: - - scala> - - This is the so called REPL (Read-Eval-Print Loop). You may type any Scala - expression, and the result will be printed. We will explain what Scala files - look like further into this tutorial, but for now, let's start with some - basics. - */ +/* + Try the REPL + + Scala has a tool called the REPL (Read-Eval-Print Loop) that is anologus to + commandline interpreters in many other languages. You may type any Scala + expression, and the result will be evaluated and printed. + + The REPL is a very handy tool to test and verify code. Use it as you read + this tutorial to quickly explore concepts on your own. +*/ + +// Start a Scala REPL by running `scala`. You should see the prompt: +$ scala +scala> + +// By default each expression you type is saved as a new numbered value +scala> 2 + 2 +res0: Int = 4 + +// Default values can be reused. Note the value type displayed in the result.. +scala> res0 + 2 +res1: Int = 6 + +// Scala is a strongly typed language. You can use the REPL to check the type +// without evaluating an expression. +scala> :type (true, 2.0) +(Boolean, Double) + +// REPL sessions can be saved +scala> :save /sites/repl-test.scala + +// Files can be loaded into the REPL +scala> :load /sites/repl-test.scala +Loading /sites/repl-test.scala... +res2: Int = 4 +res3: Int = 6 + +// You can search your recent history +scala> :h? +1 2 + 2 +2 res0 + 2 +3 :save /sites/repl-test.scala +4 :load /sites/repl-test.scala +5 :h? + +// Now that you know how to play, let's learn a little scala... ///////////////////////////////////////////////// // 1. Basics -- cgit v1.2.3 From 71e503325ec63ea16697ca74b8678f7148db7cc2 Mon Sep 17 00:00:00 2001 From: rajanand ilangovan Date: Sat, 6 Feb 2016 00:00:12 +0530 Subject: Removed ! --- scala.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'scala.html.markdown') diff --git a/scala.html.markdown b/scala.html.markdown index e82c1c36..745605ed 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -48,7 +48,7 @@ println(10) // Printing, without forcing a new line on next print print("Hello world") print(10) -// Hello world!10 +// Hello world10 // Declaring values is done using either var or val. // val declarations are immutable, whereas vars are mutable. Immutability is -- cgit v1.2.3 From 7895dcc6043d9229b593516ebf17cca08a515227 Mon Sep 17 00:00:00 2001 From: Felipe Martins Date: Tue, 21 Jun 2016 18:20:59 -0300 Subject: Add example of an int is divided by a double (#2284) --- scala.html.markdown | 1 + 1 file changed, 1 insertion(+) (limited to 'scala.html.markdown') diff --git a/scala.html.markdown b/scala.html.markdown index 745605ed..7f5f0ec3 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -88,6 +88,7 @@ true == false // false 6 / 2 // 3 6 / 4 // 1 6.0 / 4 // 1.5 +6 / 4.0 // 1.5 // Evaluating an expression in the REPL gives you the type and value of the result -- cgit v1.2.3 From 850aae6ed55c197982857055dbc03aa3cf20e1a3 Mon Sep 17 00:00:00 2001 From: Amru Eliwat Date: Sun, 30 Oct 2016 22:40:42 -0400 Subject: Fixed confusing typo --- scala.html.markdown | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'scala.html.markdown') diff --git a/scala.html.markdown b/scala.html.markdown index 5e3ece2d..f1a10a01 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -24,15 +24,15 @@ Scala - the scalable language /* Try the REPL - - Scala has a tool called the REPL (Read-Eval-Print Loop) that is anologus to + + Scala has a tool called the REPL (Read-Eval-Print Loop) that is anologus to commandline interpreters in many other languages. You may type any Scala expression, and the result will be evaluated and printed. - - The REPL is a very handy tool to test and verify code. Use it as you read + + The REPL is a very handy tool to test and verify code. Use it as you read this tutorial to quickly explore concepts on your own. */ - + // Start a Scala REPL by running `scala`. You should see the prompt: $ scala scala> @@ -50,7 +50,7 @@ res1: Int = 6 scala> :type (true, 2.0) (Boolean, Double) -// REPL sessions can be saved +// REPL sessions can be saved scala> :save /sites/repl-test.scala // Files can be loaded into the REPL @@ -59,7 +59,7 @@ Loading /sites/repl-test.scala... res2: Int = 4 res3: Int = 6 -// You can search your recent history +// You can search your recent history scala> :h? 1 2 + 2 2 res0 + 2 @@ -594,7 +594,7 @@ for { n <- s; nSquared = n * n if nSquared < 10} yield nSquared * best practices around them. We only include this section in the tutorial * because they are so commonplace in Scala libraries that it is impossible to * do anything meaningful without using a library that has implicits. This is - * meant for you to understand and work with implicts, not declare your own. + * meant for you to understand and work with implicits, not declare your own. */ // Any value (vals, functions, objects, etc) can be declared to be implicit by -- cgit v1.2.3 From 87cb771547cc26c3ea3f7d8c6c81832df9dae9b6 Mon Sep 17 00:00:00 2001 From: Amru Eliwat Date: Sun, 30 Oct 2016 22:54:18 -0400 Subject: Changed spelling of 'favourite' to use US english --- scala.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'scala.html.markdown') diff --git a/scala.html.markdown b/scala.html.markdown index f1a10a01..d33b6234 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -19,7 +19,7 @@ Scala - the scalable language Setup Scala: 1) Download Scala - http://www.scala-lang.org/downloads - 2) Unzip/untar to your favourite location and put the bin subdir in your `PATH` environment variable + 2) Unzip/untar to your favorite location and put the bin subdir in your `PATH` environment variable */ /* -- cgit v1.2.3 From 8d76182d52eca163268ef036b5b699fe25523550 Mon Sep 17 00:00:00 2001 From: Tommy Kelly Date: Fri, 9 Jun 2017 13:52:53 -0700 Subject: [scala/en] fix anologus typo --- scala.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'scala.html.markdown') diff --git a/scala.html.markdown b/scala.html.markdown index d33b6234..5eb94986 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -25,7 +25,7 @@ Scala - the scalable language /* Try the REPL - Scala has a tool called the REPL (Read-Eval-Print Loop) that is anologus to + Scala has a tool called the REPL (Read-Eval-Print Loop) that is analogous to commandline interpreters in many other languages. You may type any Scala expression, and the result will be evaluated and printed. -- cgit v1.2.3 From 84c35749189d0d9d9f5b72036dd47780c41bf7c7 Mon Sep 17 00:00:00 2001 From: Andy Date: Tue, 4 Jul 2017 20:21:45 +0200 Subject: [scala/en] Add traits in objects chapter (#2760) * [scala/en] Add traits in objects chapter * [scala/en] Traits: Describe traits keywords --- scala.html.markdown | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) (limited to 'scala.html.markdown') diff --git a/scala.html.markdown b/scala.html.markdown index 5eb94986..78053b40 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -460,8 +460,52 @@ val otherGeorge = george.copy(phoneNumber = "9876") // And many others. Case classes also get pattern matching for free, see below. +// Traits +// Similar to Java interfaces, traits define an object type and method +// signatures. Scala allows partial implementation of those methods. +// Constructor parameters are not allowed. Traits can inherit from other +// traits or classes without parameters. + +trait Dog { + def breed: String + def color: String + def bark: Boolean = true + def bite: Boolean +} +class SaintBernard extends Dog { + val breed = "Saint Bernard" + val color = "brown" + def bite = false +} + +scala> b +res0: SaintBernard = SaintBernard@3e57cd70 +scala> b.breed +res1: String = Saint Bernard +scala> b.bark +res2: Boolean = true +scala> b.bite +res3: Boolean = false + +// A trait can also be used as Mixin. The class "extends" the first trait, +// but the keyword "with" can add additional traits. + +trait Bark { + def bark: String = "Woof" +} +trait Dog { + def breed: String + def color: String +} +class SaintBernard extends Dog with Bark { + val breed = "Saint Bernard" + val color = "brown" +} -// Traits coming soon! +scala> val b = new SaintBernard +b: SaintBernard = SaintBernard@7b69c6ba +scala> b.bark +res0: String = Woof ///////////////////////////////////////////////// -- cgit v1.2.3