0

I started learning scala recently and stumbled upon a problem trying to "print" cells:

class Cell(
            val x: Int,
            val y: Int,
            val left: Option[Cell],
            val right: Option[Cell],
            val top: Option[Cell],
            val bottom: Option[Cell],
            var isPainted: Boolean) {

  def paint(radius: Int) = {
    println("Printing")

    if (radius == 0)
      isPainted = true
    else {
      isPainted = true
      top.isPainted = true
      bottom.isPainted = true
      left.isPainted = true
      right.isPainted = true

Why are these neighbouring cells

      top.left.paint(r - 1)
      top.right.paint(r - 1)
      bottom.left.paint(r - 1)
      bottom.right.paint(r - 1)

not accessible?

    }
  }

How can I access the top, bottom, left, right cells?

edit:

thank you for the great answer. one addon- question:

how could I check that all the cells around cell x are set to true? My try unfortunately does not work.

def isMiddleCell()={
    if(List(top, bottom, left, right).forall(_.Some))
      true
    else
      false
  }
2
  • Because they are not Cells, they are Option[Cell]. Read about Options. Commented Feb 10, 2016 at 21:11
  • So if I understand correctly a .get() needs to be called similar to: val ttop = top.getOrElse("No top neighbour") ttop.isPainted = true top.getOrElse("No top neighbour").isPainted = true However both of these do not work. Even wrapping the truein Some(true) does not work. Commented Feb 10, 2016 at 21:16

1 Answer 1

2

to do this

  bottom.isPainted = true

you actually need to modify value inside Option

  bottom.foreach(_.isPainted = true)

to access nested Option you should use flatMap, so

top.left.paint(r - 1)

should be

top.flatMap(_.left).foreach(_.paint(r - 1))

You basically want to make a side effect, execute some code if value is present. For this, the idomatic solution is foreach which does exactly this.

To navigate through your cells that have their neighbours as Option as well, flatMap is the simplest solution. top is of type Option[Cell]. If you wanted to get the value inside it and extract left from it, you could think about map.

top.map(_.left)

this expression has type Option[Option[Cell]], this is why flatMap is helpful here, it will flatten nested Options and give you [Option[Cell]] that will contain inside tle left neighbour of top neighbour of your cell if it exists. Now, having this you can simply do foreach as in previous example.

To explain on your comment where you posted this code:

top.getOrElse("No top neighbour").isPainted = true

this couldn't work, because it will get you the Cell if it is present, else it will give you a String which gives you expression of type (I think) Serializable (so the most common supertype) that has no field called isPainted. When you use getOrElse you usually want to put the expression of the same type that the object inside the Option. But anyway getOrElse doesn't seem like most useful thing in your example.

Edit

To answer the follow up question, method that checks for value presence is isDefined

def isMiddleCell = List(top, bottom, left, right).forall(_.isDefined)

Some is a type. The _ syntax is just shorthand for function, expanded it looks like this:

x.forall(a => a.isDefined)
Sign up to request clarification or add additional context in comments.

4 Comments

Do I understand correctly that I sould change the method signature to return Unit? stackoverflow.com/questions/3739133/… this seems somewhat strange ...
yes, it seems like your method does not return anything, so it should have return type of Unit which is equivalent of void from java. You are calling it recursively, so scala compiler can't or won't infer the return type, you need to specify it yourself.
Thank you very much for the explanation.
May I ask a small add-on question - please see the edit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.