Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

D is a great language. I just found out today about a feature called "Uniform Function Call Syntax" which acts similarly to extension methods in C#. Essentially, you can call any function with a prototype of:

func(T, ...)

as

t.func(...)

where t is an instance of some type T. This may seem trivial, but it allows for functions to be added to a class or type which is out of your control.

For example, the function to convert a 'string' to a C-style zero terminated string is:

toStringz(string s).

calling the function as s.toStringz() has a much more natural feel, and allows for a much more modular approach than "throw everything in the class", since it can just simply be added on as a uniform function in another module (toStringz is a library feature on the concrete string type). This even works on basic types:

(5.0).sqrt();

!!



Seems like a less type-safe version of Scala's implicit value classes, but I might be wrong?


It looks pretty similar. I think both mechanisms have pretty much the same type constraints.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: