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 6566029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:10:05+00:00 2026-05-25T14:10:05+00:00

Suppose I’m writing a GUI class Kitteh (val age: Int) { require (age <

  • 0

Suppose I’m writing a GUI

enter image description here

class Kitteh (val age: Int) {
  require (age < 5)
  def saveMeow(file: File) = { /* implementation */ }
  def savePurr(file: File) = { /* implementation */ }
}

The frame has a field for the current Kitteh, which is an Option because it might not have been defined yet, or the user may have attempted to create an invalid one:

var currentKitteh: Option[Kitteh] = None

Now I want to create a Kitteh safely when the user hits Create

val a = ... // parse age from text box
currentKitteh = try { Some(new Kitteh(a)) } catch { case _ => None }

My GUI has two buttons which do similar things. In psedocode, they should both

if (currentKitteh.isDefined) {
  if (file on the disk already exists) {
    bring up a dialog box asking for confirmation
    if (user confirms)
       << execute method on currentKitteh >>
  }
}
else bring up warning dialog

Don’t worry about the detail: the point is that because there is code duplication, I want to create a common method that I can call from both buttons. The only difference is the method on the Kitteh that needs to be executed.

Now if currentKitteh were not an Option, the common method could have a signature like

def save(filename: String, f:(File => Unit)) {

which I could call with, for example

save("meow.txt", currentKitteh.saveMeow _)

but since it is actually an Option, how could I implement this?

I could just check whether currentKitteh is defined, and do a .get before calling the save method for each button, but is there another way, leaving this check in the save method? In other words, given an Option[A], is it possible to specify a partial function from a method on the (possibly non-existent) A object?

(hope this question makes sense, convoluted example notwithstanding)

edit: Bonus question: what if, instead of Option[Kitteh], I used Either[Throwable, Kitteh]?

update: Additional line added to pseudocode to bring up warning dialog: ideally, the save method should always be called so that the user is warned if there is no valid Kitteh to save.

  • 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-05-25T14:10:06+00:00Added an answer on May 25, 2026 at 2:10 pm

    This looks like the best option to me:

    currentKitteh foreach { c => save("meow.txt", c.saveMeow _) }
    

    If you’re repeatedly doing this, you can abstract it,

    def currentSaveMeow(file: String) = currentKitteh foreach { c =>
      save(file, c.saveMeow _)
    }
    currentSaveMeow("meow.txt")
    

    I suppose to answer your original question, you could also push the logic into the function argument,

    save("meow.txt", file => currentKitten.foreach(_.saveMeow(file)))
    

    The semantics are a little different with this version.

    Update. If k: Option[Kitteh] is replaced by k: Either[Throwable, Kitteh], then what about k.right foreach { c => ... }? You could also use k.right map ... if you want to preserve error information.


    In response to the modified question, here’s another abstraction possibility,

    def save(filename: String, f: (Kitteh, File) => Unit)
    

    Now save has the responsibility of unpacking currentKitteh. Call save like this,

    save("meow.txt", (k, f) => k.saveMeow(f))
    

    or like this,

    save("meow.txt", _ saveMeow _)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have this code: class Num: def __init__(self,num): self.n = num def getn(self):
Suppose I have a class module clsMyClass with an object as a member variable.
Suppose we have the Lyric model: class Lyric < ActiveRecord::Base belongs_to :song end and
Suppose I have a grid of squared defined like so in a class: Square
Suppose you create a class names Person using the builder pattern, and suppose the
Suppose the following constructor: class Needed { public: Needed () {} Needed (const char
Suppose I have 2 pointers: int *a = something; int *b = something; If
Suppose I have a class MyClass to which I want to add certain 'observer'
Suppose: struct P { P(int v); }; int v; P p = 0; //
Suppose I have a FruitDetector class which takes in a string and returns the

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.