通过在C#中使用更多静态方法,我可以获得函数式编程(F#)的相同好处吗?

时间:2022-09-11 09:31:16

I admit I haven't grokked F# yet. But in the 30,000 foot descriptions, they keep talking about easy to test code that doesn't have mutable state. Is that the same as static methods?

我承认我还没有弄过F#。但是在30,000英尺的描述中,他们一直在谈论易于测试的代码,这些代码没有可变状态。这和静态方法一样吗?

Could I get the benefit of F# by writing some of my code in classes with all static methods?

我可以通过在所有静态方法的类中编写一些代码来获得F#的好处吗?

I'm just looking for the short answer, I know whole books exist on the topic.

我只是在寻找简短的答案,我知道有关该主题的全书。

7 个解决方案

#1


You could certainly write C# code immutably, but that's nothing to do with static functions. Immutability are things like having a struct or object that only "changes state" by making a copy of itself and having the copy be different.

你当然可以不可思议地编写C#代码,但这与静态函数无关。不变性就像拥有一个结构或对象,它只是通过复制自身并使副本不同来“改变状态”。

#2


Absolutely no, immutability has nothing to do with methods being static or instance. String, being an immutable class, has plenty of instance methods, which, in a very functional manner, return a new instance of String class, rather than modifying anything.

绝对不,不变性与静态或实例方法无关。 String是一个不可变类,有很多实例方法,它们以非常实用的方式返回String类的新实例,而不是修改任何东西。

You could try to emulate F# by using functional decomposition, but this will still be pretty imperative code.

您可以尝试使用功能分解来模拟F#,但这仍然是非常必要的代码。

#3


Apart from static, functional modules versus objects, you can attempt to get some of the benefits of F# by using C# 3 and lambdas, LINQ, etc. However, that doesn't go very far. What I find nice in F# is:

