views:

164

answers:

6

What if a Java allow both static and dynamic types. That might allow the best of both worlds. i.e.:

String str = "Hello";
var temp = str;
temp = 10;
temp = temp * 5;
  1. Would that be possible?
  2. Would that be beneficial?
  3. Do any languages currently support both and how well does it work out?

Here is a better example (generics can't be used but the program does know the type):

var username = HttpServletRequest.getSession().getAttribute("username");//Returns a String
if(username.length() == 0) {
    //Error
}
+1  A: 

Can you explain the difference between your example and

String str = "Hello";
Object temp = str;
temp = 10;
jon
This actually won't compile, as ints are not subclasses of Object
Silas
usually, what is meant by dynamic is that one could then call methods on the var and they would work if the runtime type had the method. So in your case, calling temp.length() would not work without a cast. Dynamic implies runtime type checking, not compile-time
Lou Franco
I added a better example that hopefully better shows the benefit.
James A. N. Stauffer
A: 

Dynamic typed variables just have type Universal, such that every other type is a subtype of Universal. Any language where all types inherit from such a universal type (such as Java modulo unboxed values) already have this capability. As for usefulness - depends on what you're doing. I prefer a fully static type system where it's easy to create safe tagged unions so I can put whatever values I need into a variable, but in a more controlled way than Universal.

Thelema
A: 

Yes, it would be beneficial, but not for the example you are showing. I think something like this would be better

public void f(var o)
{
   o.method();
}

and I could call f with any object with a method called method() without needing to introduce an interface. It's nice for places where there is a convention, but not necessarily an interface. It would also be useful for interop with languages with a dynamic dispatch.

Lou Franco
Sorry about my bad example -- I added a better example.
James A. N. Stauffer
+1  A: 

C# has a "var" keyword used in the manner you describe, but it ends up being a strongly typed variable based on the types of values that type checking suggests will go into it.

Kevin Conner
A: 
  1. Would that be possible?
    Hardly. Java is a C-like language, which was initially designed to support only static typing. It will take a lot of time to introduce this feature to Java and I don't think developers will spend time on that.
  2. Would that be beneficial?
    No. Java is a traditional language, and static typing is one of the base principles or it's architecture. In fact, static-typing is still more popular in programming (you can check the interesting statistics here).
  3. Do any languages currently support that and how well does it work out?
    Well, there are a lot of languages which support dynamic typing, but I guess you know this. If the language supports dynamic typing, it doesn't make sense to introduce static typing to this language...

Also, feature which you show in your example can be implemented with static typing. jon gave you an example with "Object" class in Java. You can do similar with void* in C/C++. Still, this doesn't make language dynamic-typed.

Das
#2: Just because it was designed one way doesn't mean that it would not be beneficial another way. That logical would have prevented many recent changes -- generics, for each, enum, etc.#3: Sorry I wasn't clear -- meant a language that supports both.
James A. N. Stauffer
A: 

@Lou: It would be just syntactic sugar for:

void doSth(Object foo) throws Exception{
     Method m = foo.getClass().getMethod("foo", String.class);
     m.invoke(foo, "baz");  
}

I do use reflection, but I prefer it to be ugly so it's not abused.

jb
That logic could be used to say that everything dangerous should be hard to avoid abuse. for loops, threads, etc can be abused so do you think they should be hard also?
James A. N. Stauffer
It might be -- it might be possible to implement it without reflection -- the point is that some languages (LISP, python, Ruby) are built entirely on this, and their users find it useful -- I think that would be true in Java too (for some applications)
Lou Franco
One of features of Java is lack of features. Java makes it harder to shoot yourself in foot. And, well,bad programmers happen... and working with bad code happens. With dynamic types Java would reach new levels of bad code.
jb