-3

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
}
James A. N. Stauffer
  • 2,649
  • 3
  • 22
  • 32

8 Answers8

2

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.

easeout
  • 8,665
  • 5
  • 43
  • 51
1

Can you explain the difference between your example and

String str = "Hello";
Object temp = str;
temp = 10;
jon
  • 2,520
  • 1
  • 14
  • 6
  • This actually won't compile, as ints are not subclasses of Object – Silas Snider Sep 19 '08 at 03:37
  • 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 Sep 19 '08 at 03:39
  • I added a better example that hopefully better shows the benefit. – James A. N. Stauffer Sep 19 '08 at 14:51
0
  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
  • 317
  • 2
  • 8
  • 14
  • #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 Sep 19 '08 at 14:54
0

@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.
  • 23,300
  • 18
  • 98
  • 136
  • 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 Sep 19 '08 at 14:57
  • 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 Sep 19 '08 at 17:57
  • 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. Oct 20 '08 at 20:06
0

Dynamic typing certainly has its uses.

However, I don't think it would make sense adding this to Java - Java is designed as a statically typed language and it would be too large a philosophical and architectural shift to try and graft on dynamic types at this point.

If you want proper dynamic typing but still want to be part of the Java ecosystem, take a look at one of the other great JVM languages that supports dynamic typing: e.g. Clojure and Groovy.

mikera
  • 105,238
  • 25
  • 256
  • 415
0

Dynamically typed languages make coding somewhat simpler and faster, but they do so at the cost of pushing bugs from compile-time (very easy to find) to run-time (hard to find and unpredictable). If Java introduced it and it became prevalent, I would look for another language that cared more about code quality, and my time.

I'm doing a lot of Flex development at work as a front end to my Java work, and it frustrates me to no end that when I am working on a Flex method with parameters, most of the time the developer used Object for the parameter type, and I have absolutely no idea what properties/methods it will have without running the program with breakpoints and hoping I've tested all possible calls to the method. And even when they used a specific class for the parameter, there's no saying that properties/methods haven't been added or deleted from it.

dj_segfault
  • 11,957
  • 4
  • 29
  • 37
0

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
  • 14,257
  • 6
  • 27
  • 35
0

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
  • 87,846
  • 14
  • 132
  • 192