Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8005405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:13:20+00:00 2026-06-04T17:13:20+00:00

How can I archive a kind of Option type which either returns something of

  • 0

How can I archive a kind of Option type which either returns something of type T or of type Error?

I am doing some web requests and the responses are either “ok” and contain the object or the call returns an error, in which case I want to provide an Error object with the reason of error.

So something like:

def myRequest() : Result[MyObject] {
  if (thereWasAnError) Error(reason) else MyObject 
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T17:13:21+00:00Added an answer on June 4, 2026 at 5:13 pm

    scala.Either

    Either type should be exactly what you want:

    def myRequest() : Either[String, MyObject] = 
        if (thereWasAnError) 
            Left("Some error message") 
        else 
            Right(new MyObject)
    

    Either is similar to Option but can hold one out of two possible values: left or right. By convention right is OK while left is an error.

    You can now do some fancy pattern matching:

    myRequest() match {
        case Right(myObject) =>
            //...
        case Left(errorMsg) =>
            //...
    }
    

    scala.Option.toRight()

    Similarily you can use Option and translate it to Either. Since typically *right* values is used for success and left for failure, I suggest usingtoRight()rather thantoLeft()`:

    def myRequest() : Option[MyObject] =
        if (thereWasAnError)
            None
        else
            Some(new MyObject)
    
    val result: Either[String, MyObject] = myRequest() toRight "Some error message"
    

    However returning Either directly as a result of myRequest() seems more straightforward in this simple example.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my web application, users can download a .tar.gz archive containing the app files.
I have some image-files stored into one file (some kind of archive). That file
In My wordpress blog I can get all the archive list in monthly view.
I'm creating an archive of news articles in a sharepoint publishing site. users can
I can execute this kind of query in SQL Server: update tblmembers set hits_archived
Can a post be hidden from home page, archive view, category lists etc. and
I have an archive object which manages various byte arrays and hands out InputStream
I would like some kind of delete/copy/move/etc Windows commands that completely ignores if a
I don't know if I just have some kind of blind spot or what,
I've got an archive file containing multiple audio files in .mp3 format. I can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.