Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6658004

Add a cast() method to Object

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Won't Fix
    • Icon: P5 P5
    • None
    • 7
    • core-libs
    • x86
    • windows_xp

      A DESCRIPTION OF THE REQUEST :
      Although generics have reduced the need for casting there are still occasions where you need to do it. A cast method that uses type argument inference would make casting much easier.

      So if this method existed in Object :

         @SuppressWarnings("unchecked")
         public <T> T cast()
         {
            return (T)this;
         }

      then most casts would be simplified to

      // very contrived example
      Object o = new MyType();

      MyType myObject = o.cast();

      instead of the current

      MyType myObject = (MyType)o;



      JUSTIFICATION :
      This means less code to type and read. That has to be good!

            darcy Joe Darcy
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: