I'm used to Ruby. In Ruby, you can use nil with abandon and just do something like if variable to check if it exists. Below is valid Ruby code (though forgive me if I'm now out of practice):

1
2
3
4
5
6
7

defdisplayUser(user)ifuseruser.nameelse"No name"endend

When I started on this project, I started treating Scala the same way. However, I found out that apparently you want to avoid using null in Scala. My first iteration prior to discovering this was the following:

This is not proper Scala. Unlike Ruby, Scala has Option. Option allows you to have Some or None. As you might expect, Some has a value, while None is the equivalent of null. Here's an example of how to do that same function properly:

1
2
3
4
5
6
7
8
9
10
11

defdisplayUser(user:Option[User]=None):String={if(user.isDefined){returnuser.get.name}else{return"No name"}}// call it like thisuser=User()displayUser(Some[user])//ordisplayUser()

Either provides a similar function to Option but is better for returning error messages. The problem I was trying to solve was creating an organization user. For that to happen, there must be an organization and a user. Here's my inital way I did it, thinking as a rubyist:

However, the better option (hehe) is to use Either in this case. So here's the better way to do this same function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

traitValidationError{valmessage:String}caseclassIdentifierNotFound(message:String)extendsValidationErrordefvalidateUserAndOrgExist(name:String,orgId:Long):Either[IdentifierNotFound, (Organization, User)]={valuser=usersService.findByEmail(name).headOptionvalorganization=organizationsService.find(orgId).headOptionif(user.isEmpty){Left(IdentifierNotFound("User does not exist. Please create user first."))}elseif(organization.isEmpty){Left(IdentifierNotFound("Organization does not exist. Please create organization first."))}else{Right((organization.get,user.get))}}// then call it and use it like this:valvalidationOfUserOrg=validateUserAndOrgExist("test",123)if(validationOfUserOrg.isRight){val(organization,user)=validationOfUserOrg.right.getprint("User $user.name is in Organization $organization.id.")}else{print(validationOfUserOrg.left.get.message)}