![]() Just add if after the pattern.ĭef showImportantNotification ( notification : Notification, importantPeopleInfo : Seq ) : String = val importantPeopleInfo = Seq ( "867-5309", ) val someSms = SMS ( "123-4567", "Are you there?" ) val someVoiceRecording = VoiceRecording ( "Tom", "/id/123" ) val importantEmail = Email (, "Drinks tonight?", "I'm free after 5!" ) val importantSms = SMS ( "867-5309", "I'm here! Where are you?" ) println ( showImportantNotification ( someSms, importantPeopleInfo )) // prints You got an SMS from 123-4567! Message: Are you there? println ( showImportantNotification ( someVoiceRecording, importantPeopleInfo )) // prints You received a Voice Recording from Tom! Click the link to hear it: /id/123 println ( showImportantNotification ( importantEmail, importantPeopleInfo )) // prints You got an email from special someone! println ( showImportantNotification ( importantSms, importantPeopleInfo )) // prints You got an SMS from special someone!ĭef showImportantNotification ( notification : Notification, importantPeopleInfo : Seq ) : String = notification match case Email ( sender, _, _ ) if importantPeopleInfo. Pattern guards are boolean expressions which are used to make cases more specific. In the case Email(sender, title, _) the fields sender and title are used in the return value but the body field is ignored with _. it figures out whether it’s an Email, SMS, or VoiceRecording). ![]() The function showNotification takes as a parameter the abstract type Notification and matches on the type of Notification (i.e. Def showNotification ( notification : Notification ) : String = notification match case Email ( sender, title, _ ) => s "You got an email from $sender with title: $title" case SMS ( number, message ) => s "You got an SMS from $number! Message: $message" case VoiceRecording ( name, link ) => s "You received a Voice Recording from $name! Click the link to hear it: $link" val someSms = SMS ( "12345", "Are you there?" ) val someVoiceRecording = VoiceRecording ( "Tom", "/id/123" ) println ( showNotification ( someSms )) // prints You got an SMS from 12345! Message: Are you there? println ( showNotification ( someVoiceRecording )) // prints You received a Voice Recording from Tom! Click the link to hear it: /id/123 ![]()
0 Comments
Leave a Reply. |