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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:50:43+00:00 2026-06-17T22:50:43+00:00

I’m attempting to use abstract typing to simplify and clarify type handling, but I

  • 0

I’m attempting to use abstract typing to simplify and clarify type handling, but I keep encountering seemingly non-sensical errors like these:

trait Delta[A] {
  def apply(c: A)
}

abstract class ValueHolder {

  type Value
  type Event <: Delta[ValueHolder]

  def update(next: Value): Event = new UpdateEvent(next)
  // type mismatch;  found   : UpdateEvent[ValueHolder]
  // required: ValueHolder.this.Event
}

class UpdateEvent[C <: ValueHolder](next: C#Value) extends Delta[C] {

  def apply(c: C) = c.update(next)
  // type mismatch;  found   :
  // UpdateEvent.this.next.type (with underlying type C#Value)
  // required: c.Value
}
  1. Doesn’t Delta[C], where C <: ValueHolder, thus conform to Event <: Delta[ValueHolder]?

  2. Likewise, given that c is a C, isn’t c.Value a C#Value?

I can use a cast to remove the second error, but that defeats the point of using types.

I tried to incorporate the answer suggested in [this related question][1]…

class UpdateEvent[C <: ValueHolder, V <: C#Value](next: V) extends Delta[C] {

… which, sadly, fails to alleviate either problem (although it requires a few more type parameters when called from update()).

Help???


Update: Unfortunately, the example I gave above was a bit oversimplified. I’m trying to propagate changes to classes having the same method signatures (though possibly different type parameters), which thus act as “views” of the original.

For example, imagine you could run this:

(ListBuffer[Int]:_).map(_.toString)

… and then have the resulting ListBuffer[String] updated every time the original is. (Without just running “map” over and over, for reasons I can’t explain briefly.) As with this tiny example, others define the traits being implemented, meaning I can’t change the method signatures to work around the problem.

(NB: I also can’t get rid of type Event because there’s a variable (not illustrated here) containing all the listeners who receive each Event — and the type of that should be refined by subclasses to allow more specific kinds of listeners for each.)

Anyway, after much time pondering the not-very-explanatory Scala Reference manual (the information is all there, but it assumes you know a lot already), I finally figured out how to constrain UpdateEvent so that C and C#Value correspond:

class UpdateEvent[V, C <: ValueHolder { type Value = V }](
  next: V) extends Delta[C] { ... }

This fixes both compilation errors and preserves the existing approach. But I’m marking Peter’s answer (below) as correct (giving him the reputation points) because I so very much appreciate his spending time on it. Thanks, Peter, and best to you.

  • 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-17T22:50:44+00:00Added an answer on June 17, 2026 at 10:50 pm

    Doesn’t Delta[C], where C <: ValueHolder, thus conform to Event <: Delta[ValueHolder]?

    I am not entirely sure about this, so take the following as a theory. The compiler could only ensure that Delta[C], where C <: ValueHolder, is-a Delta[ValueHolder], if the type parameter of Delta were covariant (that is, Delta[+A]). But currently it is invariant, so the above is not true. Neither can the compiler know whether a Delta[C] is an Event: Event is an abstract type, i.e. a placeholder for some type to be defined later, which will be a subtype of Delta[ValueHolder]. However, it can be defined as any such subclass, not just UpdateEvent! So I may define a ValueHolder subclass with an Event type of OtherEvent, which is thus not an UpdateEvent. If the compiler allowed this, the eventual result would be a runtime error.

    Likewise, given that c is a C, isn’t c.Value a C#Value?

    Indeed it is. But look at the error message:

      // type mismatch;  found   :
      // UpdateEvent.this.next.type (with underlying type C#Value)
      // required: c.Value
    

    That is, the compiler needs a type of c.Value and it got a C#Value instead. And C#Value is not a c.Value – the former is the more general type!

    Part of a potential solution may be to make update parameterized with a bound type parameter C <: ValueHolder, and then use C#Value as the parameter type, instead of ValueHolder#Value. This would eliminate the second error. A solution for the first problem may be to replace the return type of Event with Delta[C]. Thus the following compiles:

    trait Delta[A] {
      def apply(c: A): Delta[A]
    }
    
    abstract class ValueHolder {
    
      type Value
    
      def update[C <: ValueHolder](next: C#Value): Delta[C] = new UpdateEvent(next)
    }
    
    class UpdateEvent[C <: ValueHolder](next: C#Value) extends Delta[C] {
    
      override def apply(c: C) = c.update(next)
    }
    

    Notes:

    • in this scheme, Event is actually no more used, so it can be completely removed
    • I defined the return type of Delta.apply as Delta[A], to comply with the type actually returned by UpdateEvent.apply
    • due to this, the type parameter A could only be invariant here, because A now stands both in a covariant (as a method return type) and a contravariant (as a method parameter) position within Delta.

    Hope this helps – I am not sure what you are trying to achieve though, so I may have slaughtered your original idea with these modifications 🙂

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

Sidebar

Related Questions

Seemingly simple, but I cannot find anything relevant on the web. What is the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am confused How to use looping for Json response Array in another Array.
I am trying to understand how to use SyndicationItem to display feed which is
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.

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.