1
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?

zerocool 18
  • 45
  • 1
  • 9

2 Answers2

2

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.

MattPutnam
  • 2,927
  • 2
  • 17
  • 23
  • 2
    Presumably that's a "set" because we can view a set as a function taking a potential element and returning true or false depending on whether that element is in the set. (so the set of evens would return i%2==0, for an obvious example) – Jon Kiparsky Oct 14 '14 at 04:38
-1

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.

Mike
  • 698
  • 1
  • 9
  • 16
  • Does not answer the current (5 year old) question because `type T = ...` is very different from `[T]` which is a type parameter that might **or might not** "refer to the type of the elements in this object". – jwvh Nov 20 '19 at 08:21
  • That's why I've added the (0 year old) comment: *"when I got to this post I was actually trying to understand what the meaning of T within square brackets was"* to clarify my post **with** a **screenshot** of what I've read and where I've read it. It is a common way of sharing related information **without** opening a brand new question for it... that's all. – Mike Nov 20 '19 at 11:58