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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:05:01+00:00 2026-06-06T20:05:01+00:00

The ListNode example , taken from Scalas homepage goes like this: case class ListNode[+T](h:

  • 0

The ListNode example, taken from Scalas homepage goes like this:

case class ListNode[+T](h: T, t: ListNode[T]) {
  def head: T = h
  def tail: ListNode[T] = t
  def prepend[U >: T](elem: U): ListNode[U] =
    ListNode(elem, this)
}

With this class, we can create objects like:

val empty: ListNode[Null] = ListNode(null, null)
val strList: ListNode[String] = empty.prepend("hello")
                                     .prepend("world")
val anyList: ListNode[Any] = strList.prepend(12345)

As we can see, we can prepend an integer value to a String node. I guess, this works because the type parameter U will be automatically set to Any, when given the integer to the prepend method (because Int is not a supertype of String).

When trying this with an own lower bound example, I will get an error:

scala> class E[T >: String]
defined class E

scala> new E[Any]
res1: E[Any] = E@135f0a

scala> new E[Int]
<console>:11: error: type arguments [Int] do not conform to class E's type param
eter bounds [T >: String]
      val res2 =
          ^
<console>:12: error: type arguments [Int] do not conform to class E's type param
eter bounds [T >: String]
              new E[Int]
                  ^

Why is the type Int here not automatically seen as type Any like in the ListNode example ?


UPDATE 1: This also works (without explicitley saying the new ListNode should be of type Any)

scala> val empty: ListNode[Null] = ListNode(null, null)
empty: example.listNode.ListNode[Null] = ListNode(null,null)

scala> empty.prepend("hello").prepend("world")
res0: example.listNode.ListNode[java.lang.String] = ListNode(world,ListNode(hell
o,ListNode(null,null)))

scala> val strList: ListNode[String] = empty.prepend("hello").prepend("world")
strList: example.listNode.ListNode[String] = ListNode(world,ListNode(hello,ListN
ode(null,null)))

scala> strList.prepend(12345)
res1: example.listNode.ListNode[Any] = ListNode(12345,ListNode(world,ListNode(he
llo,ListNode(null,null))))
  • 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-06T20:05:03+00:00Added an answer on June 6, 2026 at 8:05 pm

    You get the above error because Int is not a supertype of String.

    Note that in the ListNode code above, String is a supertype of Null (see class hierarchy), and Any is a supertype of String (as you rightly pointed out).

    I would guess the confusions is caused by comparing two operations that are not really the same: new E[Int] instantiates the class with the type parameter Int, which is not conformant to the lower bound String and hence fails.

    In the ListNode code above, on the other hand, you call the prepend method that takes a supertype U of T. When creating anyList, U is (just as you guessed) resolved to Any since this is the only common supertype of String and Int, so you could think of it as not really passing an Int to it, but just some arbitrary instance of Any (which also happens to be of type Int).

    Therefore,

    val anyList: ListNode[Int] = strList.prepend(12345)
    

    fails as well, as strList.prepend(12345) can only return ListNode[Any].

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

Sidebar

Related Questions

class List { ListNode *head; ListNode *prev; }; class ListNode { int data; ListNode
Is it possible to implement a bi-directional tree in a case class. This seems
I'm trying to understand this code from Deitels 5th edition c book. If I'm
#include<iostream> using namespace std; class TCSGraph{ public: void addVertex(int vertex); void display(); TCSGraph(){ head
Here is my code.... package calendar; import java.util.*; /** * This class represents a
Example of my XML list: <listnode> <nodeA id=1> <nodeB id=1 /> </nodeA> <nodeA id=2>
I have this function: template <typename T> List<T>::ListNode *List<T>::find(int index) const { if (
Write a class ListNode which has the following properties: int value; ListNode *next; Provide
int findLargest (ListNode *p) // -------------------------------------------------------------------------- // Preconditions: list head pointer is passed as
class ListNode { public object Data { get; private set; } public ListNode Next

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.