zerocool 18
zerocool 18

Reputation: 45

What does "type T = .." mean in Scala?

type Set = Int => Boolean

I'm trying figure out what this means. From my understanding type is like an alias, but I'm not sure how that's different from using def. If my teacher wouldn't have told me to use type, I would have went with def. I also need a little clarity on the what the rest means. So we have a type named Set, that is Int, but what does => Boolean mean?

Upvotes: 1

Views: 1003

Answers (2)

Mike
Mike

Reputation: 790

T within square brackets [T]

I'm completely new to Scala, and when I got to this post I was actually trying to understand what the meaning of T within square brackets was [T] and, I found that it refers to the type of the elements in this 'object' (WrappedArray was the object I was reading about).

enter image description here

...just in case this helps someone.

Upvotes: -1

MattPutnam
MattPutnam

Reputation: 3017

The type keyword creates a type alias. Very similar to typedef in C++ if you know that. The purpose is to assign context-specific names to generic things. So in a Person class you might do type Firstname = String / type Lastname = String just so you can differentiate the two fields by type and not confuse them (this is a stupid example but it's past midnight so deal with it).

In this case, you're defining Set to be an alias of Int => Boolean, which is the type of a function that takes Int and returns Boolean. I'm not exactly sure why that constitutes a "set", but that's what the code means.

Upvotes: 2

Related Questions