3 ms·
The following code is legal C++ and does exactly what you’d expect it to do. auto result = "Hello" <repeat> 3 <join> ", "; I had no idea what that might do. N
by EliRivers 12y ago
The following code is legal C++ and does exactly what you’d expect it to do.
auto result = "Hello" <repeat> 3 <join> ", ";
I had no idea what that might do. No expectations at all. Is this an attempt to write language X in language Y?
- epistasis 12y agoThis is fully explained in the primary link if you read past the tl;dr section.
- MaulingMonkey 12y agoI think the point is that this isn't... necessarily idiomatic C++.
- coldtea 12y ago>I had no idea what that might do. No expectations at all. Really? How is it any different than: "Hello".repeat(3).join(",");
- coldpie 12y agoMostly because the order of operations is unclear. I read it as producing "HelloHelloHello, ". That is, repeat the previous thing three times and then concatenate that with the string ", ". It's surprising to me that "repeat" results in a new object with new semantics.
- ilitirit 12y agoDepends on the language I guess. I read it as "repeat the string Hello thrice and then add a comma and space". It's not really clear that "repeat" returns a list. A C# equivalent would be: String.Join(", ", Enumerable.Repeat("Hello", 3)) Here it's easier to see that Repeat returns an Enumerable object. Using an Extension Method, it's easy to see how someone could mistake the output: https://ideone.com/DuIvZN https://ideone.com/DuIvZN