Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why don't we require interfaces in dynamic languages?

Is it just because of dynamic typing we don't require a concept of interfaces(like in Java and C#) in python?

like image 769
Srinivas Reddy Thatiparthy Avatar asked Jun 17 '10 14:06

Srinivas Reddy Thatiparthy


People also ask

Why interface is not used in Python?

Python does not have interfaces, but the same functionality can be achieved with the help of abstract base classes and multiple inheritance. The reason for not having interfaces is that Python does not use static typing, so there's no compile-time type checking.

Why are dynamic languages slower?

Dynamically typed languages must make all of their checks at runtime because the type might change during the course of the execution. Static typed languages resolve all types during compile time so the cost is consumed up front, one time. This is the main reason why dynamic typed languages are typically slower.

Why do people use dynamically typed languages?

Advantages of dynamically-typed languages:The absence of a separate compilation step (which is much more common) means that you don't have to wait for the compiler to finish before you can test changes that you've made to your code. This makes the debug cycle much shorter and less cumbersome.

Are dynamic languages better?

Benefits of Dynamically Typed Languages Generally speaking, dynamic languages are more succinct than their statically typed counterparts. Type annotations, generics etc. all add to the verbosity of the syntax. Languages like C# & Java require quite a bit of code before you can write any useful code.


4 Answers

The interface as a keyword and artifact was introduced by Java1 ( and C# took it from there ) to describe what the contract an object must adhere was.

But, interface has always been a key part of Object Oriented Paradigm and basically it represents the methods an object has to respond. Java just enforces this mechanism to provide statically type checking.

So, dynamic ( OO ) programming languages do use interfaces, even thought they don't statically check them. Just like other data types, for instance in Ruby:

 @i = 1;

You don't have to declare i of type FixNum you just use it. Same goes for interfaces, they just flow. The trade-off is, you can't have a static check on that and failures are only show at runtime.

In the other hand Structural type ( or static duck type as I call it :P ) used by languages as Go or Scala, gives the best of both worlds.

1. See Daniel Earwicker comment about CORBA interface keyword

like image 178
OscarRyz Avatar answered Oct 14 '22 04:10

OscarRyz


We don't require them, but we do support them. Check out Zope Interfaces (which can be and are used outside of Zope).

like image 35
gomad Avatar answered Oct 14 '22 04:10

gomad


It's worth noting that, contrary to what many people will say as a first response, interfaces can be used to do more than document "what methods a class supports". Grzenio touches on this with his wording on "implement the same behaviour". As a specific example of this, look at the Java interface Serializable. It doesn't implement any methods; rather it's used as a "marker" to indicate that the class can be serialized safely.

When considered this way, it could be reasonable to have a dynamic language that uses interfaces. That being said, something akin to annotations might be a more reasonable approach.

like image 42
RHSeeger Avatar answered Oct 14 '22 05:10

RHSeeger


Interfaces are used in statically typed languages to describe that two otherwise independent objects "implement the same behaviour". In dynamically typed languages one implicitly assumes that when two objects have a method with the same name/params it does the same thing, so interfaces are of no use.

like image 42
Grzenio Avatar answered Oct 14 '22 04:10

Grzenio