对不起,我的标题太含糊了——如果我能想出一个简洁的标题,我就不用问这个问题了。

假设我有一个不可变的列表类型。它有一个操作Foo(x),该操作返回一个新的不可变列表,其中指定的参数作为结尾的额外元素。因此,要建立一个包含"Hello", "immutable", "world"值的字符串列表,你可以这样写:

var empty = new ImmutableList<string>();
var list1 = empty.Foo("Hello");
var list2 = list1.Foo("immutable");
var list3 = list2.Foo("word");

(这是c#代码,我最感兴趣的是c#建议,如果你觉得语言很重要的话。这根本不是一个语言问题,但语言中的习语可能很重要。)

重要的是现有的列表不会被Foo改变,所以是空的。Count仍然返回0。

另一种(更习惯的)达到最终结果的方式是:

var list = new ImmutableList<string>().Foo("Hello")
                                      .Foo("immutable")
                                      .Foo("word");

我的问题是:Foo最好的名字是什么?

编辑3:正如我稍后所揭示的,类型的名称实际上可能不是ImmutableList<T>,这使得位置很清楚。相反,想象它是TestSuite,它是不可变的,因为它所在的整个框架是不可变的……

(编辑3结束)

到目前为止,我想到的选项是:

Add: common in .NET, but implies mutation of the original list Cons: I believe this is the normal name in functional languages, but meaningless to those without experience in such languages Plus: my favourite so far, it doesn't imply mutation to me. Apparently this is also used in Haskell but with slightly different expectations (a Haskell programmer might expect it to add two lists together rather than adding a single value to the other list). With: consistent with some other immutable conventions, but doesn't have quite the same "additionness" to it IMO. And: not very descriptive. Operator overload for + : I really don't like this much; I generally think operators should only be applied to lower level types. I'm willing to be persuaded though!

我选择的标准是:

给出方法调用结果的正确印象(即它是带有额外元素的原始列表) 尽可能清楚地表明它不会改变现有的列表 当像上面的第二个例子一样连接在一起时,听起来很合理

如果我说得不够清楚,请再问我一些细节。

编辑1:以下是我更喜欢Plus而不是Add的理由。考虑以下两行代码:

list.Add(foo);
list.Plus(foo);

在我看来(这是我个人的观点),后者显然是有问题的——它就像写“x + 5;”作为一个单独的语句。第一行看起来是可以的,直到您记得它是不可变的。事实上,加号操作符本身不改变操作数的方式是我最喜欢加号的另一个原因。没有操作符重载的麻烦,它仍然具有相同的含义,其中包括(对我来说)不改变操作数(在本例中是方法目标)。

编辑2:不喜欢添加的原因。

各种各样的回答都很有效:“使用Add。这就是DateTime所做的,而String有Replace方法等,这些方法不会使不可变性变得明显。”我同意,这里有优先权。然而,我见过很多人称之为DateTime。添加或字符串。替换并期待突变。有大量的新闻组问题(如果我仔细研究的话,可能会有这样的问题),这些问题的答案是“您忽略了String.Replace的返回值;字符串是不可变的,返回一个新的字符串"

现在,我应该揭示这个问题的一个微妙之处——类型实际上可能不是一个不可变列表,而是一个不同的不可变类型。特别是,我正在开发一个基准测试框架,在该框架中,您可以向套件中添加测试,并创建一个新的套件。这可能是显而易见的:

var list = new ImmutableList<string>();
list.Add("foo");

不会完成任何事情,但当你把它改成:

var suite = new TestSuite<string, int>();
suite.Add(x => x.Length);

看起来应该没问题。然而,对我来说,这个错误更清楚了:

var suite = new TestSuite<string, int>();
suite.Plus(x => x.Length);

这只是在乞求:

var suite = new TestSuite<string, int>().Plus(x => x.Length);

理想情况下,我希望我的用户不必被告知测试套件是不可变的。我想让他们坠入成功的深渊。这可能是不可能的,但我想试试。

很抱歉,我只讨论了不可变的列表类型,过度简化了最初的问题。并非所有的集合都像ImmutableList<T>:)那样具有自描述性。


当前回答

