Skip to content

rewrote generic classes tour #705

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 1 commit into from
May 10, 2017

Conversation

travissarles
Copy link
Contributor

No description provided.

Copy link
Member

@jvican jvican left a comment

Choose a reason for hiding this comment

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

LGTM, great job!

Like in Java 5 (aka. [JDK 1.5](http://java.sun.com/j2se/1.5/)), Scala has built-in support for classes parameterized with types. Such generic classes are particularly useful for the development of collection classes.
Here is an example which demonstrates this:

## Defining a generic classes
Copy link
Member

Choose a reason for hiding this comment

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

Defining "a generic classes", typo?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops!

println(stack.pop) // prints 97
println(stack.peek) // prints 1
```
Here we have a stack of Ints. Notice how we can pass in the Char object `a`. This is because the `Char` class has a [method to implicitly convert a `Char` to an Int](http://scala-lang.org/api/current/scala/Char$.html#char2int(x:Char):Int).
Copy link
Member

Choose a reason for hiding this comment

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

Is this worth to be explained here? I think the example adds too much info to the reader, which is assumed to be familiar with implicit conversions. I would just remove the char example 😄.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fair enough

@travissarles travissarles force-pushed the generic-classes branch 2 times, most recently from e9153fd to 52d57a6 Compare February 20, 2017 21:56
Here is an example which demonstrates this:

## Defining a generic class
Generic classes take a type as a parameter within square brackets `[]`. One convention is to use the letter `T` as type parameter identifier, though this is not required.
Copy link
Contributor

@soronpo soronpo Feb 20, 2017

Choose a reason for hiding this comment

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

though this is not required

This suggests that you may omit the parameter completely, which is not the case.
Perhaps "though any parameter name may be applied".

Copy link

Choose a reason for hiding this comment

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

In Scala, using A rather than T is the convention for a single parameter.

Copy link
Member

Choose a reason for hiding this comment

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

It is? I use T.

Copy link

Choose a reason for hiding this comment

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

@SethTisue - A is ubiquitous in the standard library and in various other locations (IIRC it was the convention used in Programming in Scala, and it is the convention used in Functional Programming in Scala (which I can check despite being away from books because a chapter is available online).

Copy link
Member

Choose a reason for hiding this comment

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

interesting — thanks. I seem to have been oblivious.

Copy link
Member

Choose a reason for hiding this comment

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

Perhaps "It's common to use arbitrary letters such as A and B for this."

}
```
This implementation of a `Stack` class takes any type `A` as a parameter. This means the underlying list, `var elements: List[T] = Nil`, can only store elements of type `A`. The procedure `def push` only accepts objects of type `A` (note: `elements = x :: elements` reassigns `elements` to a new list created by prepending `x` to the current `elements`).
Copy link

Choose a reason for hiding this comment

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

There's a stray List[T] in here.

@Ichoran
Copy link

Ichoran commented Feb 23, 2017

There's a single typo left (a missed [T] vs. [A])

@travissarles travissarles mentioned this pull request Mar 17, 2017
33 tasks
@travissarles travissarles self-assigned this Mar 21, 2017
@travissarles travissarles merged commit ce78a06 into scala:master May 10, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
6 participants