Skip to content

get tut to work with Travis #593

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Oct 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# opt-in to Travis new infrastructure
sudo: false
sudo: true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to be able to use sudo and similar commands. The main thing is to be able to chmod coursier. This is disallowed on the container version of Travis. The sudo: true puts it all on a virtual machine. At least this is my understanding.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can't even chmod +x things we just downloaded ourselves in our own home directory? that surprises me, I wouldn't expect superuser privileges to be necessary. are you sure?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it is our home directory. It is Travis's directory and Travis executes our scripts. At least this is how I think about it.

https://docs.travis-ci.com/user/ci-environment/

The container builds are heavily restricted. You can't even install all apt packages.

https://github.com/travis-ci/apt-package-whitelist

I did not look to deeply into it. If you remove the sudo: true Travis will throw an error when you try to chmod. There maybe another way to get around this error. Feel feel to play around with it.


language: ruby
rvm: 2.0.0
Expand All @@ -8,7 +7,10 @@ rvm: 2.0.0
# will kick in and use `vendor`, ignoring our BUNDLE_PATH
# declaration in `.bundle/config`
install:
- ./scripts/install-coursier.sh
- bundle install

script:
- ./scripts/run-tut.sh
- rm -r tut-tmp
- bundle exec jekyll build
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
source 'https://rubygems.org'
gem 'github-pages'
gem 'github-pages', group: :jekyll_plugins
3 changes: 2 additions & 1 deletion _config.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
markdown: kramdown
kramdown:
input: GFM

title: "Scala Documentation"
description: "Documentation for the Scala programming language - Tutorials, Overviews, Cheatsheets, and more."
Expand All @@ -21,4 +23,3 @@ baseurl:
exclude: ["vendor"]
gems:
- jekyll-redirect-from

2 changes: 2 additions & 0 deletions contribute.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ It's statically generated from [Markdown](http://en.wikipedia.org/wiki/Markdown)

