├── .gitignore ├── README.md ├── build.sbt ├── project └── build.properties ├── sbt ├── screenshots ├── class.png ├── object.png ├── operators-01.png └── operators-02.png └── src ├── main └── scala │ ├── Calculation.scala │ ├── Calculator.scala │ └── MyIntList.scala └── test └── scala ├── CalculatorSpec.scala └── MyIntListSpec.scala /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | tmp/ 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Scala quickstart 2 | 3 | This is a basic [SBT][sbt] project designed to help you start working 4 | effectively in a Scala REPL (read-eval-print loop) as quickly as possible. Once 5 | you've cloned the project, you can open the SBT console with the following 6 | command: 7 | 8 | ```bash 9 | ./sbt 10 | ``` 11 | 12 | Note that you'll need to have [Java][java] installed on your machine for this to 13 | work. Once you're in the SBT console you can type `console` to open a Scala 14 | REPL. You should see something like this: 15 | 16 | ```scala 17 | > console 18 | [info] Starting scala interpreter... 19 | [info] 20 | Welcome to Scala version 2.11.7 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_66). 21 | Type in expressions to have them evaluated. 22 | Type :help for more information. 23 | 24 | scala> 25 | ``` 26 | 27 | You can now type Scala definitions and expressions and they will be evaluated 28 | immediately. 29 | 30 | ## REPL tips 31 | 32 | Most of the time the REPL should just do what you'd expect it to do (try to 33 | evaluate code and print the result), but in some cases you may need or want to 34 | use special REPL-specific commands that it provides to accomplish certain 35 | things. 36 | 37 | ### Defining companions 38 | 39 | If you simply type the following definitions into the REPL as two separate 40 | lines, you'll get an error, since the case class and the object won't be treated 41 | as companions: 42 | 43 | ```scala 44 | case class Foo(i: Int) 45 | object Foo { def empty: Foo = Foo(0) } 46 | ``` 47 | 48 | In these situations you can use `:paste` to combine definitions into a single 49 | compilation unit: 50 | 51 | ```scala 52 | scala> :paste 53 | // Entering paste mode (ctrl-D to finish) 54 | 55 | case class Foo(i: Int) 56 | object Foo { def empty: Foo = Foo(0) } 57 | 58 | // Exiting paste mode, now interpreting. 59 | 60 | defined class Foo 61 | defined object Foo 62 | ``` 63 | 64 | ### Copying and pasting entire session entries 65 | 66 | Suppose you've got the following in a REPL session: 67 | 68 | ```scala 69 | scala> val doubled = "a" * 2 70 | doubled: String = aa 71 | 72 | scala> doubled.size 73 | res1: Int = 2 74 | ``` 75 | 76 | If you want to evaluate these definitions again (either in the same session or a 77 | new one), you can copy and paste the entire block of text, including the 78 | `scala>` prompts and the result output, and the REPL will recognize that that's 79 | what's going on: 80 | 81 | ```scala 82 | scala> scala> val doubled = "a" * 2 83 | 84 | // Detected repl transcript paste: ctrl-D to finish. 85 | 86 | doubled: String = aa 87 | 88 | scala> doubled.size 89 | res1: Int = 2 90 | 91 | // Replaying 2 commands from transcript. 92 | ``` 93 | 94 | Finding the type of an expression: 95 | 96 | You can use the `:type` command to make the REPL tell you the type of any 97 | expression, even if that expression doesn't evaluate to a value: 98 | 99 | ```scala 100 | scala> :type 1 / 0 101 | Int 102 | ``` 103 | 104 | ### Saving a REPL session to a file 105 | 106 | You can use `:save` to write a REPL session to a file in the current directory: 107 | 108 | ```scala 109 | scala> val doubled = "a" * 2 110 | doubled: String = aa 111 | 112 | scala> doubled.size 113 | res0: Int = 2 114 | 115 | scala> :save doubling.sc 116 | ``` 117 | 118 | `doubling.sc` will now contain the following: 119 | 120 | ```scala 121 | val doubled = "a" * 2 122 | doubled.size 123 | ``` 124 | 125 | Note that these definitions won't necessarily be a valid `.scala` file. I'm 126 | using the `.sc` extension here so that SBT won't try to parse the file as Scala, 127 | but you can use any other extension you wish. 128 | 129 | ### Loading Scala definitions into the REPL from a file 130 | 131 | You can also use `:load` to import definitions from a file into the REPL: 132 | 133 | ```scala 134 | scala> :load doubling.sc 135 | Loading doubling.sc... 136 | doubled: String = aa 137 | res0: Int = 2 138 | ``` 139 | 140 | ### Desugaring extension methods and other syntactic magic 141 | 142 | Thanks to the magic of extension methods and implicit conversions, it's often 143 | very difficult to determine why you can call certain methods on certain 144 | expressions and where these methods are coming from. We've been writing the 145 | following, for example: 146 | 147 | ```scala 148 | scala> val doubled = "a" * 2 149 | doubled: String = aa 150 | 151 | scala> doubled.size 152 | res0: Int = 2 153 | ``` 154 | 155 | But Scala's `String` is just an alias for `java.lang.String`, which doesn't have 156 | `*` or `size` methods. If we want to know where these are coming from, we can 157 | use Scala's reflection API in the REPL to find out: 158 | 159 | ```scala 160 | scala> import scala.reflect.runtime.universe.{ reify, showCode } 161 | import scala.reflect.runtime.universe.{reify, showCode} 162 | 163 | scala> showCode(reify("a" * 2).tree) 164 | res0: String = Predef.augmentString("a").*(2) 165 | ``` 166 | 167 | Now we can look up the `augmentString` method on `Predef` in the standard 168 | library's API docs, which will lead us to [`StringOps`][string-ops]. 169 | 170 | This approach also works for syntactic sugar like `for`-comprehensions. For 171 | example, it might not be clear why we can mix and match sequences and options in 172 | a `for`-comprehension: 173 | 174 | ```scala 175 | scala> showCode(reify(for { i <- 1 to 3; j <- Option(4) } yield i + j).tree) 176 | ``` 177 | 178 | This will print the following (which I've reformatted for clarity): 179 | 180 | ```scala 181 | Predef.intWrapper(1).to(3).flatMap( 182 | ((i) => Option.option2Iterable(Option.apply(4).map(((j) => i.+(j))))) 183 | )(IndexedSeq.canBuildFrom) 184 | ``` 185 | 186 | This takes some processing to understand, but at least everything is explicit. 187 | 188 | ### Viewing bytecode and JVM method signatures 189 | 190 | Many Scala language features (such as case classes) involve the generation of 191 | synthetic methods, and even user-defined methods often end up having signatures 192 | at the JVM level that you might not expect. It's possible to use `:javap` in the 193 | Scala REPL to inspect the signatures and bytecode generated by the Scala 194 | compiler for any classes in your project (or that you have defined in the REPL): 195 | 196 | ```scala 197 | scala> :javap -p IntOption 198 | Compiled from "MyIntList.scala" 199 | public interface IntOption { 200 | public abstract Z fold(Z, scala.Function1); 201 | } 202 | 203 | scala> :javap -p SomeInt 204 | Compiled from "MyIntList.scala" 205 | public final class SomeInt implements IntOption,scala.Product,scala.Serializable { 206 | private final int value; 207 | public static scala.Option unapply(SomeInt); 208 | public static SomeInt apply(int); 209 | public static scala.Function1 andThen(scala.Function1); 210 | public static scala.Function1 compose(scala.Function1); 211 | public Z fold(Z, scala.Function1); 212 | public int value(); 213 | public SomeInt copy(int); 214 | public int copy$default$1(); 215 | public java.lang.String productPrefix(); 216 | public int productArity(); 217 | public java.lang.Object productElement(int); 218 | public scala.collection.Iterator productIterator(); 219 | public boolean canEqual(java.lang.Object); 220 | public int hashCode(); 221 | public java.lang.String toString(); 222 | public boolean equals(java.lang.Object); 223 | public SomeInt(int); 224 | } 225 | ``` 226 | 227 | `:javap` has most of the same options as the `javap` command-line tool, and you 228 | `javap -help` is useful for understanding what `:javap` can do. 229 | 230 | ## Scaladoc tips 231 | 232 | The Scala standard library has [extensive API documentation][scala-scaladocs], 233 | but some useful features of the Scaladoc interface aren't immediately obvious. 234 | 235 | ### Browsing symbolic operators 236 | 237 | For example, suppose you see the symbol `/:` in some Scala code. You 238 | could try to figure out what type it's being called on and search for that type, 239 | but you can also browse symbolic operators like this directly in the Scaladoc 240 | interface. To do this, click the small `#` in the upper left corner of the 241 | screen. 242 | 243 | ![Operator list link](/screenshots/operators-01.png) 244 | 245 | Now you can search the page for `/:`: 246 | 247 | ![Operator list](/screenshots/operators-02.png) 248 | 249 | ### Instance and companion object definitions 250 | 251 | Another simple but useful feature (which I personally only discovered after 252 | writing Scala for an embarrassingly long time) is the ability to switch easily 253 | between the docs for a class or trait and its companion object. If you're 254 | reading the docs for the `List` class, for example: 255 | 256 | ![Operator list link](/screenshots/class.png) 257 | 258 | The circle with a "C" in it is a link that will take you to the docs for the 259 | `List` companion object: 260 | 261 | ![Operator list link](/screenshots/object.png) 262 | 263 | There's no need to search for `List` again to be able to select the object 264 | instead of the class. Clicking the "O" will take you back to the class. 265 | 266 | [java]: https://www.java.com/en/download/ 267 | [sbt]: http://www.scala-sbt.org 268 | [scala-scaladocs]: http://www.scala-lang.org/api/2.11.7/#package 269 | [string-ops]: http://www.scala-lang.org/api/2.11.7/#scala.collection.immutable.StringOps 270 | -------------------------------------------------------------------------------- /build.sbt: -------------------------------------------------------------------------------- 1 | scalaVersion := "2.11.8" 2 | 3 | libraryDependencies ++= Seq( 4 | "org.scalacheck" %% "scalacheck" % "1.12.5" % "test", 5 | "org.scalatest" %% "scalatest" % "2.2.6" % "test" 6 | ) 7 | -------------------------------------------------------------------------------- /project/build.properties: -------------------------------------------------------------------------------- 1 | sbt.version=0.13.11 2 | -------------------------------------------------------------------------------- /sbt: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # 3 | # A more capable sbt runner, coincidentally also called sbt. 4 | # Author: Paul Phillips 5 | 6 | set -o pipefail 7 | 8 | # todo - make this dynamic 9 | declare -r sbt_release_version="0.13.9" 10 | declare -r sbt_unreleased_version="0.13.9" 11 | declare -r buildProps="project/build.properties" 12 | 13 | declare sbt_jar sbt_dir sbt_create sbt_version 14 | declare scala_version sbt_explicit_version 15 | declare verbose noshare batch trace_level log_level 16 | declare sbt_saved_stty debugUs 17 | 18 | echoerr () { echo >&2 "$@"; } 19 | vlog () { [[ -n "$verbose" ]] && echoerr "$@"; } 20 | 21 | # spaces are possible, e.g. sbt.version = 0.13.0 22 | build_props_sbt () { 23 | [[ -r "$buildProps" ]] && \ 24 | grep '^sbt\.version' "$buildProps" | tr '=\r' ' ' | awk '{ print $2; }' 25 | } 26 | 27 | update_build_props_sbt () { 28 | local ver="$1" 29 | local old="$(build_props_sbt)" 30 | 31 | [[ -r "$buildProps" ]] && [[ "$ver" != "$old" ]] && { 32 | perl -pi -e "s/^sbt\.version\b.*\$/sbt.version=${ver}/" "$buildProps" 33 | grep -q '^sbt.version[ =]' "$buildProps" || printf "\nsbt.version=%s\n" "$ver" >> "$buildProps" 34 | 35 | vlog "!!!" 36 | vlog "!!! Updated file $buildProps setting sbt.version to: $ver" 37 | vlog "!!! Previous value was: $old" 38 | vlog "!!!" 39 | } 40 | } 41 | 42 | set_sbt_version () { 43 | sbt_version="${sbt_explicit_version:-$(build_props_sbt)}" 44 | [[ -n "$sbt_version" ]] || sbt_version=$sbt_release_version 45 | export sbt_version 46 | } 47 | 48 | # restore stty settings (echo in particular) 49 | onSbtRunnerExit() { 50 | [[ -n "$sbt_saved_stty" ]] || return 51 | vlog "" 52 | vlog "restoring stty: $sbt_saved_stty" 53 | stty "$sbt_saved_stty" 54 | unset sbt_saved_stty 55 | } 56 | 57 | # save stty and trap exit, to ensure echo is reenabled if we are interrupted. 58 | trap onSbtRunnerExit EXIT 59 | sbt_saved_stty="$(stty -g 2>/dev/null)" 60 | vlog "Saved stty: $sbt_saved_stty" 61 | 62 | # this seems to cover the bases on OSX, and someone will 63 | # have to tell me about the others. 64 | get_script_path () { 65 | local path="$1" 66 | [[ -L "$path" ]] || { echo "$path" ; return; } 67 | 68 | local target="$(readlink "$path")" 69 | if [[ "${target:0:1}" == "/" ]]; then 70 | echo "$target" 71 | else 72 | echo "${path%/*}/$target" 73 | fi 74 | } 75 | 76 | die() { 77 | echo "Aborting: $@" 78 | exit 1 79 | } 80 | 81 | make_url () { 82 | local version="$1" 83 | 84 | case "$version" in 85 | 0.7.*) echo "http://simple-build-tool.googlecode.com/files/sbt-launch-0.7.7.jar" ;; 86 | 0.10.* ) echo "$sbt_launch_repo/org.scala-tools.sbt/sbt-launch/$version/sbt-launch.jar" ;; 87 | 0.11.[12]) echo "$sbt_launch_repo/org.scala-tools.sbt/sbt-launch/$version/sbt-launch.jar" ;; 88 | *) echo "$sbt_launch_repo/org.scala-sbt/sbt-launch/$version/sbt-launch.jar" ;; 89 | esac 90 | } 91 | 92 | init_default_option_file () { 93 | local overriding_var="${!1}" 94 | local default_file="$2" 95 | if [[ ! -r "$default_file" && "$overriding_var" =~ ^@(.*)$ ]]; then 96 | local envvar_file="${BASH_REMATCH[1]}" 97 | if [[ -r "$envvar_file" ]]; then 98 | default_file="$envvar_file" 99 | fi 100 | fi 101 | echo "$default_file" 102 | } 103 | 104 | declare -r cms_opts="-XX:+CMSClassUnloadingEnabled -XX:+UseConcMarkSweepGC" 105 | declare -r jit_opts="-XX:ReservedCodeCacheSize=256m -XX:+TieredCompilation" 106 | declare -r default_jvm_opts_common="-Xms512m -Xmx1536m -Xss2m $jit_opts $cms_opts" 107 | declare -r noshare_opts="-Dsbt.global.base=project/.sbtboot -Dsbt.boot.directory=project/.boot -Dsbt.ivy.home=project/.ivy" 108 | declare -r latest_28="2.8.2" 109 | declare -r latest_29="2.9.3" 110 | declare -r latest_210="2.10.6" 111 | declare -r latest_211="2.11.7" 112 | declare -r latest_212="2.12.0-M3" 113 | 114 | declare -r script_path="$(get_script_path "$BASH_SOURCE")" 115 | declare -r script_name="${script_path##*/}" 116 | 117 | # some non-read-onlies set with defaults 118 | declare java_cmd="java" 119 | declare sbt_opts_file="$(init_default_option_file SBT_OPTS .sbtopts)" 120 | declare jvm_opts_file="$(init_default_option_file JVM_OPTS .jvmopts)" 121 | declare sbt_launch_dir="$HOME/.sbt/launchers" 122 | declare sbt_launch_repo="http://repo.typesafe.com/typesafe/ivy-releases" 123 | 124 | # pull -J and -D options to give to java. 125 | declare -a residual_args 126 | declare -a java_args 127 | declare -a scalac_args 128 | declare -a sbt_commands 129 | 130 | # args to jvm/sbt via files or environment variables 131 | declare -a extra_jvm_opts extra_sbt_opts 132 | 133 | addJava () { 134 | vlog "[addJava] arg = '$1'" 135 | java_args+=("$1") 136 | } 137 | addSbt () { 138 | vlog "[addSbt] arg = '$1'" 139 | sbt_commands+=("$1") 140 | } 141 | setThisBuild () { 142 | vlog "[addBuild] args = '$@'" 143 | local key="$1" && shift 144 | addSbt "set $key in ThisBuild := $@" 145 | } 146 | addScalac () { 147 | vlog "[addScalac] arg = '$1'" 148 | scalac_args+=("$1") 149 | } 150 | addResidual () { 151 | vlog "[residual] arg = '$1'" 152 | residual_args+=("$1") 153 | } 154 | addResolver () { 155 | addSbt "set resolvers += $1" 156 | } 157 | addDebugger () { 158 | addJava "-Xdebug" 159 | addJava "-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=$1" 160 | } 161 | setScalaVersion () { 162 | [[ "$1" == *"-SNAPSHOT" ]] && addResolver 'Resolver.sonatypeRepo("snapshots")' 163 | addSbt "++ $1" 164 | } 165 | setJavaHome () { 166 | java_cmd="$1/bin/java" 167 | setThisBuild javaHome "Some(file(\"$1\"))" 168 | export JAVA_HOME="$1" 169 | export JDK_HOME="$1" 170 | export PATH="$JAVA_HOME/bin:$PATH" 171 | } 172 | setJavaHomeQuietly () { 173 | addSbt warn 174 | setJavaHome "$1" 175 | addSbt info 176 | } 177 | 178 | # if set, use JDK_HOME/JAVA_HOME over java found in path 179 | if [[ -e "$JDK_HOME/lib/tools.jar" ]]; then 180 | setJavaHomeQuietly "$JDK_HOME" 181 | elif [[ -e "$JAVA_HOME/bin/java" ]]; then 182 | setJavaHomeQuietly "$JAVA_HOME" 183 | fi 184 | 185 | # directory to store sbt launchers 186 | [[ -d "$sbt_launch_dir" ]] || mkdir -p "$sbt_launch_dir" 187 | [[ -w "$sbt_launch_dir" ]] || sbt_launch_dir="$(mktemp -d -t sbt_extras_launchers.XXXXXX)" 188 | 189 | java_version () { 190 | local version=$("$java_cmd" -version 2>&1 | grep -E -e '(java|openjdk) version' | awk '{ print $3 }' | tr -d \") 191 | vlog "Detected Java version: $version" 192 | echo "${version:2:1}" 193 | } 194 | 195 | # MaxPermSize critical on pre-8 jvms but incurs noisy warning on 8+ 196 | default_jvm_opts () { 197 | local v="$(java_version)" 198 | if [[ $v -ge 8 ]]; then 199 | echo "$default_jvm_opts_common" 200 | else 201 | echo "-XX:MaxPermSize=384m $default_jvm_opts_common" 202 | fi 203 | } 204 | 205 | build_props_scala () { 206 | if [[ -r "$buildProps" ]]; then 207 | versionLine="$(grep '^build.scala.versions' "$buildProps")" 208 | versionString="${versionLine##build.scala.versions=}" 209 | echo "${versionString%% .*}" 210 | fi 211 | } 212 | 213 | execRunner () { 214 | # print the arguments one to a line, quoting any containing spaces 215 | vlog "# Executing command line:" && { 216 | for arg; do 217 | if [[ -n "$arg" ]]; then 218 | if printf "%s\n" "$arg" | grep -q ' '; then 219 | printf >&2 "\"%s\"\n" "$arg" 220 | else 221 | printf >&2 "%s\n" "$arg" 222 | fi 223 | fi 224 | done 225 | vlog "" 226 | } 227 | 228 | [[ -n "$batch" ]] && exec /dev/null; then 250 | curl --fail --silent --location "$url" --output "$jar" 251 | elif which wget >/dev/null; then 252 | wget --quiet -O "$jar" "$url" 253 | fi 254 | } && [[ -r "$jar" ]] 255 | } 256 | 257 | acquire_sbt_jar () { 258 | local sbt_url="$(jar_url "$sbt_version")" 259 | sbt_jar="$(jar_file "$sbt_version")" 260 | 261 | [[ -r "$sbt_jar" ]] || download_url "$sbt_url" "$sbt_jar" 262 | } 263 | 264 | usage () { 265 | cat < display stack traces with a max of frames (default: -1, traces suppressed) 284 | -debug-inc enable debugging log for the incremental compiler 285 | -no-colors disable ANSI color codes 286 | -sbt-create start sbt even if current directory contains no sbt project 287 | -sbt-dir path to global settings/plugins directory (default: ~/.sbt/) 288 | -sbt-boot path to shared boot directory (default: ~/.sbt/boot in 0.11+) 289 | -ivy path to local Ivy repository (default: ~/.ivy2) 290 | -no-share use all local caches; no sharing 291 | -offline put sbt in offline mode 292 | -jvm-debug Turn on JVM debugging, open at the given port. 293 | -batch Disable interactive mode 294 | -prompt Set the sbt prompt; in expr, 's' is the State and 'e' is Extracted 295 | 296 | # sbt version (default: sbt.version from $buildProps if present, otherwise $sbt_release_version) 297 | -sbt-force-latest force the use of the latest release of sbt: $sbt_release_version 298 | -sbt-version use the specified version of sbt (default: $sbt_release_version) 299 | -sbt-dev use the latest pre-release version of sbt: $sbt_unreleased_version 300 | -sbt-jar use the specified jar as the sbt launcher 301 | -sbt-launch-dir directory to hold sbt launchers (default: $sbt_launch_dir) 302 | -sbt-launch-repo repo url for downloading sbt launcher jar (default: $sbt_launch_repo) 303 | 304 | # scala version (default: as chosen by sbt) 305 | -28 use $latest_28 306 | -29 use $latest_29 307 | -210 use $latest_210 308 | -211 use $latest_211 309 | -212 use $latest_212 310 | -scala-home use the scala build at the specified directory 311 | -scala-version use the specified version of scala 312 | -binary-version use the specified scala version when searching for dependencies 313 | 314 | # java version (default: java from PATH, currently $(java -version 2>&1 | grep version)) 315 | -java-home alternate JAVA_HOME 316 | 317 | # passing options to the jvm - note it does NOT use JAVA_OPTS due to pollution 318 | # The default set is used if JVM_OPTS is unset and no -jvm-opts file is found 319 | $(default_jvm_opts) 320 | JVM_OPTS environment variable holding either the jvm args directly, or 321 | the reference to a file containing jvm args if given path is prepended by '@' (e.g. '@/etc/jvmopts') 322 | Note: "@"-file is overridden by local '.jvmopts' or '-jvm-opts' argument. 323 | -jvm-opts file containing jvm args (if not given, .jvmopts in project root is used if present) 324 | -Dkey=val pass -Dkey=val directly to the jvm 325 | -J-X pass option -X directly to the jvm (-J is stripped) 326 | 327 | # passing options to sbt, OR to this runner 328 | SBT_OPTS environment variable holding either the sbt args directly, or 329 | the reference to a file containing sbt args if given path is prepended by '@' (e.g. '@/etc/sbtopts') 330 | Note: "@"-file is overridden by local '.sbtopts' or '-sbt-opts' argument. 331 | -sbt-opts file containing sbt args (if not given, .sbtopts in project root is used if present) 332 | -S-X add -X to sbt's scalacOptions (-S is stripped) 333 | EOM 334 | } 335 | 336 | process_args () { 337 | require_arg () { 338 | local type="$1" 339 | local opt="$2" 340 | local arg="$3" 341 | 342 | if [[ -z "$arg" ]] || [[ "${arg:0:1}" == "-" ]]; then 343 | die "$opt requires <$type> argument" 344 | fi 345 | } 346 | while [[ $# -gt 0 ]]; do 347 | case "$1" in 348 | -h|-help) usage; exit 1 ;; 349 | -v) verbose=true && shift ;; 350 | -d) addSbt "--debug" && addSbt debug && shift ;; 351 | -w) addSbt "--warn" && addSbt warn && shift ;; 352 | -q) addSbt "--error" && addSbt error && shift ;; 353 | -x) debugUs=true && shift ;; 354 | -trace) require_arg integer "$1" "$2" && trace_level="$2" && shift 2 ;; 355 | -ivy) require_arg path "$1" "$2" && addJava "-Dsbt.ivy.home=$2" && shift 2 ;; 356 | -no-colors) addJava "-Dsbt.log.noformat=true" && shift ;; 357 | -no-share) noshare=true && shift ;; 358 | -sbt-boot) require_arg path "$1" "$2" && addJava "-Dsbt.boot.directory=$2" && shift 2 ;; 359 | -sbt-dir) require_arg path "$1" "$2" && sbt_dir="$2" && shift 2 ;; 360 | -debug-inc) addJava "-Dxsbt.inc.debug=true" && shift ;; 361 | -offline) addSbt "set offline := true" && shift ;; 362 | -jvm-debug) require_arg port "$1" "$2" && addDebugger "$2" && shift 2 ;; 363 | -batch) batch=true && shift ;; 364 | -prompt) require_arg "expr" "$1" "$2" && setThisBuild shellPrompt "(s => { val e = Project.extract(s) ; $2 })" && shift 2 ;; 365 | 366 | -sbt-create) sbt_create=true && shift ;; 367 | -sbt-jar) require_arg path "$1" "$2" && sbt_jar="$2" && shift 2 ;; 368 | -sbt-version) require_arg version "$1" "$2" && sbt_explicit_version="$2" && shift 2 ;; 369 | -sbt-force-latest) sbt_explicit_version="$sbt_release_version" && shift ;; 370 | -sbt-dev) sbt_explicit_version="$sbt_unreleased_version" && shift ;; 371 | -sbt-launch-dir) require_arg path "$1" "$2" && sbt_launch_dir="$2" && shift 2 ;; 372 | -sbt-launch-repo) require_arg path "$1" "$2" && sbt_launch_repo="$2" && shift 2 ;; 373 | -scala-version) require_arg version "$1" "$2" && setScalaVersion "$2" && shift 2 ;; 374 | -binary-version) require_arg version "$1" "$2" && setThisBuild scalaBinaryVersion "\"$2\"" && shift 2 ;; 375 | -scala-home) require_arg path "$1" "$2" && setThisBuild scalaHome "Some(file(\"$2\"))" && shift 2 ;; 376 | -java-home) require_arg path "$1" "$2" && setJavaHome "$2" && shift 2 ;; 377 | -sbt-opts) require_arg path "$1" "$2" && sbt_opts_file="$2" && shift 2 ;; 378 | -jvm-opts) require_arg path "$1" "$2" && jvm_opts_file="$2" && shift 2 ;; 379 | 380 | -D*) addJava "$1" && shift ;; 381 | -J*) addJava "${1:2}" && shift ;; 382 | -S*) addScalac "${1:2}" && shift ;; 383 | -28) setScalaVersion "$latest_28" && shift ;; 384 | -29) setScalaVersion "$latest_29" && shift ;; 385 | -210) setScalaVersion "$latest_210" && shift ;; 386 | -211) setScalaVersion "$latest_211" && shift ;; 387 | -212) setScalaVersion "$latest_212" && shift ;; 388 | 389 | --debug) addSbt debug && addResidual "$1" && shift ;; 390 | --warn) addSbt warn && addResidual "$1" && shift ;; 391 | --error) addSbt error && addResidual "$1" && shift ;; 392 | *) addResidual "$1" && shift ;; 393 | esac 394 | done 395 | } 396 | 397 | # process the direct command line arguments 398 | process_args "$@" 399 | 400 | # skip #-styled comments and blank lines 401 | readConfigFile() { 402 | local end=false 403 | until $end; do 404 | read || end=true 405 | [[ $REPLY =~ ^# ]] || [[ -z $REPLY ]] || echo "$REPLY" 406 | done < "$1" 407 | } 408 | 409 | # if there are file/environment sbt_opts, process again so we 410 | # can supply args to this runner 411 | if [[ -r "$sbt_opts_file" ]]; then 412 | vlog "Using sbt options defined in file $sbt_opts_file" 413 | while read opt; do extra_sbt_opts+=("$opt"); done < <(readConfigFile "$sbt_opts_file") 414 | elif [[ -n "$SBT_OPTS" && ! ("$SBT_OPTS" =~ ^@.*) ]]; then 415 | vlog "Using sbt options defined in variable \$SBT_OPTS" 416 | extra_sbt_opts=( $SBT_OPTS ) 417 | else 418 | vlog "No extra sbt options have been defined" 419 | fi 420 | 421 | [[ -n "${extra_sbt_opts[*]}" ]] && process_args "${extra_sbt_opts[@]}" 422 | 423 | # reset "$@" to the residual args 424 | set -- "${residual_args[@]}" 425 | argumentCount=$# 426 | 427 | # set sbt version 428 | set_sbt_version 429 | 430 | # only exists in 0.12+ 431 | setTraceLevel() { 432 | case "$sbt_version" in 433 | "0.7."* | "0.10."* | "0.11."* ) echoerr "Cannot set trace level in sbt version $sbt_version" ;; 434 | *) setThisBuild traceLevel $trace_level ;; 435 | esac 436 | } 437 | 438 | # set scalacOptions if we were given any -S opts 439 | [[ ${#scalac_args[@]} -eq 0 ]] || addSbt "set scalacOptions in ThisBuild += \"${scalac_args[@]}\"" 440 | 441 | # Update build.properties on disk to set explicit version - sbt gives us no choice 442 | [[ -n "$sbt_explicit_version" ]] && update_build_props_sbt "$sbt_explicit_version" 443 | vlog "Detected sbt version $sbt_version" 444 | 445 | [[ -n "$scala_version" ]] && vlog "Overriding scala version to $scala_version" 446 | 447 | # no args - alert them there's stuff in here 448 | (( argumentCount > 0 )) || { 449 | vlog "Starting $script_name: invoke with -help for other options" 450 | residual_args=( shell ) 451 | } 452 | 453 | # verify this is an sbt dir or -create was given 454 | [[ -r ./build.sbt || -d ./project || -n "$sbt_create" ]] || { 455 | cat < Z): Z = this match { 3 | case NoInt => ifNone 4 | case SomeInt(value) => ifSome(value) 5 | } 6 | } 7 | 8 | final case object NoInt extends IntOption 9 | final case class SomeInt(value: Int) extends IntOption 10 | 11 | 12 | sealed trait MyIntList { 13 | def toList: List[Int] = ??? 14 | 15 | def fold[Z](ifEmpty: Z, ifCell: (Int, Z) => Z): Z = ??? 16 | } 17 | 18 | final case object Empty extends MyIntList 19 | final case class Cell(head: Int, tail: MyIntList) extends MyIntList 20 | -------------------------------------------------------------------------------- /src/test/scala/CalculatorSpec.scala: -------------------------------------------------------------------------------- 1 | import org.scalatest.FlatSpec 2 | import org.scalatest.prop.Checkers 3 | 4 | class CalculatorSpec extends FlatSpec with Checkers { 5 | "A Calculator" should "add integers" in { 6 | check { (a: Int, b: Int) => 7 | Calculator.+(Success(a), b) === Success(a + b) 8 | } 9 | } 10 | 11 | it should "subtract integers" in { 12 | check { (a: Int, b: Int) => 13 | Calculator.-(Success(a), b) === Success(a - b) 14 | } 15 | } 16 | 17 | it should "divide integers safely" in { 18 | check { (a: Int, b: Int) => 19 | val result = Calculator./(Success(a), b) 20 | 21 | if (b == 0) { 22 | result === Failure("Division by zero") 23 | } else { 24 | result === Success(a / b) 25 | } 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/test/scala/MyIntListSpec.scala: -------------------------------------------------------------------------------- 1 | import org.scalacheck.{ Arbitrary, Gen } 2 | import org.scalatest.FlatSpec 3 | import org.scalatest.prop.Checkers 4 | 5 | class MyIntListSpec extends FlatSpec with Checkers { 6 | implicit val arbitraryMyIntList: Arbitrary[MyIntList] = Arbitrary( 7 | Gen.oneOf( 8 | for { 9 | head <- Arbitrary.arbitrary[Int] 10 | tail <- Arbitrary.arbitrary[MyIntList] 11 | } yield Cell(head, tail), 12 | Gen.const(Empty) 13 | ) 14 | ) 15 | 16 | "MyIntList" should "be convertible to a regular list with fold" in { 17 | check { (l: MyIntList) => 18 | l.fold[List[Int]](Nil, (head, tail) => head :: tail) === l.toList 19 | } 20 | } 21 | } 22 | --------------------------------------------------------------------------------