1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# Appendix F: Implicit polymorphism
If you've used Lux's interfaces and implementations already (with the `\` macro), you've probably noticed that you need to pass around the specific implementations you need every time you want to call some interface's method, or access some constant value.
That can become tiresome if you need to do it all the time, and specially if you come from languages that do method-selection for you automatically.
Object-oriented languages do polymorphism in an easy way, because they link objects to the method table of their associated classes, and when you call a method on an object, the run-time system can figure out where the code that needs to be run lies within the program's memory.
Languages with type-classes, such as Haskell, perform that look-up at compile-time, by using the type-information present in the compilation context to figure out which implementation (or _instance_) of a type-class is suitable to each particular circumstance.
Lux, on the other hand, forces you to be specific about the implementations that you're going to use.
While that gives you a level of power and flexibility you wouldn't otherwise have in other languages, it also introduces the problem that when what you want doesn't warrant that level of power, you have to pay the tax it involves nonetheless.
But, that sounds like a raw deal.
Why do you have to pay for something you're not taking advantage of?
Clearly, there is an asymmetry here.
It is a feature that is most useful in the few instances when you want full power.
At any other point, it's a hindrance.
Well... there is an alternative.
The Lux Standard Library includes a module called `library/lux/type/implicit`, which provides a macro called `\\`, that serves as an easier-to-use alternative to the `\` macro.
What it does is that instead of requiring the implementation you want to use, it only requires the name of the function you want to call and the arguments.
Then, at compile-time, it does some type-checking and some look-ups and selects an implementation for you that will satisfy those requirements.
That implementation can come from the local-var environment, from the definitions in your own module, or even from the exported definitions of the modules you're importing.
That way, you can use `\` whenever you need precision and power, and use `\\` whenever you're doing more lightweight programming.
Fantastic!
This is how you'd use it:
```clojure
... Equality for nats
(\ nat.equivalence = x y)
## vs
(\\ = x y)
```
```clojure
... Equality for lists of nats
(\ (list.equivalence nat.equivalence) =
(list.indices 10)
(list.indices 10))
... vs
(\\ = (list.indices 10) (list.indices 10))
```
```clojure
... Functor mapping
(\ list.functor each nat.inc (list.indices 10))
... vs
(\\ each nat.inc (list.indices 10))
```
---
Thanks to implicit polymorphism, you don't have to choose between power and ease of use.
Just do a static-import of the `library/lux/type/implicit` module, and you'll get the `\\` available and ready for action.
|