3 ms·
Functional languages tend to have name resolution as a separate step before type checking. OO languages tend to intermingle name resolution and type checking.
by kccqzy 7y ago
Functional languages tend to have name resolution as a separate step before type checking. OO languages tend to intermingle name resolution and type checking.
For example in OO languages `obj.func(1, 2)` will typically require the compiler to know the type of obj before knowing which func method is being referred to here.
In Haskell however the equivalent expression is `func obj 1 2`. The compiler has already performed name resolution and knows exactly what func is. So that when doing type-checking, it is not only possible for typing information to propagate from object to method, but also from function to object. That is to say, it is possible that the compiler initially knows what type func has, but has no idea what type obj has; it will only do so after type inference using the type of func.
This makes type inference more general and more elegant. I suspect it also makes the implementation of name resolution easier. Unfortunately as you have pointed out, it's practically more of a nuisance.
Also note that this applies to more than just names of fields associated with a type; it even applies to type names associated with a type. For example if you have
class G a where
data N a :: *
The name N is directly usable without even mentioning G. This would require, say in C++, you to write `G::N` to refer to an inner class:
class G {
public:
class N;
};
The reasoning is of course the same.