Skip to main content
deleted 45 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Trying to learn scala. Wanted to know if this is the standard way of doing things in Scala - or is there a better way to write this code ?

Basically, this function returns a list of flights for an airline. But if the airline doesn't exist  , I want to throw some custom runtime exceptioncustom runtime exception. If that airline does exist, iI want to return a list of flights (which could potentially be empty).

Is this is the standard way of doing things in Scala, or is there a better way to write this code?

Coming from a Java background, I'm still trying to get to grips with the Scala way of doing things.

def getFlightsForAirline(id:Long):Try[Option[List[Flight]]] = {
     val maybeAirline:Option[Airline] = Airline.getById(id)
     
     maybeAirline match {
       case Some(airline) => {
          Success(Flight.getFlightsByAirline(airline))
       }
       case None => Failure(new RecordNotFoundException("Couldnt find airline"))
     }
   }

Trying to learn scala. Wanted to know if this is the standard way of doing things in Scala - or is there a better way to write this code ?

Basically, this function returns a list of flights for an airline. But if the airline doesn't exist  , I want to throw some custom runtime exception. If that airline does exist, i want to return a list of flights (which could potentially be empty).

Coming from a Java background, I'm still trying to get to grips with the Scala way of doing things.

def getFlightsForAirline(id:Long):Try[Option[List[Flight]]] = {
     val maybeAirline:Option[Airline] = Airline.getById(id)
     
     maybeAirline match {
       case Some(airline) => {
          Success(Flight.getFlightsByAirline(airline))
       }
       case None => Failure(new RecordNotFoundException("Couldnt find airline"))
     }
   }

Basically, this function returns a list of flights for an airline. But if the airline doesn't exist, I want to throw some custom runtime exception. If that airline does exist, I want to return a list of flights (which could potentially be empty).

Is this is the standard way of doing things in Scala, or is there a better way to write this code?

Coming from a Java background, I'm still trying to get to grips with the Scala way of doing things.

def getFlightsForAirline(id:Long):Try[Option[List[Flight]]] = {
     val maybeAirline:Option[Airline] = Airline.getById(id)
     
     maybeAirline match {
       case Some(airline) => {
          Success(Flight.getFlightsByAirline(airline))
       }
       case None => Failure(new RecordNotFoundException("Couldnt find airline"))
     }
   }
edited tags
Link
200_success
  • 145.7k
  • 22
  • 192
  • 481
include beginner tag, format paragraphs to match line-breaks that were not enough to create markups.
Source Link
rolfl
  • 98.2k
  • 17
  • 220
  • 419

Trying to learn scala. Wanted to know if this is the standard way of doing things in Scala - or is there a better way to write this code ? Basically

Basically, this function returns a list of flights for an airline. But if the airline doesn't exist , I want to throw some custom runtime exception. If that airline does exist, i want to return a list of flights (which could potentially be empty). Coming

Coming from a Java background, I'm still trying to get to grips with the Scala way of doing things.

def getFlightsForAirline(id:Long):Try[Option[List[Flight]]] = {
     val maybeAirline:Option[Airline] = Airline.getById(id)
     
     maybeAirline match {
       case Some(airline) => {
          Success(Flight.getFlightsByAirline(airline))
       }
       case None => Failure(new RecordNotFoundException("Couldnt find airline"))
     }
   }

Trying to learn scala. Wanted to know if this is the standard way of doing things in Scala - or is there a better way to write this code ? Basically, this function returns a list of flights for an airline. But if the airline doesn't exist , I want to throw some custom runtime exception. If that airline does exist, i want to return a list of flights (which could potentially be empty). Coming from a Java background, I'm still trying to get to grips with the Scala way of doing things.

def getFlightsForAirline(id:Long):Try[Option[List[Flight]]] = {
     val maybeAirline:Option[Airline] = Airline.getById(id)
     
     maybeAirline match {
       case Some(airline) => {
          Success(Flight.getFlightsByAirline(airline))
       }
       case None => Failure(new RecordNotFoundException("Couldnt find airline"))
     }
   }

Trying to learn scala. Wanted to know if this is the standard way of doing things in Scala - or is there a better way to write this code ?

Basically, this function returns a list of flights for an airline. But if the airline doesn't exist , I want to throw some custom runtime exception. If that airline does exist, i want to return a list of flights (which could potentially be empty).

Coming from a Java background, I'm still trying to get to grips with the Scala way of doing things.

def getFlightsForAirline(id:Long):Try[Option[List[Flight]]] = {
     val maybeAirline:Option[Airline] = Airline.getById(id)
     
     maybeAirline match {
       case Some(airline) => {
          Success(Flight.getFlightsByAirline(airline))
       }
       case None => Failure(new RecordNotFoundException("Couldnt find airline"))
     }
   }
Source Link
smk
  • 371
  • 3
  • 8
Loading