From 4c2743827544c9373fde5e4d34e5196ae5f5f85c Mon Sep 17 00:00:00 2001 From: Shogo Wada Date: Wed, 1 Feb 2017 02:46:41 -0600 Subject: [PATCH 01/16] Update unified-types.md Make it more intuitive to new comers. - It removes use of singleton object to better focus on the topic (unified type). - It removes unnecessary and redundant information. - It uses REPL instead of complete Scala application. - I am still not sure if this is a good idea, but I thought people reading this tutorials is not looking to write a complete Scala application yet; they'd rather want to quickly get familiar with the language, for which I think use of REPL suits better. --- tutorials/tour/unified-types.md | 50 ++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/tutorials/tour/unified-types.md b/tutorials/tour/unified-types.md index 223aaceb00..a3df9f8575 100644 --- a/tutorials/tour/unified-types.md +++ b/tutorials/tour/unified-types.md @@ -16,33 +16,43 @@ In contrast to Java, all values in Scala are objects (including numerical values ## Scala Class Hierarchy ## -The superclass of all classes `scala.Any` has two direct subclasses `scala.AnyVal` and `scala.AnyRef` representing two different class worlds: value classes and reference classes. All value classes are predefined; they correspond to the primitive types of Java-like languages. All other classes define reference types. User-defined classes define reference types by default; i.e. they always (indirectly) subclass `scala.AnyRef`. Every user-defined class in Scala implicitly extends the trait `scala.ScalaObject`. Classes from the infrastructure on which Scala is running (e.g. the Java runtime environment) do not extend `scala.ScalaObject`. If Scala is used in the context of a Java runtime environment, then `scala.AnyRef` corresponds to `java.lang.Object`. -Please note that the diagram above also shows implicit conversions between the value classes. -Here is an example that demonstrates that both numbers, characters, boolean values, and functions are objects just like every other object: - +The superclass of all classes `scala.Any` has two direct subclasses: `scala.AnyVal` and `scala.AnyRef`. + +`scala.AnyVal` represents value classes. All value classes are predefined; they correspond to the primitive types of Java-like languages. Note that the diagram above also shows implicit conversions between the value classes. + +`scala.AnyRef` represents reference classes. All non-value classes are defined as reference class. Every user-defined class in Scala implicitly extends `scala.AnyRef`. If Scala is used in the context of a Java runtime environment, then `scala.AnyRef` corresponds to `java.lang.Object`. + +Here is an example that demonstrates that numbers, characters, boolean values, and functions are all objects just like every other object: + ```tut -object UnifiedTypes extends App { - val set = new scala.collection.mutable.LinkedHashSet[Any] - set += "This is a string" // add a string - set += 732 // add a number - set += 'c' // add a character - set += true // add a boolean value - set += main _ // add the main function - val iter: Iterator[Any] = set.iterator - while (iter.hasNext) { - println(iter.next.toString()) - } -} -``` +scala> var list: List[Any] = List() +list: List[Any] = List() + +scala> list = list :+ "This is a string" +list: List[Any] = List(This is a string) -The program declares an application `UnifiedTypes` in form of a top-level [singleton object](singleton-objects.html) extending `App`. The application defines a local variable `set` which refers to an instance of class `LinkedHashSet[Any]`. The program adds various elements to this set. The elements have to conform to the declared set element type `Any`. In the end, string representations of all elements are printed out. +scala> list = list :+ 732 +list: List[Any] = List(This is a string, 732) -Here is the output of the program: +scala> list = list :+ 'c' +list: List[Any] = List(This is a string, 732, c) +scala> list = list :+ true +list: List[Any] = List(This is a string, 732, c, true) + +scala> list = list :+ (() => "This is an anonymous function returning a string") +list: List[Any] = List(This is a string, 732, c, true, $$Lambda$1095/599060649@108a7fff) ``` + +The program defines a variable `list` which refers to an instance of class `List[Any]`. The program adds elements of various types to this list. + +When you print each element, it will output something like the following: + +```tut +scala> list.foreach(element => println(element)) This is a string 732 c true - +$line15.$read$$iw$$iw$$$Lambda$1095/599060649@108a7fff ``` From f38d7ae3890c3a9be8c7265a448c5a0179bf3241 Mon Sep 17 00:00:00 2001 From: Shogo Wada Date: Wed, 1 Feb 2017 02:55:25 -0600 Subject: [PATCH 02/16] Update unified-types.md --- tutorials/tour/unified-types.md | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/tutorials/tour/unified-types.md b/tutorials/tour/unified-types.md index a3df9f8575..679fa1a761 100644 --- a/tutorials/tour/unified-types.md +++ b/tutorials/tour/unified-types.md @@ -25,31 +25,22 @@ The superclass of all classes `scala.Any` has two direct subclasses: `scala.AnyV Here is an example that demonstrates that numbers, characters, boolean values, and functions are all objects just like every other object: ```tut -scala> var list: List[Any] = List() -list: List[Any] = List() +object UnifiedTypes extends App { + var list: List[Any] = List() -scala> list = list :+ "This is a string" -list: List[Any] = List(This is a string) + list = list :+ "This is a string" + list = list :+ 732 + list = list :+ 'c' + list = list :+ true + list = list :+ (() => "This is an anonymous function returning a string") -scala> list = list :+ 732 -list: List[Any] = List(This is a string, 732) - -scala> list = list :+ 'c' -list: List[Any] = List(This is a string, 732, c) - -scala> list = list :+ true -list: List[Any] = List(This is a string, 732, c, true) - -scala> list = list :+ (() => "This is an anonymous function returning a string") -list: List[Any] = List(This is a string, 732, c, true, $$Lambda$1095/599060649@108a7fff) + list.foreach(element => println(element)) +} ``` -The program defines a variable `list` which refers to an instance of class `List[Any]`. The program adds elements of various types to this list. - -When you print each element, it will output something like the following: +The program defines a variable `list` which refers to an instance of class `List[Any]`. The program adds elements of various types to this list. In the end, the program outputs something like below: ```tut -scala> list.foreach(element => println(element)) This is a string 732 c From c18fe3bed322602feddb11c95777cf9465bcf307 Mon Sep 17 00:00:00 2001 From: Shogo Wada Date: Wed, 1 Feb 2017 02:57:13 -0600 Subject: [PATCH 03/16] Update unified-types.md --- tutorials/tour/unified-types.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tutorials/tour/unified-types.md b/tutorials/tour/unified-types.md index 679fa1a761..2157d93f66 100644 --- a/tutorials/tour/unified-types.md +++ b/tutorials/tour/unified-types.md @@ -26,13 +26,13 @@ Here is an example that demonstrates that numbers, characters, boolean values, a ```tut object UnifiedTypes extends App { - var list: List[Any] = List() - - list = list :+ "This is a string" - list = list :+ 732 - list = list :+ 'c' - list = list :+ true - list = list :+ (() => "This is an anonymous function returning a string") + val list: List[Any] = List( + "This is a string", + 732, + 'c', + true, + () => "This is an anonymous function returning a string" + ) list.foreach(element => println(element)) } From 6ddf3f7c2e6dd4adceaab48158f194372ad985d8 Mon Sep 17 00:00:00 2001 From: Shogo Wada Date: Wed, 1 Feb 2017 02:58:52 -0600 Subject: [PATCH 04/16] Update unified-types.md --- tutorials/tour/unified-types.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tutorials/tour/unified-types.md b/tutorials/tour/unified-types.md index 2157d93f66..bf5cadc554 100644 --- a/tutorials/tour/unified-types.md +++ b/tutorials/tour/unified-types.md @@ -38,7 +38,7 @@ object UnifiedTypes extends App { } ``` -The program defines a variable `list` which refers to an instance of class `List[Any]`. The program adds elements of various types to this list. In the end, the program outputs something like below: +The application defines a variable `list` which refers to an instance of class `List[Any]`. The list is initialized with elements of various types. In the end, the application outputs something like below: ```tut This is a string From 1548220055baf060a78b677ba6ede4e0819d6da8 Mon Sep 17 00:00:00 2001 From: Shogo Wada Date: Wed, 1 Feb 2017 03:00:48 -0600 Subject: [PATCH 05/16] Update unified-types.md --- tutorials/tour/unified-types.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tutorials/tour/unified-types.md b/tutorials/tour/unified-types.md index bf5cadc554..92f18a1327 100644 --- a/tutorials/tour/unified-types.md +++ b/tutorials/tour/unified-types.md @@ -22,16 +22,16 @@ The superclass of all classes `scala.Any` has two direct subclasses: `scala.AnyV `scala.AnyRef` represents reference classes. All non-value classes are defined as reference class. Every user-defined class in Scala implicitly extends `scala.AnyRef`. If Scala is used in the context of a Java runtime environment, then `scala.AnyRef` corresponds to `java.lang.Object`. -Here is an example that demonstrates that numbers, characters, boolean values, and functions are all objects just like every other object: +Here is an example that demonstrates that strings, integers, characters, boolean values, and functions are all objects just like every other object: ```tut object UnifiedTypes extends App { val list: List[Any] = List( - "This is a string", - 732, - 'c', - true, - () => "This is an anonymous function returning a string" + "a string", + 732, // an integer + 'c', // a character + true, // a boolean value + () => "an anonymous function returning a string" ) list.foreach(element => println(element)) From 183f7d070ef59d43f46873b650a9f4ca404da377 Mon Sep 17 00:00:00 2001 From: Shogo Wada Date: Wed, 1 Feb 2017 03:01:36 -0600 Subject: [PATCH 06/16] Update unified-types.md --- tutorials/tour/unified-types.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tutorials/tour/unified-types.md b/tutorials/tour/unified-types.md index 92f18a1327..b832c19c53 100644 --- a/tutorials/tour/unified-types.md +++ b/tutorials/tour/unified-types.md @@ -41,7 +41,7 @@ object UnifiedTypes extends App { The application defines a variable `list` which refers to an instance of class `List[Any]`. The list is initialized with elements of various types. In the end, the application outputs something like below: ```tut -This is a string +a string 732 c true From 4f9f0bb1e875069766c3fdf6a3fe99e02e90b326 Mon Sep 17 00:00:00 2001 From: Shogo Wada Date: Wed, 1 Feb 2017 03:07:44 -0600 Subject: [PATCH 07/16] Update unified-types.md --- tutorials/tour/unified-types.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tutorials/tour/unified-types.md b/tutorials/tour/unified-types.md index b832c19c53..82f951a78f 100644 --- a/tutorials/tour/unified-types.md +++ b/tutorials/tour/unified-types.md @@ -18,7 +18,7 @@ In contrast to Java, all values in Scala are objects (including numerical values The superclass of all classes `scala.Any` has two direct subclasses: `scala.AnyVal` and `scala.AnyRef`. -`scala.AnyVal` represents value classes. All value classes are predefined; they correspond to the primitive types of Java-like languages. Note that the diagram above also shows implicit conversions between the value classes. +`scala.AnyVal` represents value classes. All value classes are not nullable and predefined; they correspond to the primitive types of Java-like languages. Note that the diagram above also shows implicit conversions between the value classes. `scala.AnyRef` represents reference classes. All non-value classes are defined as reference class. Every user-defined class in Scala implicitly extends `scala.AnyRef`. If Scala is used in the context of a Java runtime environment, then `scala.AnyRef` corresponds to `java.lang.Object`. @@ -38,7 +38,7 @@ object UnifiedTypes extends App { } ``` -The application defines a variable `list` which refers to an instance of class `List[Any]`. The list is initialized with elements of various types. In the end, the application outputs something like below: +The application defines a variable `list` of type `List[Any]`. The list is initialized with elements of various types, but they all are instance of `scala.Any`. In the end, the application outputs something like below: ```tut a string From c1f59207900072f0124822b8ae99a2d2d293515b Mon Sep 17 00:00:00 2001 From: Shogo Wada Date: Wed, 1 Feb 2017 03:08:30 -0600 Subject: [PATCH 08/16] Update unified-types.md --- tutorials/tour/unified-types.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tutorials/tour/unified-types.md b/tutorials/tour/unified-types.md index 82f951a78f..89c772ea0c 100644 --- a/tutorials/tour/unified-types.md +++ b/tutorials/tour/unified-types.md @@ -20,7 +20,7 @@ The superclass of all classes `scala.Any` has two direct subclasses: `scala.AnyV `scala.AnyVal` represents value classes. All value classes are not nullable and predefined; they correspond to the primitive types of Java-like languages. Note that the diagram above also shows implicit conversions between the value classes. -`scala.AnyRef` represents reference classes. All non-value classes are defined as reference class. Every user-defined class in Scala implicitly extends `scala.AnyRef`. If Scala is used in the context of a Java runtime environment, then `scala.AnyRef` corresponds to `java.lang.Object`. +`scala.AnyRef` represents reference classes. All non-value classes are defined as reference class. Every user-defined class in Scala implicitly extends `scala.AnyRef`. If Scala is used in the context of a Java runtime environment, `scala.AnyRef` corresponds to `java.lang.Object`. Here is an example that demonstrates that strings, integers, characters, boolean values, and functions are all objects just like every other object: From 3fd83519da64d304c8200e9fccc8641b480c10c0 Mon Sep 17 00:00:00 2001 From: Shogo Wada Date: Wed, 1 Feb 2017 03:12:34 -0600 Subject: [PATCH 09/16] Update unified-types.md --- tutorials/tour/unified-types.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tutorials/tour/unified-types.md b/tutorials/tour/unified-types.md index 89c772ea0c..ebaf71ecce 100644 --- a/tutorials/tour/unified-types.md +++ b/tutorials/tour/unified-types.md @@ -10,7 +10,7 @@ next-page: classes previous-page: tour-of-scala --- -In contrast to Java, all values in Scala are objects (including numerical values and functions). Since Scala is class-based, all values are instances of a class. The diagram below illustrates the class hierarchy. +In Scala, all values are instance of a class, including numerical values and functions. The diagram below illustrates the class hierarchy. ![Scala Type Hierarchy]({{ site.baseurl }}/resources/images/classhierarchy.img_assist_custom.png) From 3ac5632ce566ee69c72337df971dd7c14105003d Mon Sep 17 00:00:00 2001 From: Shogo Wada Date: Wed, 1 Feb 2017 03:26:13 -0600 Subject: [PATCH 10/16] Update unified-types.md --- tutorials/tour/unified-types.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tutorials/tour/unified-types.md b/tutorials/tour/unified-types.md index ebaf71ecce..4833fd7812 100644 --- a/tutorials/tour/unified-types.md +++ b/tutorials/tour/unified-types.md @@ -38,7 +38,9 @@ object UnifiedTypes extends App { } ``` -The application defines a variable `list` of type `List[Any]`. The list is initialized with elements of various types, but they all are instance of `scala.Any`. In the end, the application outputs something like below: +The program declares an application `UnifiedTypes` in form of a top-level [singleton object](singleton-objects.html) extending [`App`](http://www.scala-lang.org/api/2.12.x/scala/App.html) so that the body of it acts as a main function. + +The application defines a variable `list` of type `List[Any]`. The list is initialized with elements of various types, and they all are instance of `scala.Any`. In the end, the application outputs something like below: ```tut a string From 47f1b41ca306ffcc546d6fd612afa9cbf5f02ec7 Mon Sep 17 00:00:00 2001 From: Shogo Wada Date: Wed, 1 Feb 2017 03:41:16 -0600 Subject: [PATCH 11/16] Update unified-types.md --- tutorials/tour/unified-types.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tutorials/tour/unified-types.md b/tutorials/tour/unified-types.md index 4833fd7812..c63c92185d 100644 --- a/tutorials/tour/unified-types.md +++ b/tutorials/tour/unified-types.md @@ -47,5 +47,5 @@ a string 732 c true -$line15.$read$$iw$$iw$$$Lambda$1095/599060649@108a7fff + ``` From 4f65cd358693c89b5823cca0987ef4ec4c53ca15 Mon Sep 17 00:00:00 2001 From: Shogo Wada Date: Wed, 1 Feb 2017 03:49:03 -0600 Subject: [PATCH 12/16] Update unified-types.md --- tutorials/tour/unified-types.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tutorials/tour/unified-types.md b/tutorials/tour/unified-types.md index c63c92185d..2ad14450b5 100644 --- a/tutorials/tour/unified-types.md +++ b/tutorials/tour/unified-types.md @@ -40,7 +40,9 @@ object UnifiedTypes extends App { The program declares an application `UnifiedTypes` in form of a top-level [singleton object](singleton-objects.html) extending [`App`](http://www.scala-lang.org/api/2.12.x/scala/App.html) so that the body of it acts as a main function. -The application defines a variable `list` of type `List[Any]`. The list is initialized with elements of various types, and they all are instance of `scala.Any`. In the end, the application outputs something like below: +The application defines a variable `list` of type `List[Any]`. The list is initialized with elements of various types, but they all are instance of `scala.Any`, so you can add them to the list. + +In the end, the application outputs something like below: ```tut a string From 049e09444e6ca074e2e1bd18282cb9e3b765f2d4 Mon Sep 17 00:00:00 2001 From: Shogo Wada Date: Wed, 1 Feb 2017 04:12:05 -0600 Subject: [PATCH 13/16] Update unified-types.md --- tutorials/tour/unified-types.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tutorials/tour/unified-types.md b/tutorials/tour/unified-types.md index 2ad14450b5..5d6e93bb48 100644 --- a/tutorials/tour/unified-types.md +++ b/tutorials/tour/unified-types.md @@ -42,7 +42,7 @@ The program declares an application `UnifiedTypes` in form of a top-level [singl The application defines a variable `list` of type `List[Any]`. The list is initialized with elements of various types, but they all are instance of `scala.Any`, so you can add them to the list. -In the end, the application outputs something like below: +Here is the output of the program: ```tut a string From ef5493dd8085062471c32899c1562fd929ea334c Mon Sep 17 00:00:00 2001 From: Shogo Wada Date: Wed, 1 Feb 2017 19:05:17 -0600 Subject: [PATCH 14/16] Update unified-types.md --- tutorials/tour/unified-types.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tutorials/tour/unified-types.md b/tutorials/tour/unified-types.md index 5d6e93bb48..0d72b2550a 100644 --- a/tutorials/tour/unified-types.md +++ b/tutorials/tour/unified-types.md @@ -10,7 +10,7 @@ next-page: classes previous-page: tour-of-scala --- -In Scala, all values are instance of a class, including numerical values and functions. The diagram below illustrates the class hierarchy. +In Scala, all values are instances of a class, including numerical values and functions. The diagram below illustrates the class hierarchy. ![Scala Type Hierarchy]({{ site.baseurl }}/resources/images/classhierarchy.img_assist_custom.png) @@ -18,7 +18,7 @@ In Scala, all values are instance of a class, including numerical values and fun The superclass of all classes `scala.Any` has two direct subclasses: `scala.AnyVal` and `scala.AnyRef`. -`scala.AnyVal` represents value classes. All value classes are not nullable and predefined; they correspond to the primitive types of Java-like languages. Note that the diagram above also shows implicit conversions between the value classes. +`scala.AnyVal` represents value classes. All value classes are non-nullable and predefined; they correspond to the primitive types of Java-like languages. Note that the diagram above also shows implicit conversions between the value classes. `scala.AnyRef` represents reference classes. All non-value classes are defined as reference class. Every user-defined class in Scala implicitly extends `scala.AnyRef`. If Scala is used in the context of a Java runtime environment, `scala.AnyRef` corresponds to `java.lang.Object`. @@ -44,7 +44,7 @@ The application defines a variable `list` of type `List[Any]`. The list is initi Here is the output of the program: -```tut +``` a string 732 c From 9b684e6fa5f713ee439c7669ec26f753a1c96b98 Mon Sep 17 00:00:00 2001 From: Shogo Wada Date: Tue, 7 Feb 2017 05:52:41 -0600 Subject: [PATCH 15/16] Remove use of "object" and "App" Now that Basics page has a link to ScalaFiddle, so I thought it's okay as long as it's runnable on the ScalaFiddle. Concepts like "App" is not related to the topic of this page. --- tutorials/tour/unified-types.md | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/tutorials/tour/unified-types.md b/tutorials/tour/unified-types.md index 0d72b2550a..591d3ed505 100644 --- a/tutorials/tour/unified-types.md +++ b/tutorials/tour/unified-types.md @@ -25,20 +25,16 @@ The superclass of all classes `scala.Any` has two direct subclasses: `scala.AnyV Here is an example that demonstrates that strings, integers, characters, boolean values, and functions are all objects just like every other object: ```tut -object UnifiedTypes extends App { - val list: List[Any] = List( - "a string", - 732, // an integer - 'c', // a character - true, // a boolean value - () => "an anonymous function returning a string" - ) - - list.foreach(element => println(element)) -} -``` - -The program declares an application `UnifiedTypes` in form of a top-level [singleton object](singleton-objects.html) extending [`App`](http://www.scala-lang.org/api/2.12.x/scala/App.html) so that the body of it acts as a main function. +val list: List[Any] = List( + "a string", + 732, // an integer + 'c', // a character + true, // a boolean value + () => "an anonymous function returning a string" +) + +list.foreach(element => println(element)) +```` The application defines a variable `list` of type `List[Any]`. The list is initialized with elements of various types, but they all are instance of `scala.Any`, so you can add them to the list. From cccedf57ebc61d682cb6a0186ffb0171bb844b47 Mon Sep 17 00:00:00 2001 From: Shogo Wada Date: Tue, 7 Feb 2017 05:53:36 -0600 Subject: [PATCH 16/16] Change "the application" to "it" It is no longer an application. --- tutorials/tour/unified-types.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tutorials/tour/unified-types.md b/tutorials/tour/unified-types.md index 591d3ed505..b2eb82f8c0 100644 --- a/tutorials/tour/unified-types.md +++ b/tutorials/tour/unified-types.md @@ -36,7 +36,7 @@ val list: List[Any] = List( list.foreach(element => println(element)) ```` -The application defines a variable `list` of type `List[Any]`. The list is initialized with elements of various types, but they all are instance of `scala.Any`, so you can add them to the list. +It defines a variable `list` of type `List[Any]`. The list is initialized with elements of various types, but they all are instance of `scala.Any`, so you can add them to the list. Here is the output of the program: