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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:55:27+00:00 2026-06-05T02:55:27+00:00

I would like to do the following: public class ImmutableList<T> { public <U super

  • 0

I would like to do the following:

public class ImmutableList<T> {
  public <U super T> ImmutableList<U> add(U element) { ... }
}

That is, given an immutable list of T, you can add any U to the list to yield an immutable list of U, with the constraint that U must be a supertype of T. For example

  • I can add a monkey to a list of monkeys, yielding a new list of monkeys;
  • I can add a human to a list of monkeys, yielding a new list of hominids (presumably the least upper bound of monkey and human);
  • I can add a rock to a list of hominids, yielding a new list of Object (assuming rocks and hominids share no other common ancestor).

This sounds great in theory, but the lower bound on U is not legal per the JLS. I could instead write:

public class ImmutableList<T> {
  public ImmutableList<T> add(T element) { ... }
  public static <U> ImmutableList<U> add(ImmutableList<? extends U> list, U element) { ... }
}

In this fashion, the compiler will correctly infer the least upper bound between the list’s element type and the U we want to add. This is legal. It also sucks. Compare:

// if 'U super T' were legal
list.add(monkey).add(human).add(rock);

// assuming 'import static ImmutableList.add'
add(add(add(list, monkey), human), rock);

I’m a big fan of functional programming, but I don’t want my Java code to look like a Lisp dialect. So I have three questions:

  1. WTF? Why is the <U super T> bound not legal? This question has actually been asked here before (“Why can’t a Java type parameter have a lower bound?”) but I think the question as phrased there is sort of muddled, and the answers there boil down to “it’s not useful enough,” which I don’t really buy.

  2. JLS section 4.5.1 states: “Unlike ordinary type variables declared in a method signature, no type inference is required when using a wildcard. Consequently, it is permissible to declare lower bounds on a wildcard.” Given the alternative static method signature above, the compiler is clearly able to infer the least upper bound it needs, so the argument seems broken. Can somebody argue that it is not?

  3. Most importantly: Can anybody think of a legal alternative to my method signature with the lower bound? The goal, in a nutshell, is to have calling code that looks like Java (list.add(monkey)) and not Lisp (add(list, monkey)).

  • 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-05T02:55:30+00:00Added an answer on June 5, 2026 at 2:55 am

    You can almost do it, but Java type inference manages to suck just enough to make it not worth it.

    public abstract class ImmutableList< T > {
        public interface Converter< U, V > {
            V convert( U u );
        }
    
        public abstract < U > ImmutableList< U > map( Converter< ? super T, ? extends U > cnv );
    
        public static class EmptyIL< T > extends ImmutableList< T >{
            @Override
            public < U > EmptyIL< U > map( Converter< ? super T, ? extends U > cnv ) {
                return new EmptyIL< U >();
            }
        }
    
        public static class NonEmptyIL< T > extends ImmutableList< T > {
            private final T tHead;
            private final ImmutableList< ? extends T > ltTail;
            public NonEmptyIL( T tHead, ImmutableList< ? extends T > ltTail ) {
                this.tHead = tHead;
                this.ltTail = ltTail;
            }
            @Override
            public < U > NonEmptyIL< U > map( Converter< ? super T, ? extends U > cnv ) {
                return new NonEmptyIL< U >( cnv.convert( tHead ), ltTail.map( cnv ) );
            }
        }
    
        public < U > ImmutableList< U > add( U u, final Converter< ? super T, ? extends U > cnv ) {
            return new NonEmptyIL< U >( u, map( cnv ) );
        }
    
        public static < V > Converter< V, V > id() {
            return new Converter< V, V >() {
                @Override public V convert( V u ) {
                    return u;
                }
            };
        }
    
        public static < W, U extends W, V extends W > Converter< W, W > sup( U u, V v ) {
            return id();
        }
    
        static class Rock {}
        static class Hominid {}
        static class Human extends Hominid {}
        static class Monkey extends Hominid {}
        static class Chimpanzee extends Monkey {}
    
        public static void main( String[] args ) {
            Monkey monkey = new Monkey();
            Human human = new Human();
            Rock rock = new Rock();
    
            // id() should suffice, but doesn't
            new EmptyIL< Chimpanzee >().
                add( monkey, ImmutableList.< Monkey >id() ).
                add( human, ImmutableList.< Hominid >id() ).
                add( rock, ImmutableList.< Object >id() );
    
            // sup() finds the supremum of the two arguments' types and creates an identity conversion
            // but we have to remember what we last added
            new EmptyIL< Chimpanzee >().
                add( monkey, sup( monkey, monkey ) ).
                add( human, sup( monkey, human ) ). // add( human, sup( monkey, monkey ) ) also works
                add( rock, sup( human, rock ) );
        }
    }
    

    You at least get compile-time enforcement of convertibility between types, and as an added bonus you can define your own converters that go beyond just subclassing. But you can’t have Java figure out that in the absence of a converter it should use the appropriate subclassing converter as a default, which would bring us to your original API.

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

Sidebar

Related Questions

I have the following: public class InstanceList : List<Instance> {} I would like to
So I have a class that looks something like the following: public class MyClass
I have a class that is something like the following: public class Foo {
I would like to use NHibernate Automapper to map the following class: public class
I have the following class: public class ContentService : IContentService I would like to
I would like to do the following: [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct SomeStruct
With code like the following public class Task { string Name; public static bool
Is it possible to do something like the following: public class ChildClass : BaseClass
I have an enum class like the following: public enum Letter { OMEGA_LETTER(Omega), GAMMA_LETTER(Gamma),
I have a class with primitive fields like the following: public class Person{ String

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.