Scala generally favors typed data and immutability, and you're fighting against both of those here. I don't know what the context is for this map, but I think it would be more idiomatic to use a case clase with optional parameters. For example:
case class Person(name: String, age: Option[Int], children: Seq[Person]) {
def hasChildren: Boolean = !children.isEmpty
}
Now you might call this as follows with an optional name variable.
val name: Option[String] = Option("suud")
val age: Option[Int] = Option(25)
val children: Seq[Person] = Seq.empty
val suud: Option[Person] = name map {Person(_, age, children)}
The way that foo is written, it's possible to pass in an empty list of children with a boolean parameter which says the map has children. Writing hasChildren as a method of a case class guards against this, because the boolean method depends on the collection it's meant to provide information about.
If you really insist on using a map here, you can either use a MapBuilder to get an immutable map, or just import and use the mutable map.
import scala.collection.mutable.MapBuilder
val builder: MapBuilder[String, Any, Map[String, Any]] = new MapBuilder(Map.empty)
if (!name.isEmpty) builder += ("name" -> name.get)
if (!age.isEmpty) builder += ("age" -> age.get)
if (!hasChilds.isEmpty) builder += ("hasChilds" -> hasChilds.get)
if (!childs.isEmpty) builder += ("childs" -> childs.get)
builder.result
Now the result of the builder is an immutable map. If you really need a mutable map, you can just:
import scala.collection.mutable
val m = mutable.Map[String, Any]()
Now you've got a mutable map which can be converted to an immutable map with its toMap method.
hasChildsseems redundant if you're using anOptionforchilds.hasJobsinstead :)