Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Should Java have a "this" type parameter?

  1. #1
    Junior Member
    Join Date
    Aug 2022
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Should Java have a "this" type parameter?

    It could look something like this:

    public interface Vehicle {
        public <T:this> T copy();
    }
    public class Car implements Vehicle {
        public <T:this> T copy() { ... }
    }
    public class Boat implements Vehicle {
        public <T:this> T copy() { ... }
    }

    As it is now, you'll have to either declare the return type as Vehicle, so you have to cast your copy (Car copy = (Car) car.copy();), or use non-interface copy methods so that copying a Vehicle becomes a PITA (Vehicle copy = vehicle instanceof Car car ? car.copy() : vehicle instanceof Boat boat ? boat.copy() : ...).

    I see no reason why you couldn't have the best of both worlds: if you have a Vehicle, you know it will have a copy method which returns a type implementing Vehicle, and if you have a Car, you know the return type will be Car (or a subclass of Car).

  2. #2
    Junior Member
    Join Date
    Jun 2022
    Location
    canada
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Should Java have a "this" type parameter?

    a class can use the this keyword to refer to itself. The this keyword is also required when a local variable within a method has the same name as a class member variable, to distinguish them both.

  3. #3
    Junior Member
    Join Date
    Aug 2022
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Should Java have a "this" type parameter?

    I don't see how my suggested use of this within the type parameter declaration conflicts with any current use of it. But some other keyword could be used, or some other notation entirely.

  4. #4
    Junior Member
    Join Date
    Feb 2023
    Location
    Hungary
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Should Java have a "this" type parameter?

    Hi,
    The interface definition is incorrect IMHO. It should look like this.
    public interface Vehicle<T> {
            public T copy();
        }
     
        public class Car implements Vehicle<Car> {
     
            @Override
            public Car copy() {
                return null;
            }
        }
     
        public class Boat implements Vehicle<Boat> {
     
            @Override
            public Boat copy() {
                return null;
            }
        }

    But it is even better if you separate the interfaces.

    public interface Vehicle {}
     
    public interface Copyable<T extends Vehicle>{
        T copy();
    }
     
    public class Car implements Vehicle, Copyable<Car> {
     
        @Override
        public Car copy() {
            return null;
        }
    }
     
    public class Boat implements Vehicle, Copyable<Boat> {
     
        @Override
        public Boat copy() {
            return null;
        }
    }

    Then you can use
    Car c = new Car()
    Vehicle v = c.copy()

Similar Threads

  1. [SOLVED] Can I build "my own type of java number"?
    By grogger in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 23rd, 2022, 05:05 AM
  2. Replies: 1
    Last Post: August 20th, 2019, 07:07 AM
  3. Replies: 4
    Last Post: July 18th, 2014, 02:04 AM
  4. Replies: 1
    Last Post: July 16th, 2014, 04:16 AM
  5. Replies: 2
    Last Post: June 22nd, 2013, 10:30 AM

Tags for this Thread