Results 1 to 17 of 17
Like Tree1Likes
  • 1 Post By gbritton

Thread: Stupid noob question: Why not have type inference in Java?

  1. #1
    gbritton is offline Member
    Join Date
    Jul 2013
    Posts
    11
    Rep Power
    0

    Default Stupid noob question: Why not have type inference in Java?

    Say I have this line of code:

    Java Code:
    MySpecialGenericType<MyActualType> myvar = new MySpecialGenericType<MyActualType> ();
    My stupid newbie question is:

    Why doesn't Java figure out the type of myvar from the assignment, so that I could shorten this to:

    Java Code:
    myvar = new MySpecialGenericType<MyActualType> ();
    That is, if myvar is not yet defined in the current scope, could the compiler not declare it implicitly and infer the type from the assignment that follows?

    If such a feature were in the language, what are the likely problems/pitfalls that would arise?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default Re: Stupid noob question: Why not have type inference in Java?

    Java Code:
    public class SomeClass {
        myAttribute;  // what type is myAttribute?
    
       public void someMethod() {
          myAttribute = new SomeOtherClass(); // Is this the same type as the myAttribute above or is this supposed to be simply a local variable?
       }
    }
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Stupid noob question: Why not have type inference in Java?

    Well I'm not one of the compiler designers so I can't answer that. Its never as simple as it being technically possible though - it probably is and has been for a long time. It would also have been possible to add unsigned types to the language, and multiple inheritance or operator overloading. But the decision was made to not do that. I am not going to question any such decisions, because the people who make them have delivered an awesome platform with a highly optimized compiler and runtime.

    Note that you can shorten it to this:

    Java Code:
    MySpecialGenericType<MyActualType> myvar = new MySpecialGenericType<> ();
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    gbritton is offline Member
    Join Date
    Jul 2013
    Posts
    11
    Rep Power
    0

    Default Re: Stupid noob question: Why not have type inference in Java?

    Quote Originally Posted by Tolls View Post
    Java Code:
    public class SomeClass {
        myAttribute;  // what type is myAttribute?
    
       public void someMethod() {
          myAttribute = new SomeOtherClass(); // Is this the same type as the myAttribute above or is this supposed to be simply a local variable?
       }
    }
    Two options:

    1. The first reference to myAttribute should be illegal. That is, it is neither explicitly nor implicitly typed. The second is easy. myAttribute is a SomeOtherClass. IOW, one of explicit or implicit is required.

    2. OTOH, the first could default to Object (so, equivalent to
    Java Code:
    object myAttribute;
    In that case the second reference merely assigns an instance of SomeOtherClass to it. I like option 1 better.

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Stupid noob question: Why not have type inference in Java?

    You don't understand; in case 2, is myAttribute referring to the class attribute, or is it supposed to be a new local variable declaration? The compiler can't know.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,541
    Blog Entries
    7
    Rep Power
    20

    Default Re: Stupid noob question: Why not have type inference in Java?

    Quote Originally Posted by gbritton View Post
    Why doesn't Java figure out the type of myvar from the assignment, so that I could shorten this to:

    Java Code:
    myvar = new MySpecialGenericType<MyActualType> ();
    That is, if myvar is not yet defined in the current scope, could the compiler not declare it implicitly and infer the type from the assignment that follows?
    It could've been the programmer's intention to have 'myvar' to be of a type extending that MySpecialGenericType<MyActualType> type; if the type would've implicitly infered, that wouldn't be possible ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    gbritton is offline Member
    Join Date
    Jul 2013
    Posts
    11
    Rep Power
    0

    Default Re: Stupid noob question: Why not have type inference in Java?

    You're right I don't understand! That's the newbie condition! Anyway I would say that the compiler would say to itself. "Oh, there's no explicit declaration. Hmmm...is the variable already defined in the current scope (including the class)? Why, yes it is!. That's the one I'll use then!!"

    So, if someMethod wants a local variable, it needs to be explicit.

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Stupid noob question: Why not have type inference in Java?

    So now you want Java to be a hybrid between a typed/untyped language.

    Yeah I'd rather stick with how it is now - designed to prevent programming mistakes, not to save keystrokes.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,620
    Rep Power
    5

    Default Re: Stupid noob question: Why not have type inference in Java?

    Relating to what Jos said, it is considered best practice to assign interface implementations to interface types. Example:
    Java Code:
    List<Foo> myList = new ArrayList<>();
    But sometimes you may want any additional functionality that is available in the implementation so you need this:
    Java Code:
    ArrayList<Foo> = new ArrayList<>();
    So what is the inferred type by default? What if the implementation implements several interfaces? IMHO the more that is done for the programmer in these types of situations the more often unexpected (or unintended behavior) will arise.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default Re: Stupid noob question: Why not have type inference in Java?

    Quote Originally Posted by gbritton View Post
    So, if someMethod wants a local variable, it needs to be explicit.
    And then you have two different ways to define a variable.
    Since, from what we currently have in Java, the designers seemed to be trying to avoid complications (eg no multiple inheritance), adding in this sort of rule doesn't really fit.

    It also, to be frank, follows from its ancestry in C++.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,541
    Blog Entries
    7
    Rep Power
    20

    Default Re: Stupid noob question: Why not have type inference in Java?

    I accidentally said it the wrong way around; have a look at this example:

    Java Code:
    class Super() { ... }
    class Sub extends Super { ... }
    ...
    myVar= new Sub();
    The compiler can't infer much: myVar can be of type Super or Sub; any choice can be not what the programmer wanted, so better leave it to the programmer.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default Re: Stupid noob question: Why not have type inference in Java?

    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  13. #13
    gbritton is offline Member
    Join Date
    Jul 2013
    Posts
    11
    Rep Power
    0

    Default Re: Stupid noob question: Why not have type inference in Java?

    Quote Originally Posted by gimbal2 View Post
    So now you want Java to be a hybrid between a typed/untyped language.

    Yeah I'd rather stick with how it is now - designed to prevent programming mistakes, not to save keystrokes.
    The only thing I want is to understand. Other typed languages do type inference (e.g. C#, VB.net). As a new Java learner I found the lack of type inference surprising. Hence the question.

    I've been digging around a bit in other blogs and mailing lists. It looks like type inference is coming in Java 8, or at least partial support for it.

  14. #14
    gbritton is offline Member
    Join Date
    Jul 2013
    Posts
    11
    Rep Power
    0

    Default Re: Stupid noob question: Why not have type inference in Java?

    Quote Originally Posted by JosAH View Post
    I accidentally said it the wrong way around; have a look at this example:

    Java Code:
    class Super() { ... }
    class Sub extends Super { ... }
    ...
    myVar= new Sub();
    The compiler can't infer much: myVar can be of type Super or Sub; any choice can be not what the programmer wanted, so better leave it to the programmer.

    kind regards,

    Jos
    That one's easy: Have the compiler take the "closest" inference, which is Sub. If you want Super, then make it explicit.

    In searching for the answer to my question, I found that the topic had been thoroughly explored before (no surprise there!) and has an official (but rejected) RFE:

    Bug ID: JDK-4459053 Type inference for variable declarations

    About half-way down there is this quote:
    Humans benefit from the redundancy of the type declaration in two ways.
    First, the redundant type serves as valuable documentation - readers do
    not have to search for the declaration of getMap() to find out what type
    it returns.
    Second, the redundancy allows the programmer to declare the intended type,
    and thereby benefit from a cross check performed by the compiler.
    I don't totally buy the generalization about the benefits to humanity but at least I understand the reasoning better.
    KevinWorkman likes this.

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,541
    Blog Entries
    7
    Rep Power
    20

    Default Re: Stupid noob question: Why not have type inference in Java?

    Quote Originally Posted by gbritton View Post
    That one's easy: Have the compiler take the "closest" inference, which is Sub. If you want Super, then make it explicit.
    I think I prefer explicit typing; what if you have this?

    Java Code:
    a= Super(), b= Sub();
    It's a declaration (and initialization) list; is a of type Super or of type Sub()? Are both of type Sub?
    What about arrays?

    Java Code:
    [] array= { new Sub(), new Super() };
    Except for the funny (nonexistent) notation, what would the type of the array be?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  16. #16
    gbritton is offline Member
    Join Date
    Jul 2013
    Posts
    11
    Rep Power
    0

    Default Re: Stupid noob question: Why not have type inference in Java?

    Quote Originally Posted by JosAH View Post
    I think I prefer explicit typing; what if you have this?

    Java Code:
    a= Super(), b= Sub();
    It's a declaration (and initialization) list; is a of type Super or of type Sub()? Are both of type Sub?
    What about arrays?
    a is type Super(); b is type Sub()

    Java Code:
    [] array= { new Sub(), new Super() };
    Except for the funny (nonexistent) notation, what would the type of the array be?

    kind regards,

    Jos[/QUOTE]

    array is type Object.

  17. #17
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default Re: Stupid noob question: Why not have type inference in Java?

    Object[] are almost invariably evil.
    Besides, why not Super[]?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Noob Java Math Question...
    By tomtrom77 in forum New To Java
    Replies: 5
    Last Post: 01-31-2013, 02:40 PM
  2. New to android, probably a stupid question
    By mwr1976 in forum Android
    Replies: 0
    Last Post: 11-22-2011, 08:47 PM
  3. a really stupid question
    By SwinGirl in forum NetBeans
    Replies: 10
    Last Post: 06-25-2008, 09:06 PM
  4. Quick Stupid Question
    By bluekswing in forum New To Java
    Replies: 7
    Last Post: 01-08-2008, 06:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •