[Solved] How can i make a for loop with if else and make the good return type


Your use of for does not behave as you expect. You’re using this for-comprehension:

for (data <- category(i)) {
  if (data.startsWith(x)) true
  else false
}

This expression “desugars” into (i.e. is shorthand for):

category(i).foreach(data => {
  if (data.startsWith(x)) true
  else false
})

foreach returns Unit, and therefore the type of this expression (and the type returned from your get_info method, this being the body of the method) is Unit.

It’s unclear what you expect to be returned here, I’m assuming you want the method to return true if any of the elements in category(i) start with x. If so, you can implement it as follows:

category(i).exists(_.startsWith(x))

Which returns a Boolean, as expected.

1

solved How can i make a for loop with if else and make the good return type