Scala has a pattern for this.

legendOption.map { legend => //no need to use legendOption.get.xy }