Hacker News new | comments | show | ask | jobs | submit login
Uniform Function Call Syntax in D (drdobbs.com)
27 points by stesch 1886 days ago | hide | past | web | 8 comments | favorite

Extension methods were a welcome addition back when I was doing C# work, but today, I much prefer the approach employed by Clojure.

Clojure provides two "arrow" Macros called -> and ->>. The former transforms (-> x (f 1) g) into (g (f x 1)) and the latter transforms (->> x (f 1) g) into (g (f 1 x)).

This seems a little creepy from a code archaeology standpoint? Haven't written any D so I don't know if it's an improvement over what came before, but this looks a little magical for my tastes.

I imagine it would be a relatively simply matter of getting used to it.

Objective C and C# both have extension methods, which are very useful when done right. Ruby has allowed you to reopen classes since forever.

The point is to not have to reopen classes.

But is there an essential difference between these methods?

Extension methods can only use the public API of objects. Opening a class and adding methods allows one to access the private API, too. That, of course, is only a difference in languages that have the public/private distinction.

But it doesn't make reopening classes bad. You have a choice: you can use a public API or mess with implementation (for example for performance reasons).

The essential difference is that extension methods are visible only in compilation units that explicitly import them, while adding a new method to an existing class makes it visible everywhere.

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | DMCA | Apply to YC | Contact