Author Topic: Automatic population of a variable's type in C++ and Java  (Read 3492 times)

rowbearto

  • Senior Community Member
  • Posts: 2335
  • Hero Points: 132
Automatic population of a variable's type in C++ and Java
« on: February 13, 2018, 03:26:28 PM »
I recently stumbled across this comment in a reddit thread:

Quote
in java/eclipse i really enjoyed just writing String x = methodcall(); for everything, then press ctrl+1,enter to fix the type of x.

From:
https://www.reddit.com/r/cpp/comments/7wuygc/the_15_c11_features_you_must_really_use_in_your_c/du4ewje/

Would be great to have a feature like this in SE!
« Last Edit: February 13, 2018, 03:36:02 PM by rowbearto »

Dennis

  • Senior Community Member
  • Posts: 3960
  • Hero Points: 517
Re: Automatic population of a variable's type in C++ and Java
« Reply #1 on: February 13, 2018, 03:46:56 PM »
I'll also file a feature request for this one, though I think this is a bit eclipsed by "auto" and ":="

rowbearto

  • Senior Community Member
  • Posts: 2335
  • Hero Points: 132
Re: Automatic population of a variable's type in C++ and Java
« Reply #2 on: February 13, 2018, 03:48:37 PM »
I'm not aware of "auto" and ":=", can you provide more info?

Dennis

  • Senior Community Member
  • Posts: 3960
  • Hero Points: 517
Re: Automatic population of a variable's type in C++ and Java
« Reply #3 on: February 13, 2018, 04:04:43 PM »
I was referring to "auto" in C++11 for type inference, and := which is used in languages like Go and Slick-C for type inference (declaration and assignment operator, or as Go calls it, "short variable declaration").  I had thought that := was part of Java 8, but I must have been mistaken.

x := "a string value";   // x is a string

rowbearto

  • Senior Community Member
  • Posts: 2335
  • Hero Points: 132
Re: Automatic population of a variable's type in C++ and Java
« Reply #4 on: February 13, 2018, 04:13:52 PM »
Yes, I was referring to "auto" as well. Sometimes I find "auto" not so readable, so I'd like to be able to start by writing "auto variable = function()", then press a key and have SE replace the "auto" with the actual type of the variable, so it is easier to read.

Java 8 doesn't have auto, but I'd still like to put in any type (even put "auto"), and then press a key and have it replaced with the real type.

Dennis

  • Senior Community Member
  • Posts: 3960
  • Hero Points: 517
Re: Automatic population of a variable's type in C++ and Java
« Reply #5 on: February 13, 2018, 04:25:28 PM »
Type inference is planned for Java 10.  They are going with "var" instead of "auto" or ":=".
http://openjdk.java.net/jeps/286

Few languages have a type inference as good as Slick-C's, in Slick-C, you can even use type inference to declare local variables that are passed by reference as output parameters.

Code: [Select]
save_pos(auto p);
tag_get_local_info(auto localInfo);

rowbearto

  • Senior Community Member
  • Posts: 2335
  • Hero Points: 132
Re: Automatic population of a variable's type in C++ and Java
« Reply #6 on: February 13, 2018, 04:33:26 PM »
Good to know.

Still, I generally want to avoid putting "auto" or whatever keyword that language has in my code, while it is easier to write, it is harder to read.

So having a feature where SE can replace the "auto"/whatever with the actual type would be really useful to me.