None of that has anything whatsoever to do with type driven programming. ^.^
Type driven programming would be something like, oh if we were to emulate Phoenix’s :safe
html renderer:
type safe = SafeHTML of string
let make_input_safe input =
SafeHTML (escape_html input)
let raw input = SafeHTML raw
let render_html (SafeHTML html) = render_to_whatever html
Notice that I do define a wrapper type, it is documentation but otherwise no types are listed anywhere, not in the input arguments, no where else, it is all properly inferred. With the above you cannot even accidentally pass a non-safe chunk of html text to the renderer, you have to either mark is as raw
or or escape the input. Also notice that the type drove the interface, not vice-versa.
Your examples are much more, hmm, Java-like, and Java is NOT a good language to do type-driven programming.
I’m not even sure what your example is demonstrating, do you have actual code to show?
And your things about the_word_apple
and so forth seems very hungarian notation, but significantly more verbose, and verbosity can make code much harder to read as well.
Like say you have a unit of measurement, let’s say a meter:
let width_of_house = 12
Well that is not descriptive at all, we could do:
let meters_width_of_house = 12
That just makes it longer, and does not cause an error when we doing something like:
centimeters_is_expected_in_this_function meters_width_of_house
Wow that becomes unreadable very fast, and sure you may catch it with your eyes, but it is still easily missable. Compare this to a type driven approach to measurements:
type meter = Meter of float
let cm i = Meter (i /. 100.0)
let dm i = Meter (i /. 10.0)
let m i = Meter i
let km i = Meter (i *. 1000.0)
Or you could do something like:
type length =
| Meters of float
| Centimeters of float
| Kilometers of float
| Feet of float
...
let centimeters_of_length = function
| Meters m -> m
| Centimeters cm -> cm /. 100.0
| Kilometers km -> km *. 1000.0
| Feet f -> f *. 30.48
...
Or via Witnesses or a variety of other ways, whatever type style fits the pattern best. And you can keep names short, descriptive, readable, and never worry about the system compiling when you try to pass something to somewhere where it does not belong. ^.^