DEV Community

Discussion on: .NET Looks at Functional Programming Techniques.

Collapse
 
thebuzzsaw profile image
Kelly Brown

static methods are mechanically equivalent to freestanding methods. This seems like an arbitrary qualification for being "first-class".

Thread Thread
 
jwp profile image
John Peters

Yes but I can't do this... in C# like I can in Typescript

namespace myClassAndFunctionLibrary
{
    public Func<string> junk=()=>"junk";
    public Class MyClass{ }
}
Thread Thread
 
thebuzzsaw profile image
Kelly Brown

Sure, but my point is you are not missing anything in terms of capability.

namespace MyLibrary
{
    public static class J
    {
        public static Func<string> unk = () => "junk";
    }
}

There. Now you can access it via J.unk. Happy? I mean, what is it you really want? You're gonna need to find far worse things to leverage any kind of meaningful criticism against C# here.

Thread Thread
 
jwp profile image
John Peters

Or here's one...


namespace MyLibrary
{
    public static class func
    {
       public static Func<string> junk = () => "junk";
       public static Func<string,string> append = (item)=> item + "appended";
       public static string append2(this string input){  return input + "appended";   }
    }
}

//elsewhere

var junk = func.Junk();
var appended  = func.append("prefix");
var appended2 = "prefix".append2();

The C# Equivalent of first class functions in Typescript/JavaScript are Extension Methods.