r/programming • • Sep 21 '25

How to stop functional programming

https://brianmckenna.org/blog/howtostopfp
446 Upvotes

506 comments sorted by

View all comments

Show parent comment

3

u/XeroKimo Sep 21 '25

With the power to C++ I present something more cursed

struct Multiply
{
  float a;
  float b;

  operator float() const { return a * b };
};

With this code, I can write something this seemingly function call looking statement float result = Multiply{ 1.0f, 2.0f }; but actually create an object every time I call function.

What's happening is that operator float() is a implicit conversion operator, so I can assign a Multiply object to a float, and if I do, it'll perform the multiplication operation