0

I'm new to scala and I'm trying to get to construct a list from another one which I get from a scala object this is my model:

case class Session(
    _id: Option[String],
    participants: Option[Seq[Participant]])
  case class Participant(
    contact: Contact,
    participantStatus: Option[String])
Contact.scala
  case class Contact(
    firstName: Option[FirstName],
    lastName: Option[LastName],
    address: Option[Address])
Address.scala
  case class Address(
    email: Option[String])

using this loop:

for (s <- session.participants) println(s)

I get :

List(Participant(Contact(Some(FirstName(5m,Some(5),Some(5))),Some(LastName(5,Some(5),Some(5))),Some(Address(None,None,None,None,None,Some(5),Some(5),Some(5),Some([email protected]),None)),None,None),None), Participant(Contact(Some(FirstName(contact1,Some(contact1),Some(contact1))),Some(LastName(contact1,Some(contact1),Some(contact1))),Some(Address(None,None,None,None,None,Some(1),Some(1),Some(1),Some([email protected]),None)),None,None),None))

when I try : println(s.contact) I get : value contact is not a member of Seq[models.Session.Participant]

5
  • could you give us the initialisation of your value session ? I have some code : for(s <- session.participants) s.map(s => println(s.contact)) but I cannot test it :p Commented Jun 21, 2016 at 11:32
  • it's working: Some(Address(None,None,None,None,None,Some(1),Some(1),Some(1),Some(email1@gmail.‌​com),None)) but now how can I access the email? Commented Jun 21, 2016 at 11:46
  • s.contact.address.email should do the trick ? But you should be careful with all those option and do some matching to see if it's there or not (this helped me when I was learning Options : danielwestheide.com/blog/2012/12/19/…) Commented Jun 21, 2016 at 11:50
  • thank you so much, but when using println(s.contact.address.email)) I get the same error: value email is not a member of Seq[models.Address] Commented Jun 21, 2016 at 11:54
  • s.map(s => s.contact.address.map( a => println(a.email))), would that work ? Commented Jun 21, 2016 at 12:34

1 Answer 1

3

Your s variable is getting pulled out from session.participants which has type Option[Seq[Participant]], so you get Seq[Participant]. If you want to loop through your participants, you need a list/seq, so:

val sessionParticipants = session.participants.getOrElse(Seq.empty)
for (s <- sessionParticipants) println(s)
Sign up to request clarification or add additional context in comments.

3 Comments

You might want to change participants: Option[Seq[Participant]] into participants: Seq[Participant] a Option of a Seq is a little strange, since a Seq already has an Nullobject: the empty Seq
yes I see thank you very much for your help, another last thing: when using for (s <- sessionParticipants) println(s.contact.address) I get Some(Address(None,None,None,None,None,Some(1),Some(1),Some(1),Some([email protected]),None)) how can I get the email?
val maybeEmail = s.contact.address.flatMap(_.email) should be Option[String]. Then you can specify a fallback email with getOrElse, or, if you are 100% that every time you will have an email, you can call get on it... ;)

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.