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

  • Home
  • SEARCH
  • 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 9068481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:12:12+00:00 2026-06-16T17:12:12+00:00

I would like to have higher-order-function Function[K=>V, K=>V] which takes a function K=>V and

  • 0

I would like to have higher-order-function Function[K=>V, K=>V] which takes a function K=>V and returns the type of the given function. For example I would like to have the following behaviour:

  class Foo[K, V]() {
    def apply[K1 <: K, V1 <: V](f: K1 => V1) = f
  }

  // dummy class hierachy
  class A
  class B extends A
  class C extends B

    // a function f: B=>B
  def f(some: B): B = some

  // the desired result
  val result1: B => B = new Foo[A, A]()(f)

The apply method of Foo takes a B=>B and returns a B=>B. The type-parameters K and V keep track of the “highest” type Foo can take as an argument. Now, I would like Foo to extend Function like

  class Bar[K, V] extends Function[K=>V, K=>V]() {
    def apply(f: K => V) = f
  }

  val result2: B => B = new Bar[A, A]()(f)

however this does obviously not work. Is there a way to make this work?
Thanks

Edit

  class Fuzz[K, V, K1 <: K, V1 <: V] extends Function[K1=>V1, K1=>V1] {
    def apply(f: K1 => V1) = f
  }

  val result3: B => B = new Fuzz[A, A, B, B]()(f) 

Also works, however I don’t wanna carry the two additional type-parameters

  • 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-16T17:12:14+00:00Added an answer on June 16, 2026 at 5:12 pm

    This cannot and should not work, because A => A is not a subtype of B => B (and not a supertype either). The reason for that is that Function1 is covariant in its argument type and contravariant in its result type. Thus for A => A to be a subtype of B => B, A would have to be a subtype as well as a supertype of B. That is only the case if A and B are in fact the same type. See the tour of Scala for a more in depth explanation of variance.

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

Sidebar

Related Questions

I would like to know how I solve the following problem using higher order
I would like to have a field which is updated on every change(insertion, modification),
I would like to have a regular expression which clears an HTML field when
I have the following model class Plugin(models.Model): name = models.CharField(max_length=50) # more fields which
Let's say I have a higher order function f :: (a -> b) ->
Desired: I'd like to be have my git commits reference some higher order/level task
So in my project i would like have a nice treeview that has images.
I would like to have an AppWidget that designed like this one . Image:
I would like to have a more colorful Python prompt in the terminal, just
I would like to have the cursor in the JavaDoc area when creating interfaces,

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.