Methods.
#269
Replies: 1 comment
-
Regardless of the necessity of methods, the current proposal would break existing code. As I understand it, the |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I strongly believe that no functional language can become mainstream without methods.
Why are methods important:
What are methods?
Functions that dispatch on the type of given argument. Example:
An example
With implicit
this
(this.foo == foo
) and a syntax sugar for lambdasfoo(x, lambda) == foo(x) lambda
,you can really improve on code readability:
Becomes
Furthermore, in combination with monads, this allows for a very powerful typesafe builder DSL:
If we assume that
function() == function
, and that monadicdo
is implicit. You can write html like this:Since all html elements are just methods, you will get perfect autocomplete support from IDE out of the box!
Implementation
There are two ways to implement them, based on what argument gets dispatched:
1. Dispatch on last argument
Here,
foo . bar baz
is equal tobar baz foo
. This plays nice with currying, as.
can be defined as an ordinary function:However, this will lead to some very surprising behavior:
Here, either
person . useName
orfoo . useName
will call the wrong function.That is, unless your language supports multiple dispatch. Which opens its own can of worms.
2. Dispatch on first argument.
Here,
foo . bar baz
is equal tobar foo baz
.The
.
operator must have special support from the compiler.However, this implementation requires only minimal extension to the typesystem.
Beta Was this translation helpful? Give feedback.
All reactions