Search code examples
programming-languages

Strongly (very) typed language


Could anyone please tell me if there is a language which would forbids (won't compile) if you pass an argument to a fnc which is not an exact match (but has either trivial or user defined conversion to a needed type). For example if you have:

void f(int value);
//and in code you passing:
bool a = false;
f(a);

This is strictly theoretical Q.


Solution

  • This is a vague question, but all the same: Haskell, OCaml etc have this sort of behavior. If a function requires an Int - it has to be given an Int. You maybe able to write functions that coerce Ints to Bools but that doesn't change anything i.e. you still get a type error. Of course, there are languages with far more demanding type systems and complex proof obligations that Haskell and OCaml.

    Scala is an interesting language where if there is a user defined coercion from one type to the other and it is non-ambiguous, the compiler will insert it for you. For example, sometimes people use it to coerce datatypes like (Int, (Int, Int)) to ((Int, Int) Int) which is handy.