What's the best name for a non-mutating “add” method on an immutable collection?

前端 未结 30 1105
夕颜
夕颜 2020-11-29 16:47

Sorry for the waffly title - if I could come up with a concise title, I wouldn\'t have to ask the question.

Suppose I have an immutable list type. It has an operat

相关标签:
30条回答
  • 2020-11-29 17:27

    I like mmyers suggestion of CopyAndAdd. In keeping with a "mutation" theme, maybe you could go with Bud (asexual reproduction), Grow, Replicate, or Evolve? =)

    EDIT: To continue with my genetic theme, how about Procreate, implying that a new object is made which is based on the previous one, but with something new added.

    0 讨论(0)
  • 2020-11-29 17:27

    A few random thoughts:

    • ImmutableAdd()
    • Append()
    • ImmutableList<T>(ImmutableList<T> originalList, T newItem) Constructor
    0 讨论(0)
  • 2020-11-29 17:28

    In situations like that, I usually go with Concat. That usually implies to me that a new object is being created.

    var p = listA.Concat(listB);
    var k = listA.Concat(item);
    
    0 讨论(0)
  • 2020-11-29 17:29

    Join seems appropriate.

    0 讨论(0)
  • 2020-11-29 17:30

    Apparently I'm the first Obj-C/Cocoa person to answer this question.

    NNString *empty = [[NSString alloc] init];
    NSString *list1 = [empty stringByAppendingString:@"Hello"];
    NSString *list2 = [list1 stringByAppendingString:@"immutable"];
    NSString *list3 = [list2 stringByAppendingString:@"word"];
    

    Not going to win any code golf games with this.

    0 讨论(0)
  • 2020-11-29 17:30

    I think that Plus() and Minus() or, alternatively, Including(), Excluding() are reasonable at implying immutable behavior.

    However, no naming choice will ever make it perfectly clear to everyone, so I personally believe that a good xml doc comment would go a very long way here. VS throws these right in your face when you write code in the IDE - they're hard to ignore.

    0 讨论(0)
提交回复
热议问题