扩展方法呢?在这种情况下,可以将其称为Join。作为一个扩展方法,用户应该知道它是一个静态方法,因此可能会让他们暂停一下,并鼓励他们查看返回值。同时,您拥有“实例”方法的可用性。

public static ImmutableList<T> Join(this ImmutableList<T> body, T tail)
{
    // add robust error checking in case either is null...
    return new ImmutableList<T>(body, tail);
}

再后来……

var list = new ImmutableList<string>().Join("Hello")
                                      .Join("Extensible")
                                      .Join("World");

我不太清楚发布多个答案的普遍行为,但这是一个有趣的问题,因为我认为命名是设计中的关键步骤,我的大脑一直在思考这个问题。

其他回答

1)问题分析 问题的摘要可以是: 从一个不可改变的…获得一个新的不可变的,但与原来的不同。 在当前问题的语境中: 从一个不可变列表中获得一个新的不可变列表,该列表最后包含一个新元素。 你能否给出一个有效的“动词”(编程语言中的“方法”),以简洁的方式表达动作,而不使用简单的“添加”动词?

2) Proposal analysis First concept : Immutable give a similar immutable , >>"copy" verb express conservation in "content" but less for it defined constraints (immutable) >>"clone" verb express conservative in "content" but also for its defined constraints. >>"twin" verb is similar to clone but not common in software , but it's short and sound good. Second concept : the result give something different from the original >>"add" express an action to make the difference between original and the new object but by definition we do not act (mutate) an immutable. >>"plus" express the fact that verb result will be augmented without disturbing the original object ... "adverb" approach is more "immutable" friendly >>"with" as before it express the fact that "verb" will do something more but may be ambigous of what it will doing more. Can also express the fact the "verb parameter" is the "more" of the "verb" 3) Answers With verb "clone" >> augmentClone >> growClone >> extendClone with "with" >> cloneWith >> augmentCloneWith >> growCloneWith >> extendCloneWith With verb "twin" >> augmentTwin >> growTwin >> extendTwin with "with" >> twinWith >> augmentTwinWith >> growTwinWith >> extendTwinWith

我将其命名为Extend()或者ExtendWith()如果您觉得非常冗长的话。

扩展意味着在不改变的情况下将某物添加到另一物。我认为这是c#中非常相关的术语,因为它类似于扩展方法的概念——它们“添加”一个新方法到一个类中,而不“触及”类本身。

否则,如果你真的想强调你根本没有修改原始对象,使用一些像Get-这样的前缀在我看来是不可避免的。

我知道这似乎是在徒劳无益,但我认为您可能会以一种奇怪的方式来处理这个问题(因此命名困难)。当我有一个不可变的对象时,我通常不期望该对象具有暗示可变性的函数(如Add或AddAll)。我知道字符串和DateTime这样做,但即使我已经成为受害者,忘记使用DateTime / String函数的结果,所以我会尽可能避免这些陷阱。

如果我是您,我会遵循类似于Guava使用的Builder模式。参见Guava中的不可变集合。基本上,这个概念是用它所包含的所有内容来构造不可变对象。如果你想要创建一个新的不可变对象,你需要使用一个新的构建器。

var suite = new TestSuite.Builder<string, int>().add(newTest).build();

在用户调用构建之前,您可以扩展构建器以允许对测试套件进行各种修改(如果您愿意,可以使用标准列表命名约定,如添加/删除)。

扩展方法呢?在这种情况下,可以将其称为Join。作为一个扩展方法,用户应该知道它是一个静态方法,因此可能会让他们暂停一下,并鼓励他们查看返回值。同时,您拥有“实例”方法的可用性。

public static ImmutableList<T> Join(this ImmutableList<T> body, T tail)
{
    // add robust error checking in case either is null...
    return new ImmutableList<T>(body, tail);
}

再后来……

var list = new ImmutableList<string>().Join("Hello")
                                      .Join("Extensible")
                                      .Join("World");

我不太清楚发布多个答案的普遍行为,但这是一个有趣的问题,因为我认为命名是设计中的关键步骤,我的大脑一直在思考这个问题。

我觉得"加"或"加"听起来不错。列表本身的名称应该足以传达列表的不可变性。