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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:06:48+00:00 2026-05-24T09:06:48+00:00

To illustrate my point, here an example: abstract class Wrapper[A](wrapped: A) { protected def

  • 0

To illustrate my point, here an example:

abstract class Wrapper[A](wrapped: A) {

  protected def someCondition: Boolean

  def fold[B](whenTrue: => B)(whenFalse: => B): B =
    if (someCondition) whenTrue else whenFalse

}

I’m trying to add a fold method based on an arbitrary condition defined on a wrapped type A. The problem with the code above is that this wouldn’t compile, although it could conceivably return Any:

wrapper.fold("hi")(42)

because by the time the compiler reaches the second parameter list, B has already been inferred to be String. Suppose we don’t want to have to write the type annotation. We can try changing fold to this:

def fold[B, B0 >: B](whenTrue: => B)(whenFalse: => B0): B0

but this also doesn’t work, since B0 has already been resolved as String at the end of the first parameter list, although it doesn’t appear in it at all! The simple solution, of course, is to have a single parameter list, but f the sake of the example, let’s say I want to keep the two parameter lists and try to make it work… Ideally, we should be able to delay the resolution of B0. It would be great if we could write something like this:

def fold[B](whenTrue: => B)[B0 >: B](whenFalse: => B0): B0

But unfortunately this doesn’t work. Are there any workarounds?

(I’m providing a first answer, but I’m of course looking for other workarounds as well.)

  • 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-24T09:06:50+00:00Added an answer on May 24, 2026 at 9:06 am

    One solution is to create a temporary instance of a class that defines an apply method simulating the second parameter list, which has itself the B0 type parameter:

    abstract class Wrapper[A](wrapped: A) {
    
      // ... as before...
    
      def fold[B](whenTrue: => B) = new FoldRequest[B](whenTrue)
    
      class FoldRequest[B](whenTrue: => B) {
        def apply[B0 >: B](whenFalse: => B0) =
          if (someCondition) whenTrue else whenFalse
      }
    }
    

    Then everything works correctly. Could we even imagine that def fold[B](whenTrue: => B)[B0 >: B](whenFalse: => B0): B0 could be interpreted as syntactic sugar for this?…

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

Sidebar

Related Questions

To illustrate the question check the following code: class MyDescriptor(object): def __get__(self, obj, type=None):
To illustrate what I mean by this, here is an example messages = [
Intro Here is an example to illustrate the problem. Consider I am tracking and
I guess I'll illustrate with an example: In this game you are able to
I'll attempt to illustrate this with an example. Take a common example of a
To illustrate my problem, TEST=Hi my name is John OUTP=`echo $TEST | awk '{print
To illustrate, assume that I have two tables as follows: VehicleID Name 1 Chuck
Can someone illustrate what really is the difference between the two?
I'm trying to illustrate a doubly linked list problem. this is from an old
How can you clearly illustrate multiple threads of execution in a sequence diagram or

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.