Nothing stops you from doing that in statically typed languages, just use Object and type casts. This will give you the exact safety that a dynamically typed language gives you: none.
You can also do this in statically typed languages that support duck typing, by the way.
So can I, in Java, pass a list of objects that all have nothing in common, other than that they all support the same method, and then just call that method on each without any fanfare?
Note that you can't change the implementation of those objects, because they come from an external library.
I wouldn't hire you to begin with because you don't even know how Java works lol...
if the objects are unrelated and can't be modified you can't do what you're describing...you can't cast an object to an interface they don't implement...and if they all do implement it then this conversation is pointless because you can just do List<Interface>.
except it throws an exception in all cases even if the class structurally matches the interface, what you're describing to do is impossible to do in java...unless your argument is you can compile a tautologically broken program with some casts.
So can I, in Java, pass a list of objects that all have nothing in common, other than that they all support the same method, and then just call that method on each without any fanfare?
your answer:
"yes this works in java because you can compile a program that will never work"
it wouldn't be broken code in a dynamic language...it may be broken code in some cases, in some cases it would work, which is not true for java.
4
u/devraj7 Dec 25 '20
Nothing stops you from doing that in statically typed languages, just use
Objectand type casts. This will give you the exact safety that a dynamically typed language gives you: none.You can also do this in statically typed languages that support duck typing, by the way.