Javascript is Untyped Language
No, Javascript has no concept of polymorphism, because it is an untyped language. Simply put, polymorphism means that a strict type system is less rigid under controlled conditions, that is, it will not lose type safety by behaving polymorphically.
However, untracked language is somewhat simplified. From the point of view of a static type system, Javascript has one huge type of union, and the value or its basic expression can take any of its representations at run time (variables can even adapt to different types during their existence). This type of input is also called dynamic typing.
Dynamically typed languages ββhave an introspection for checking the type of value at runtime. But that means they are limited. You cannot introspect the type of function, for example, if it is not fully applied.
However, functional programming in the original sense means working with many small specialized functions of the first and higher order, which are declared in curried form. This approach leads to partially applied functions throughout your code. The problem is that you need to not only output the types of your initial functions, but also the intermediate types that are partially used. It will be very fast:
// what the type of this function? const comp = f => g => x => f(g(x)); // and this partially applied one? const inc = n => n + 1; comp(inc); // and even worse: comp1 = comp(comp); comp2 = comp(comp) (comp);
I'm sure I lost you somewhere between the lines. It takes a long time to get these types out of the mind. And should it be your responsibility as a developer to act as a compiler? I do not think so.
Alternative solutions to the problem
Fortunately, the Javascript community is actively developing solutions to this type of problem.
Static type check above language
Stream and TypeScript are static type testers that try to add a type system to Javascript retrospectively. I personally do not think this is a promising approach, because usually you develop a type system first when creating a new language. Javascript can execute side effects literally everywhere, making it very difficult to create sound and reliable type checking. Check issues in the stream repository to get your own image.
Degrading Javascript as a Compilation Target
I., this headline may be a little based on opinions, but that seems to me. Elm, purescript, Facebook The reason is the representatives of this approach. Well, if you want to give up Javascript, these are reasonable options. But which horse to bet on? And is Javascript ecosystem fragmentation really desirable? Or do we want the community to depend on providers like Facebook? I cannot answer this question because I am very biased as you are going to see.
Runtime Check
Heads up: this is a shameless plugin!
As a dynamically typed language, Javascript comes with mature introspection capabilities. Along with the ES2015 proxy, we have everything we need to create a virtualized runtime type check. In this context, Virtual means that it is connecting, that is, you can turn it on and off. The runtime system must be plug-in because it has a significant impact on performance and is only required at the design stage.
I have been working on such type checking for several months now, and it was an exciting journey. ftor is far from stable, but I think the approach is worth exploring.
Here is the comp combinator at the top in the form of a typed version with type hints ( TS is just an internal Symbol that contains the current type signature, and you can use it to request this signature for debugging purposes):
import * as F from ".../ftor.js"; F.type(true); const comp = F.Fun( "(comp :: (b -> c) -> (a -> b) -> a -> c)", f => g => x => f(g(x)) ); const inc = F.Fun( "(inc :: Number -> Number)", n => n + 1 ); comp(inc) [TS];
comp1 signature of intermediate type tells you what it expects ...
- binary function
- value
- unary function
- and other meaning
That is, you apply it as comp1(inc) (1) (add) (2) (3) .
comp2 signature of the intermediate type tells you what it expects ...
- unary function
- binary function
- two meanings
And you apply it as comp2(inc) (add) (2) (3) . So comp2 really useful because it allows us to use a binary function as an internal composition function.
Admittedly, these type signatures are not easy to read if you are not familiar with them. But, in my experience, the learning curve is pretty short.
ftor supports parametric and strict polymorphism, but is currently not ad-hoc.