Hacker News new | past | comments | ask | show | jobs | submit login

> Interesting. However, operators and functions present very differently when writing c# code.

Aye, but that's more of a limitation of C#'s syntax

> However, if you want to do that to an operator such as "+", the first thing to do is to wrap it in a lambda - e.g.

That's pretty much what a section (the parens around the operators) does in F# or Haskell.

> I know that the language has no way to specify that the type T has a "+" operator so that's not going to work

Well it's not so much that "operators and functions present very differently when writing C# code" but that "methods and functions present very differently when writing C# code" and that operators are (static) methods more than functions in C#.

Also that C# has no numeric tower[0] so there's indeed no way to say a type is a generic number (whether that number's an int, a double or a decimal). If there was a root `Number` type you could write something along the lines of:

    public T AddNumbers<T> where T:Number(T value)
    {
        // works because all numbers have a `+`
        return value + value;
    }
which is exactly what you write in Haskell:

    addNumbers :: (Num a) => a -> a -> a
    addNumbers a b = a + b
[0] Haskell has a pretty complete/complex one, starting from Num[1] which implements only a few basic operations: (+), (-), (*), negation and conversion from integer.

[1] http://hackage.haskell.org/packages/archive/base/latest/doc/...




Join us for AI Startup School this June 16-17 in San Francisco!

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

Search: