Skip to main content

Implicit Declaration

8 replies [Last post]
Joined: 2004-10-07

(This was reposted from the "Welcome" topic that is now locked)

It is great that Java now has generics.

This adds to the type saftey of the language but it also creates a lot of clutter and unnecessary to particular class in your code. In fact in a lot of cases, the amount of code is not reduced by generics.

What I think would make the situation better is to have an implicit declaration feature I.e.

Rather than doing something like this:
List people = getPeople();
one would do this:
people := getPeople();

This ":=" would automatically declare a variable of type List for you. I think this is consistent with the new for/in syntax. The semantics of the operator would be that if you were to do ":=" to the same variable name within the same scope, it would be an error because you would be delcaring a variable twice.

The major advantage of this is that if you decide to refactor your code in some way and rename the class Person to PersonRole, then you just need to do a recompile, not a find and replace. Sure most IDEs have a "Refactor" facility, but this also cuts down on the amount of code you write. It also cuts down on the source level dependencies between classes.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2004-03-18

except foo.getList() should declare it returns a List, even if the actual return type is Vector.

But yes you are right, this whole thread is a dead duck because of problems like this.

Joined: 2003-06-20

This is not Java. -1.

Joined: 2003-06-13

Thats a really bad idea that makes code very unreadable. It also breaks OO princibles since the compiler has no idea of finding out if you mean List<> or Vector<> or ArrayList<>

Joined: 2004-10-07

Of course the compiler would know because it would know the return type of "getPeople".

I.e. if getPeople were defined as

public List getPerson(){ ... }

the compiler would know that "person" was of type List. Likewise if getPeople were defined as

public ArrayList getPerson() { ... }

the compiler would know that "person" was of type ArrayList.

Pretty simple eh ?

Joined: 2003-06-13

Nope, not that simple; you just threw away the huge advantage of the Collections framework.

Your suggestion means that if I change the returntype of method X from ArrayList to List or even Vector the field in another class would automatically change.

Class MyWidget
mylist := Foo.getList();

Vector Foo.getList() {..}

Now; if I change my getList() return type the compiler would break on setListData() in a whole other class, maybe 100 lines beneath the definition of the myList variable.
If you think that makes it clear and makes code readable, then I will simply disagree with you in silence...

Joined: 2004-10-07

This was already propose in 2001 with
a syntax that seems more easy :

But i agree with zander, this is not pratical
for writing "real" software i.e more than 3 classes :)


Joined: 2003-06-10

-1 !

Java should not be made for lazy developers. Instead make Java as clear as possible.


Joined: 2004-10-07

It's not about being lazy, it's about not having unnecessary dependencies / clutter in your source code.