The markdown syntax being used supports [Maruku](http://maruku.rubyforge.org/maruku.html) extensions, and has automatic syntax highlighting, without the need for any tags.

Additionally [tut](https://github.com/tpolecat/tut) is used during pull requests to validate Scala code blocks. To use this feature you must use the backtick notation as documented by tut. Note that only validation is done. The output files from tut are not used in the building of the tutorial. Either use `tut` or `tut:fail` for your code blocks.

## Submitting Docs

For one to contribute a document, one must simply
Expand Down
3 changes: 3 additions & 0 deletions scripts/install-coursier.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
curl -L -o coursier https://git.io/vgvpD
chmod +x coursier
6 changes: 6 additions & 0 deletions scripts/run-tut.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

COURSIER_CLASSPATH="$(./coursier fetch -p com.chuusai:shapeless_2.11:2.3.1 org.scala-lang.modules::scala-xml:1.0.3)"

./coursier launch -r "https://dl.bintray.com/tpolecat/maven/" org.tpolecat:tut-core_2.11:0.4.4 -- . tut-tmp '.*\.md$' -classpath "$COURSIER_CLASSPATH"

82 changes: 45 additions & 37 deletions tutorials/tour/abstract-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,57 +14,65 @@ tutorial-previous: inner-classes
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.
Here is an example which defines both a deferred value definition and an abstract type definition as members of [class](traits.html) `Buffer`.

trait Buffer {
type T
val element: T
}
```tut
trait Buffer {
type T
val element: T
}
```

*Abstract types* are types whose identity is not precisely known. In the example above, we only know that each object of class `Buffer` has a type member `T`, but the definition of class `Buffer` does not reveal to what concrete type the member type `T` corresponds. Like value definitions, we can override type definitions in subclasses. This allows us to reveal more information about an abstract type by tightening the type bound (which describes possible concrete instantiations of the abstract type).

In the following program we derive a class `SeqBuffer` which allows us to store only sequences in the buffer by stating that type `T` has to be a subtype of `Seq[U]` for a new abstract type `U`:

abstract class SeqBuffer extends Buffer {
type U
type T <: Seq[U]
def length = element.length
}
```tut
abstract class SeqBuffer extends Buffer {
type U
type T <: Seq[U]
def length = element.length
}
```

Traits or [classes](classes.html) with abstract type members are often used in combination with anonymous class instantiations. To illustrate this, we now look at a program which deals with a sequence buffer that refers to a list of integers:

abstract class IntSeqBuffer extends SeqBuffer {
type U = Int
}

object AbstractTypeTest1 extends App {
def newIntSeqBuf(elem1: Int, elem2: Int): IntSeqBuffer =
new IntSeqBuffer {
type T = List[U]
val element = List(elem1, elem2)
}
val buf = newIntSeqBuf(7, 8)
println("length = " + buf.length)
println("content = " + buf.element)
}
```tut
abstract class IntSeqBuffer extends SeqBuffer {
type U = Int
}

object AbstractTypeTest1 extends App {
def newIntSeqBuf(elem1: Int, elem2: Int): IntSeqBuffer =
new IntSeqBuffer {
type T = List[U]
val element = List(elem1, elem2)
}
val buf = newIntSeqBuf(7, 8)
println("length = " + buf.length)
println("content = " + buf.element)
}
```

The return type of method `newIntSeqBuf` refers to a specialization of trait `Buffer` in which type `U` is now equivalent to `Int`. We have a similar type alias in the anonymous class instantiation within the body of method `newIntSeqBuf`. Here we create a new instance of `IntSeqBuffer` in which type `T` refers to `List[Int]`.

Please note that it is often possible to turn abstract type members into type parameters of classes and vice versa. Here is a version of the code above which only uses type parameters:

abstract class Buffer[+T] {
val element: T
}
abstract class SeqBuffer[U, +T <: Seq[U]] extends Buffer[T] {
def length = element.length
}
object AbstractTypeTest2 extends App {
def newIntSeqBuf(e1: Int, e2: Int): SeqBuffer[Int, Seq[Int]] =
new SeqBuffer[Int, List[Int]] {
val element = List(e1, e2)
}
val buf = newIntSeqBuf(7, 8)
println("length = " + buf.length)
println("content = " + buf.element)
```tut
abstract class Buffer[+T] {
val element: T
}
abstract class SeqBuffer[U, +T <: Seq[U]] extends Buffer[T] {
def length = element.length
}
object AbstractTypeTest2 extends App {
def newIntSeqBuf(e1: Int, e2: Int): SeqBuffer[Int, Seq[Int]] =
new SeqBuffer[Int, List[Int]] {
val element = List(e1, e2)
}
val buf = newIntSeqBuf(7, 8)
println("length = " + buf.length)
println("content = " + buf.element)
}
```

Note that we have to use [variance annotations](variances.html) here; otherwise we would not be able to hide the concrete sequence implementation type of the object returned from method `newIntSeqBuf`. Furthermore, there are cases where it is not possible to replace abstract types with type parameters.

123 changes: 70 additions & 53 deletions tutorials/tour/annotations.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,93 +38,110 @@ In the following example we add the `throws` annotation to the definition of the
> A Java compiler checks that a program contains handlers for [checked exceptions](http://docs.oracle.com/javase/specs/jls/se5.0/html/exceptions.html) by analyzing which checked exceptions can result from execution of a method or constructor. For each checked exception which is a possible result, the **throws** clause for the method or constructor _must_ mention the class of that exception or one of the superclasses of the class of that exception.
> Since Scala has no checked exceptions, Scala methods _must_ be annotated with one or more `throws` annotations such that Java code can catch exceptions thrown by a Scala method.

package examples
import java.io._
class Reader(fname: String) {
private val in = new BufferedReader(new FileReader(fname))
@throws(classOf[IOException])
def read() = in.read()
}
```
package examples
import java.io._
class Reader(fname: String) {
private val in = new BufferedReader(new FileReader(fname))
@throws(classOf[IOException])
def read() = in.read()
}
```

The following Java program prints out the contents of the file whose name is passed as the first argument to the `main` method.

package test;
import examples.Reader; // Scala class !!
public class AnnotaTest {
public static void main(String[] args) {
try {
Reader in = new Reader(args[0]);
int c;
while ((c = in.read()) != -1) {
System.out.print((char) c);
}
} catch (java.io.IOException e) {
System.out.println(e.getMessage());
```
package test;
import examples.Reader; // Scala class !!
public class AnnotaTest {
public static void main(String[] args) {
try {
Reader in = new Reader(args[0]);
int c;
while ((c = in.read()) != -1) {
System.out.print((char) c);
}
} catch (java.io.IOException e) {
System.out.println(e.getMessage());
}
}
}
```

Commenting out the `throws` annotation in the class Reader produces the following error message when compiling the Java main program:

Main.java:11: exception java.io.IOException is never thrown in body of
corresponding try statement
} catch (java.io.IOException e) {
^
1 error
```
Main.java:11: exception java.io.IOException is never thrown in body of
corresponding try statement
} catch (java.io.IOException e) {
^
1 error
```

### Java Annotations ###

**Note:** Make sure you use the `-target:jvm-1.5` option with Java annotations.

Java 1.5 introduced user-defined metadata in the form of [annotations](http://java.sun.com/j2se/1.5.0/docs/guide/language/annotations.html). A key feature of annotations is that they rely on specifying name-value pairs to initialize their elements. For instance, if we need an annotation to track the source of some class we might define it as

@interface Source {
public String URL();
public String mail();
}
```
@interface Source {
public String URL();
public String mail();
}
```

And then apply it as follows

@Source(URL = "http://coders.com/",
mail = "support@coders.com")
public class MyClass extends HisClass ...
```
@Source(URL = "http://coders.com/",
mail = "support@coders.com")
public class MyClass extends HisClass ...
```

An annotation application in Scala looks like a constructor invocation, for instantiating a Java annotation one has to use named arguments:

@Source(URL = "http://coders.com/",
mail = "support@coders.com")
class MyScalaClass ...
```
@Source(URL = "http://coders.com/",
mail = "support@coders.com")
class MyScalaClass ...
```

This syntax is quite tedious if the annotation contains only one element (without default value) so, by convention, if the name is specified as `value` it can be applied in Java using a constructor-like syntax:

@interface SourceURL {
public String value();
public String mail() default "";
}
```
@interface SourceURL {
public String value();
public String mail() default "";
}
```

And then apply it as follows

@SourceURL("http://coders.com/")
public class MyClass extends HisClass ...
```
@SourceURL("http://coders.com/")
public class MyClass extends HisClass ...
```

In this case, Scala provides the same possibility

@SourceURL("http://coders.com/")
class MyScalaClass ...
```
@SourceURL("http://coders.com/")
class MyScalaClass ...
```

The `mail` element was specified with a default value so we need not explicitly provide a value for it. However, if we need to do it we can not mix-and-match the two styles in Java:

@SourceURL(value = "http://coders.com/",
mail = "support@coders.com")
public class MyClass extends HisClass ...
```
@SourceURL(value = "http://coders.com/",
mail = "support@coders.com")
public class MyClass extends HisClass ...
```

Scala provides more flexibility in this respect

@SourceURL("http://coders.com/",
mail = "support@coders.com")
class MyScalaClass ...





```
@SourceURL("http://coders.com/",
mail = "support@coders.com")
class MyScalaClass ...
```
36 changes: 24 additions & 12 deletions tutorials/tour/anonymous-function-syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,42 @@ tutorial-previous: mixin-class-composition

Scala provides a relatively lightweight syntax for defining anonymous functions. The following expression creates a successor function for integers:

(x: Int) => x + 1
```tut
(x: Int) => x + 1
```

This is a shorthand for the following anonymous class definition:

new Function1[Int, Int] {
def apply(x: Int): Int = x + 1
}
```tut
new Function1[Int, Int] {
def apply(x: Int): Int = x + 1
}
```

It is also possible to define functions with multiple parameters:

(x: Int, y: Int) => "(" + x + ", " + y + ")"
```tut
(x: Int, y: Int) => "(" + x + ", " + y + ")"
```

or with no parameter:

() => { System.getProperty("user.dir") }
```tut
() => { System.getProperty("user.dir") }
```

There is also a very lightweight way to write function types. Here are the types of the three functions defined above:

Int => Int
(Int, Int) => String
() => String
```
Int => Int
(Int, Int) => String
() => String
```

This syntax is a shorthand for the following types:

Function1[Int, Int]
Function2[Int, Int, String]
Function0[String]
```
Function1[Int, Int]
Function2[Int, Int, String]
Function0[String]
```
Loading