From 7bad78c4888468986f355cb9beba2b9c1cc8a63d Mon Sep 17 00:00:00 2001 From: Dave Copeland Date: Sun, 4 Dec 2011 17:18:03 -0500 Subject: [PATCH 1/4] Fixed spelling of RubyGems --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d1e5b9d514..b2a835d7d6 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ This repository contains the source for the Scala documentation website, as well ## Dependencies ## -Jekyll is required. Follow the install instructions at the Jekyll [wiki](https://github.com/mojombo/jekyll/wiki/Install). In most cases, you can install via RubyJems: +Jekyll is required. Follow the install instructions at the Jekyll [wiki](https://github.com/mojombo/jekyll/wiki/Install). In most cases, you can install via RubyGems: gem install jekyll From 6428bfeaaf6111202128b6d0ba99d80ef4b7b874 Mon Sep 17 00:00:00 2001 From: Dave Copeland Date: Sun, 4 Dec 2011 17:18:16 -0500 Subject: [PATCH 2/4] escape things that make liquid sad --- style/scaladoc.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/style/scaladoc.md b/style/scaladoc.md index 2174f478ee..a8c2545bd9 100644 --- a/style/scaladoc.md +++ b/style/scaladoc.md @@ -104,14 +104,14 @@ notation: * * ==Overview== * The main class to use is [[my.package.complex.Complex]], as so - * {{{ + * {{ "{{{" }} * scala> val complex = Complex(4,3) * complex: my.package.complex.Complex = 4 + 3i * }}} * * If you include [[my.package.complex.ComplexConversions]], you can * convert numbers more directly - * {{{ + * {{ "{{{" }} * scala> import my.package.complex.ComplexConversions._ * scala> val complex = 4 + 3.i * complex: my.package.complex.Complex = 4 + 3i @@ -181,7 +181,7 @@ ScalaDoc: /** Implicits conversions and helpers for [[mypackage.Complex]] instances. * - * {{{ + * {{ "{{{" }} * import ComplexImplicits._ * val c:Complex = 4 + 3.i * }}} From e1c82905e8b3371c5f847d4f9f06e178fcf99a6e Mon Sep 17 00:00:00 2001 From: Dave Copeland Date: Sun, 4 Dec 2011 17:37:16 -0500 Subject: [PATCH 3/4] Added tour pages for named parameters and default values --- tutorials/tour/abstract-types.md | 2 +- tutorials/tour/default_parameter_values.md | 65 ++++++++++++++++++++++ tutorials/tour/named_parameters.md | 35 ++++++++++++ 3 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 tutorials/tour/default_parameter_values.md create mode 100644 tutorials/tour/named_parameters.md diff --git a/tutorials/tour/abstract-types.md b/tutorials/tour/abstract-types.md index 820abb8196..4e512d433f 100644 --- a/tutorials/tour/abstract-types.md +++ b/tutorials/tour/abstract-types.md @@ -6,7 +6,7 @@ disqus: true tutorial: scala-tour num: 2 -outof: 33 +outof: 35 --- In Scala, classes are parameterized with values (the constructor parameters) and with types (if classes are [generic](generic-classes.html)). For reasons of regularity, it is not only possible to have values as object members; types along with values are members of objects. Furthermore, both forms of members can be concrete and abstract. diff --git a/tutorials/tour/default_parameter_values.md b/tutorials/tour/default_parameter_values.md new file mode 100644 index 0000000000..08a4be1733 --- /dev/null +++ b/tutorials/tour/default_parameter_values.md @@ -0,0 +1,65 @@ +--- +layout: tutorial +title: Default Parameter values + +disqus: true + +tutorial: scala-tour +num: 34 +--- + +Scala provides the ability to give parameters default values that can be used to allow a caller to omit those parameters. + +In Java, one tends to see a lot of overloaded methods that only serve to provide default values for certain parameters of a large method. This is especially true with constructors: + + public class HashMap { + public HashMap(Map m); + /** Create a new HashMap with default capacity (16) + * and loadFactor (0.75) + */ + public HashMap(); + /** Create a new HashMap with default loadFactor (0.75) */ + public HashMap(int initialCapacity); + public HashMap(int initialCapacity, float loadFactor); + } + +There's really only two constructors here; one that takes another map, and one that takes a capacity and load factor. The third and fourth constructors are there to allow users of HashMap to create instances with the probably-good-for-most-cases defaults of both load factor and capacity. + +More problematic is that the values used for defaults are in both the Javadoc *and* the code. Keeping this up to date is easily forgotten. A typical pattern around this would be to add public constants whose values will show up in the Javadoc: + + public class HashMap { + public static final int DEFAULT_CAPACITY = 16; + public static final float DEFAULT_LOAD_FACTOR = 0.75; + + public HashMap(Map m); + /** Create a new HashMap with default capacity (16) + * and loadFactor (0.75) + */ + public HashMap(); + /** Create a new HashMap with default loadFactor (0.75) */ + public HashMap(int initialCapacity); + public HashMap(int initialCapacity, float loadFactor); + } + +While this keeps us from repeating ourselves, it's less than expressive. + +Scala adds direct support for this: + + class HashMap[K,V](initialCapacity:Int = 16, loadFactor:Float = 0.75) { + } + + // Uses the defaults + val m1 = new HashMap[String,Int] + + // initialCapacity 20, default loadFactor + val m2= new HashMap[String,Int](20) + + // overriding both + val m3 = new HashMap[String,Int](20,0.8) + + // override only the loadFactory via + // named arguments + val m4 = new HashMap[String,Int](loadFactor = 0.8) + +Note how we can take advantage of *any* default value by using [named parameters](/tutorials/tour/named_parameters.html). + diff --git a/tutorials/tour/named_parameters.md b/tutorials/tour/named_parameters.md new file mode 100644 index 0000000000..884e165926 --- /dev/null +++ b/tutorials/tour/named_parameters.md @@ -0,0 +1,35 @@ +--- +layout: tutorial +title: Named Parameters + +disqus: true + +tutorial: scala-tour +num: 35 +--- + +When calling methods and functions, you can use the name of the variables expliclty in the call, like so: + + def printName(first:String, last:String) = { + println(first + " " + last) + } + + printName("John","Smith") + // Prints "John Smith" + printName(first = "John",last = "Smith") + // Prints "John Smith" + printName(last = "Smith",first = "John") + // Prints "John Smith" + +Note that once you are using parameter names in your calls, the order doesn't matter, so long as all parameters are named. This +feature works well with [default parameter values](/tutorials/tour/default_parameter_values.html): + + def printName(first:String = "John", last:String = "Smith") = { + println(first + " " + last) + } + + printName(last = "Jones") + // Prints "John Jones" + +Since you can place the parameters in any order you like, you can use the default value for parameters that come first in the +parameter list. From b722108d9aa8cabcf2260d3e1a4d6b812edb500a Mon Sep 17 00:00:00 2001 From: Dave Copeland Date: Sun, 4 Dec 2011 17:41:29 -0500 Subject: [PATCH 4/4] config for my local environment --- CNAME | 1 - _config.yml | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) delete mode 100644 CNAME diff --git a/CNAME b/CNAME deleted file mode 100644 index 4c54828903..0000000000 --- a/CNAME +++ /dev/null @@ -1 +0,0 @@ -docs.scala-lang.org \ No newline at end of file diff --git a/_config.yml b/_config.yml index 550a622b5d..5f30f6d68a 100644 --- a/_config.yml +++ b/_config.yml @@ -15,4 +15,4 @@ scala-version: 2.9.1 pygments: true permalink: /:categories/:title.html - +markdown: rdiscount