Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Nullwerte oder Optionen verwenden > Optionen und For-Comprehensions

Optionen und For-Comprehensions

Wenn man Option zusammen mit der for-Comprehension verwendet, hat dies noch einen weiteren Vorteil, denn in den meisten Fällen werden None-Elemente aus den Comprehensions automatisch entfernt (vgl. [Pollak2007] und [Spiewak2009c]). Betrachten Sie diese erste Version eines Skripts, das Option-Werte in einer for-Comprehension verwendet:

// code-examples/AppDesign/options-nulls/option-for-comp-v1-script.scala

case class User(userName: String, name: String, email: String, bio: String)

val newUserProfiles = List(
  Map("userName" -> "twitspam", "name" -> "Twit Spam"),
  Map("userName" -> "bucktrends", "name" -> "Buck Trends",
      "email" -> "thebuck@stops.he.re", "bio" -> "Weltgrößter Schwafler"),
  Map("userName" -> "lonelygurl", "name" -> "Lonely Gurl", 
      "bio" -> "Obviously fake..."),
  Map("userName" -> "deanwampler", "name" -> "Dean Wampler", 
      "email" -> "dean@....com", "bio" -> "Scala-Passionista"),
  Map("userName" -> "al3x", "name" -> "Alex Payne", 
      "email" -> "al3x@....com", "bio" -> "Twitter-API-Genius"))


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free 10-Day Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint