0

I've seen this present in several codebases, and more recently, in Pytorch's codebase, e.g., here https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/core/TensorBase.h#L490-L492

  TensorOptions options() const {
    return TensorOptions().dtype(dtype())
                          .device(device())
                          .layout(layout());
  }

where a member function is set up to to return a chain of other member functions, and each member function returns the same type (in this case, it's TensorOptions).

I was wondering if there's a particular name for this type of OOP design, and what the advantage of it is?

user5965026
  • 465
  • 5
  • 16
  • 2
    [Method chaining](https://isocpp.org/wiki/faq/references#method-chaining) – PaulMcKenzie Feb 11 '22 at 21:03
  • When used to create a new object, this is also called the [Named Parameter Idiom](https://isocpp.org/wiki/faq/ctors#named-parameter-idiom) which is a special case of method chaining. – alter_igel Feb 11 '22 at 21:06
  • One disadvantage is that prior to C++17 (I believe), the method chain was not guaranteed to be called from left to right. In other words, `layout` might be called before `device`. Now after C++17 (or C++20, not sure), the chain of calls is guaranteed to go from left to right (or in your case, top to bottom). – PaulMcKenzie Feb 11 '22 at 21:08
  • 2
    [Fluent Interfaces](https://en.wikipedia.org/wiki/Fluent_interface) also heavily apply method chaining. – Nexevis Feb 11 '22 at 21:08
  • @PaulMcKenzie - Are you referring to the member `layout` or `layout()` expression? (poor naming on OP's part). Because the members were always guaranteed to evaluate in chain order. The object expression of one is the result of the previous one, they cannot be re-ordered willy-nilly. – StoryTeller - Unslander Monica Feb 11 '22 at 21:12
  • @StoryTeller-UnslanderMonica Referring to the actual calling sequence, `layout()`, `device()`, etc. Older versions of C++, you weren't guaranteed the calling order. C++ programmers have been bit hard by this quirk using method chaining. Now it's been all straightened out (again, not sure of the C++ standard version where this has been "fixed"). – PaulMcKenzie Feb 11 '22 at 21:17
  • @PaulMcKenzie - That's domstrably wrong. Arguments to a function were unsequenced with respect to eachother, but always evaluated *before* the function itself. – StoryTeller - Unslander Monica Feb 11 '22 at 21:20
  • @StoryTeller-UnslanderMonica [I am referring to this](https://stackoverflow.com/questions/37252328/c-execution-order-in-method-chaining). – PaulMcKenzie Feb 11 '22 at 21:21
  • @PaulMcKenzie - Then you'd note the methods *are* evaluated in order, but their arguments are not. That's why I asked you to what you meant precisely in the OP. Without precision, all we do is spread FUD. – StoryTeller - Unslander Monica Feb 11 '22 at 21:25
  • The term I think of when I see code like that is "functional programming". – Logicrat Feb 11 '22 at 21:30

0 Answers0