Case functionality getting error when compiled

Hi I am a beginner in Scala an I am trying to write a script for matching the tuples with values in a list. I am applying condition if age is greater than 13 than print “Name + is too old” and if gender is male then print “name + is too old and is a male”

def eligibility(person: (name : String, age : Int, gender: String)) : Boolean = eligibility_ match{

    case (name : String, age:Int, gender: String) if age > 13 => print(name + " is too old")
    case _ => print(name + " is too old and not male") 

}
val people = List((“Harry”, 15, “male”), (“Peter”, 10, “male”),
(“Michele”, 20, “female”), (“Bruce”, 12, “male”),
(“Mary”, 2, “female”), (“Max”, 22, “male”))

val allowedEntry = people.filter(eligibility(_))
print(allowedEntry)

I am getting compile time errors in the code. Can anyone have a look?

Hi. What is the error you are getting?

There seem to be several things wrong with this:

  • you have two parameter lists nested inside each other, it looks like you’re trying to say person is a tuple? A tuple type does not allow names for its fields, you just give the types. If you want to name the fields, use a case class instead of a tuple.
  • you’re matching on a variable eligibility_ which is not defined in the code you posted.
  • your method doesn’t return a boolean, but its signature (and using it in filter) requires that.

PS: Some of these may be from the formatting in the forum here. For future posts, put ``` in a line before and after your code, to enable code formatting, which makes it more readable.

1 Like
def eligibility(person: (String, Int, String)) : Boolean =
{
eligibility_ match
{
        case (name : String, age:Int, gender: String) if age > 13 => print(name + " is too old")
        case _ => print(name + " is too old and not male") 
}
}

val people = List(("Harry", 15, "male"), ("Peter", 10, "male"),
                  ("Michele", 20, "female"), ("Bruce", 12, "male"),
                  ("Mary", 2, "female"), ("Max", 22, "male"))
val allowedEntry = people.filter(eligibility(_))
print(allowedEntry)

Output:

error: not found: value eligibility_
      eligibility_ match
      ^
<console>:27: error: type mismatch;
found   : Unit
required: Boolean
              case (name : String, age:Int, gender: String) if age > 13 => print(name + " is too old")
                                                                                ^
<console>:28: error: not found: value name
              case _ => print(name + " is too old and not male")
                              ^
scala> 
scala> val people = List(("Harry", 15, "male"), ("Peter", 10, "male"),
    |                   ("Michele", 20, "female"), ("Bruce", 12, "male"),
    |                   ("Mary", 2, "female"), ("Max", 22, "male"))
people: List[(String, Int, String)] = List((Harry,15,male), (Peter,10,male), (Michele,20,female), (Bruce,12,male), (Mary,2,female), (Max,22,male))
scala> val allowedEntry = people.filter(eligibility(_))
<console>:25: error: not found: value eligibility
      val allowedEntry = people.filter(eligibility(_))
                                       ^
scala> print(allowedEntry)
<console>:24: error: not found: value allowedEntry
      print(allowedEntry)

I was trying to use this example to make it work:

x match {
case 1 => print("one")
case 2 => print("two")
case 3 => print("three")
}

In simple terms, my goal is to write a function which, when given two integer sequences, returns a List of the numbers which appear at the same position in both sequences.

I have changed it to:
person: (String, Int, String)

This is the variable you match on, which like I said earlier, doesn’t exist. You’ll probably want to match on person

Your method contains only the match, so it returns whatever that match returns. Each of your cases just prints something. You’ll have to add a boolean to each case, so the match also returns boolean.

Your print statement in the second case won’t work that way, because the pattern variables are only valid in their case (otherwise, what would they be filled with, if the first case did not match?). You’ll have to provide a pattern, that binds the variable name there too. You can use _ for values you don’t need:

case (name, age, gender) if age > 13 => print(name + " is too old")
case (name, _, _) => print(name + " is too old and not male")

(BTW, your second case does not check the gender in any way, so that message seems wrong)

This error will go away, if you fix the ones before. The eligibility method isn’t defined, because it failed to compile (you wouldn’t get this error in a file instead of the REPL)

Sorry, but I don’t see how your code has anything to do with that?

1 Like

Thanks. I got it right

```  def eligibility(person: (String, Int, String)) : Boolean ={

person match
{
case (name : String, age:Int, gender: String) if age >= 13 && gender == “female” =>
println(name + " is not male and too old")
false
case (name : String, age:Int, gender: String) if gender == “female” =>
println(name + " is not male")
false
case (name : String, age:Int, gender: String) if age >= 13 =>
println(name + " is too old")
false
case _ =>
true
}
}

val people = List((“Harry”, 15, “male”), (“Peter”, 10, “male”),
(“Michele”, 20, “female”), (“Bruce”, 12, “male”),
(“Mary”, 2, “female”), (“Max”, 22, “male”))
val allowedEntry = people.filter(eligibility(_))

print(allowedEntry)