除了静态的,功能性的模块与对象之外,你可以尝试通过使用C#3和lambda,LINQ等来获得F#的一些好处。但是,这并没有走得太远。我在F#中发现的很好的是:

  • Inference everywhere
  • Auto-generalization (adds in type parameters so I don't have to sort it out manually)
  • 自动泛化(添加类型参数,因此我不必手动将其排序)

  • Easy immutability
  • Easy mix between module and classes
  • 模块和类之间的轻松混合

  • Types like discriminated unions
  • 类似歧视的工会

  • Pattern matching
  • Nested functions (lightweight)
  • 嵌套函数(轻量级)

  • First class functions (no, C#'s named delegates don't count)
  • 第一类函数(不,C#的命名代理不计)

  • Everything's an expression
  • 一切都是表达

  • Easy function composition
  • 功能组合简单

So, you can try to do some of this in C#. Some of it is simply not supported and won't work. The rest gets very ugly, very fast.

所以,你可以尝试在C#中做一些这样的事情。其中一些根本不受支持,不起作用。其余的变得非常丑陋,非常快。

So, if you go much off the beaten path of LINQ and the Enumerable extensions, you'll probably end up in a world of pain.

所以,如果你远离LINQ和Enumerable扩展的常规路径,你可能最终会陷入痛苦的世界。

#4


I beg to differ with all the other answers to date. Immutability and static methods may not be strictly technically related, but I have found that using F# has encouraged me to make C# methods static whenever I can.

我请求与日期的所有其他答案不同。不变性和静态方法在技术上可能并不严格,但我发现使用F#鼓励我尽可能地使C#方法保持静态。

The thinking is analogue, in that it is easier to handle an immutable object because you don't have to worry about it changing state. In the same way, you don't have to worry about state when using a static method (unless you use a global singleton or something...).

这种想法是类比的,因为它更容易处理不可变对象,因为您不必担心它会改变状态。以同样的方式,您在使用静态方法时不必担心状态(除非您使用全局单例或其他东西......)。

#5


No, it's not the same as static methods. You don't have mutable state if you don't assign anything (locals, function arguments, static fields, instance fields) after it was initialized. You can get some of the benefits by designing your classes to be immutable.

不,它与静态方法不同。如果在初始化之后没有分配任何东西(本地,函数参数,静态字段,实例字段),则没有可变状态。您可以通过将类设计为不可变来获得一些好处。

#6


No, the two concepts are unrelated. Static methods in C# can still modify incoming objects as normal, and other variables using ref or out.

不,这两个概念是无关的。 C#中的静态方法仍然可以正常修改传入对象,使用ref或out修改其他变量。

#7


IT's true. You aren't going to get the benefits of functional programming just by using more static functions in C#. Howver, if you were to look under the hood (using Reflector, for example) I understand a simple let statement is a static function. In other words,

这是真的。仅仅通过在C#中使用更多静态函数,您将无法获得函数式编程的好处。但是,如果你要深入了解(例如使用Reflector),我理解一个简单的let语句是一个静态函数。换一种说法,

//F#
let a = 2

is like a function in C#

就像C#中的一个函数

//C#
static int a()
{
    return 2;
}

I can understand the confusion.

我能理解这种困惑。

Explanation pulled from Leon Bambrick's "F# Eye for the C# Guy presentation."

从Leon Bambrick的“F#Eye for the C#Guy演示文稿”中解读说明。

#1


You could certainly write C# code immutably, but that's nothing to do with static functions. Immutability are things like having a struct or object that only "changes state" by making a copy of itself and having the copy be different.

你当然可以不可思议地编写C#代码,但这与静态函数无关。不变性就像拥有一个结构或对象,它只是通过复制自身并使副本不同来“改变状态”。

#2


Absolutely no, immutability has nothing to do with methods being static or instance. String, being an immutable class, has plenty of instance methods, which, in a very functional manner, return a new instance of String class, rather than modifying anything.

绝对不,不变性与静态或实例方法无关。 String是一个不可变类,有很多实例方法,它们以非常实用的方式返回String类的新实例,而不是修改任何东西。

You could try to emulate F# by using functional decomposition, but this will still be pretty imperative code.

您可以尝试使用功能分解来模拟F#,但这仍然是非常必要的代码。

#3


Apart from static, functional modules versus objects, you can attempt to get some of the benefits of F# by using C# 3 and lambdas, LINQ, etc. However, that doesn't go very far. What I find nice in F# is:

除了静态的,功能性的模块与对象之外,你可以尝试通过使用C#3和lambda,LINQ等来获得F#的一些好处。但是,这并没有走得太远。我在F#中发现的很好的是:

  • Inference everywhere
  • Auto-generalization (adds in type parameters so I don't have to sort it out manually)
  • 自动泛化(添加类型参数,因此我不必手动将其排序)

  • Easy immutability
  • Easy mix between module and classes
  • 模块和类之间的轻松混合

  • Types like discriminated unions
  • 类似歧视的工会

  • Pattern matching
  • Nested functions (lightweight)
  • 嵌套函数(轻量级)

  • First class functions (no, C#'s named delegates don't count)
  • 第一类函数(不,C#的命名代理不计)

  • Everything's an expression
  • 一切都是表达

  • Easy function composition
  • 功能组合简单

So, you can try to do some of this in C#. Some of it is simply not supported and won't work. The rest gets very ugly, very fast.

所以,你可以尝试在C#中做一些这样的事情。其中一些根本不受支持,不起作用。其余的变得非常丑陋,非常快。

So, if you go much off the beaten path of LINQ and the Enumerable extensions, you'll probably end up in a world of pain.

所以,如果你远离LINQ和Enumerable扩展的常规路径,你可能最终会陷入痛苦的世界。

#4


I beg to differ with all the other answers to date. Immutability and static methods may not be strictly technically related, but I have found that using F# has encouraged me to make C# methods static whenever I can.

我请求与日期的所有其他答案不同。不变性和静态方法在技术上可能并不严格,但我发现使用F#鼓励我尽可能地使C#方法保持静态。

The thinking is analogue, in that it is easier to handle an immutable object because you don't have to worry about it changing state. In the same way, you don't have to worry about state when using a static method (unless you use a global singleton or something...).

这种想法是类比的,因为它更容易处理不可变对象,因为您不必担心它会改变状态。以同样的方式,您在使用静态方法时不必担心状态(除非您使用全局单例或其他东西......)。

#5


No, it's not the same as static methods. You don't have mutable state if you don't assign anything (locals, function arguments, static fields, instance fields) after it was initialized. You can get some of the benefits by designing your classes to be immutable.

不,它与静态方法不同。如果在初始化之后没有分配任何东西(本地,函数参数,静态字段,实例字段),则没有可变状态。您可以通过将类设计为不可变来获得一些好处。

#6


No, the two concepts are unrelated. Static methods in C# can still modify incoming objects as normal, and other variables using ref or out.

不,这两个概念是无关的。 C#中的静态方法仍然可以正常修改传入对象,使用ref或out修改其他变量。

#7


IT's true. You aren't going to get the benefits of functional programming just by using more static functions in C#. Howver, if you were to look under the hood (using Reflector, for example) I understand a simple let statement is a static function. In other words,

这是真的。仅仅通过在C#中使用更多静态函数,您将无法获得函数式编程的好处。但是,如果你要深入了解(例如使用Reflector),我理解一个简单的let语句是一个静态函数。换一种说法,

//F#
let a = 2

is like a function in C#

就像C#中的一个函数

//C#
static int a()
{
    return 2;
}

I can understand the confusion.

我能理解这种困惑。

Explanation pulled from Leon Bambrick's "F# Eye for the C# Guy presentation."

从Leon Bambrick的“F#Eye for the C#Guy演示文稿”中解读说明。