Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
In this video we'll take a few steps to dramatically reduce the amount of code required to create a deck!
Kotlin Links
Project Files
Workshop
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Our Deck class works just fine as it is,
but
0:00
with a little help from Kotlin
we can make this a lot simpler.
0:03
But first, let's move this function
around to look a bit more normal.
0:06
For starters, instead of creating
a variable for our value,
0:10
let's just pass in i % 13.
0:14
And instead of passing in a suit,
let's pass in a new function getSuit and
0:20
then pass in i to our new function.
0:25
And let's use Alt+Enter to generate it.
0:29
Then, inside the function,
let's paste in our suit variable.
0:33
And then return it.
0:42
Nice, but remember,
0:45
if a function is just one expression then
we can rewrite it with an equal sign.
0:47
So let's rewrite getSuit to just
be equal to our when expression.
0:51
Another way we can shorten our code
is to declare our anonymous function
0:57
by using what's known
as a lambda expression.
1:01
When you're trying to pass some kind
of functionality as an argument,
1:04
like we're doing here, as the second
argument to the array function,
1:08
anonymous functions
are just a lot of typing.
1:13
To turn this anonymous function
into a lambda expression,
1:16
let's start by deleting
the function declaration.
1:19
Then, let's also get rid
of the return keyword.
1:28
And finally, let's add back in our
parameter by typing i, and then an arrow.
1:32
And there we go.
1:38
It's the same code as before,
with a lot less words.
1:39
Now let's clean this up to put
our cards array all on one line.
1:44
And for one final trick, when there's only
one parameter to our lambda expression,
2:00
like we have here with i, then we can
omit the parameter section entirely.
2:05
And Kotlin will automatically
create the parameter for
2:12
us behind the scenes with the name it.
2:15
So let's change i to it.
2:19
And that's about as good as it gets for
populating the cards array,
2:23
after I remember that parentheses.
2:26
At this point, we've simplified
the code almost as much as we can but
2:29
there's still one step we
haven't taken yet, and
2:33
that is that data types in
Kotlin are mostly optional.
2:36
If Kotlin can tell what data type
something is then we don't need
2:39
to specify it.
2:43
So in getSuit, since each option in
our when statement returns a string,
2:44
we can safely get away with
not declaring a return type.
2:48
Kotlin knows it will be a string.
2:53
Kotlin also knows that our cards
array is an array of cards.
2:55
So we don't need to declare
this return type either.
2:59
But don't get into the habit of
leaving off return types just yet.
3:04
Watch what happens if I
change Spades to a 1.
3:08
Now we have an error in
our card constructor
3:11
saying that there's a type mismatch.
3:15
A string is required, but that we're
giving it an object of type Any.
3:17
In Kotlin, the Any class can basically be
thought of as the Object class from Java.
3:22
All classes in Kotlin, one way or
another, have Any as a superclass.
3:27
So since getSuit can now return a string
or an int, the only safe way for
3:33
Kotlin to reconcile this is if
getSuit returns an object of type Any,
3:38
since both int and
string are descendants of Any.
3:43
Then, if we add the return type
back to our getSuit function,
3:49
We get the error right away,
instead of finding out from some piece of
3:55
code that could be anywhere
in your code base.
3:58
If we don't declare a type and
then end up making a mistake like this.
4:01
Kotlin's just going to
assume that you meant for
4:06
this to return whatever
data type makes sense.
4:08
That said,
let's add back in our Spade suit.
4:11
And then get rid of the return type.
4:17
I'm not worried about it being
anything other than a string.
4:19
Great work.
4:23
You created the card class and you've even
used it to create an entire deck of cards.
4:24
And in the next video, we'll start adding
some more functionality to our deck.
4:29
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up