-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Conversation
There was a problem hiding this 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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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). |
There was a problem hiding this comment.
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 😄.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough
e9153fd
to
52d57a6
Compare
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. |
There was a problem hiding this comment.
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".
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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."
52d57a6
to
0e41493
Compare
} | ||
``` | ||
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`). |
There was a problem hiding this comment.
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.
There's a single typo left (a missed |
0e41493
to
305e2e0
Compare
No description provided.