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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:57:37+00:00 2026-06-07T03:57:37+00:00

scala> class A { type T <: String; def f(a: T) = println(foo)} defined

  • 0
scala> class A { type T <: String; def f(a: T) = println("foo")}
defined class A

scala> (new A).f("bar")
<console>:9: error: type mismatch;
found   : java.lang.String("bar")
required: _1.T where val _1: A
             (new A).f("bar")
                       ^

Class A has an abstract type T, but is not an abstract class. Creating an object of A (as shown) does not define type T.

My first thought was, I am allowed to pass any type as T which is a subclass of String, but I am not. So what type actually is T in the object and what am I allowed to pass through ?

  • 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-07T03:57:38+00:00Added an answer on June 7, 2026 at 3:57 am

    As you say, T in A is abstract; therefore you won’t find any value you can put into method f, until you have a subtype of A which actually fixes T.

    (new A { type T = String }).f("bar")
    

    The idea is that the type can be successively refined:

    trait Var[A] { def get: A; def set(v: A): Unit }
    
    trait Sys {
      type V[A] <: Var[A]
    
      def swap[A](x: V[A], y: V[A]): Unit = {
        val tmp = x.get
        x.set(y.get)
        y.set(tmp)
      }
    }
    
    trait HistVar[A] extends Var[A] { def created: java.util.Date }
    
    trait HistSys extends Sys {
      type V[A] <: HistVar[A]
    
      def newest[A](x: V[A], y: V[A]): A =
        if (x.created after y.created) x.get else y.get
    }
    

    But of course you’re question is good — there is no reason why you would want a concrete instantiation of a class whose type parameter is not fixed. I can’t think of a case where that makes sense. (Well of course, you could still have functionality that is accessible, if it does not involve type T)


    Further searching finds the following, quasi duplicate, SO question. You can find there a reference to a Scala ticket which outlines it as a ‘feature’ — still doesn’t show a case where this ‘feature’ is actually useful 🙂

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

Sidebar

Related Questions

Foo.java public class Foo{ public int i = 0; } Bar.scala class Bar() extends
scala> class A (s: String*) { val l: ListBuffer[String] = ListBuffer[String](s) } <console>:8: error:
I had a Scala class as follows: class ClassA(val name: String) { println(this is
In Scala, I'd like to do: class Identifier(val str: String) { override def toString():
I have something like this in scala: abstract class Point[Type](n: String){ val name =
scala> class A defined class A scala> trait B defined trait B Creating an
In Scala IDE I get the following error about the class I am to
import java.util.Random class Kostka { val rand = new Random(System.currentTimeMillis()) val value: List[Int] =
I have this class in Scala: object Util { class Tapper[A](tapMe: A) { def
I have this case class define: class Protocol(protocol:String) object Protocol { def apply(protocol:String) :Protocol

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.