3 ms·
Function declaration syntax where the return type is last is very common in functional languages. It's even allowed in C++11, because it allows one to define re
by scott_s 11y ago
Function declaration syntax where the return type is last is very common in functional languages. It's even allowed in C++11, because it allows one to define return types that depend on parameter types. An example taken from http://en.cppreference.com/w/cpp/language/auto http://en.cppreference.com/w/cpp/language/auto,
template<class T, class U>
auto add(T t, U u) -> decltype(t + u) // return type is type of operator+(T, U)
{
return t + u;
}
It's quite a sensible approach, and hardly an impediment to understanding. A more valid concern is that Scala introduces a lot more concepts. I don't necessarily agree with it, but I think it's a potential issue, whereas a simple syntax difference that a lot of languages have is not a big deal.
- edem 11y agoI see. I was not aware that this kind of syntax was present elsewhere, thanks for pointing it out!
- autoreleasepool 11y agoI agree the syntax is sensible. I quite like it in Swift: protocol Numeric { func +(lhs: Self, rhs: Self) -> Self } func<T: Numeric>(a: T, b: T) -> T { return a + b }
- autoreleasepool 11y agoCorrection: Function name. func add<T: Numeric>(a: T, b: T) -> T { return a + b }