2

I'm coming from Javascript like language where I can make functions like this.

function tween(obj, tweenParams, time, tweenOptions);

This is used like:

tween(mySprite, {x: 10, y: 10}, 1, {startDelay: 1, loops: 5});

If it's not clear, this lerps mySprite's x and y property to 10 over the course of 1 second, looping 5 times with a 1 second delay. tweenParams can contain any values that the obj might have, and tweenOptions is a fixed struct.

I want to know how terse this can reasonable be in C++, struct and array initializes don't seem flexible enough to express this. My best guess involves calling multiple tween functions one property at a time, something like.

TweenOptions opts = {};
opts.startDelay = 1;
opts.loops = 5;
tween(&mySprite.x, 10, 1, opts);
tween(&mySprite.y, 10, 1, opts);

I don't know much about C++ advanced features, maybe operator overloading or custom initializes could help me here?

EDIT: To be clear, I don't expect it to exactly match the Javascript example, I just want to know how terse this can reasonably be.

Jeru Sanders
  • 41
  • 2
  • 4
  • 3
    Unfortunately, C++ currently doesn't have anything this clean. C++20 will add *designated initializers* which will make something like this more doable. At this time, you might have to settle for the Builder pattern. – Justin Jul 20 '17 at 20:15
  • 2
    You wont be able to write `tween(mySprite, {x: 10, y: 10}, 1, {startDelay: 1, loops: 5});` but you will be able to write it as `tween(mySprite, {10, 10}, 1, {1, 5});`. You can learn how with a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – NathanOliver Jul 20 '17 at 20:16
  • 3
    C++ is not JavaScript. the technique you've shown in your JavaScript code has more cons than pros, and is error prone. just think what would happen if someone had a typo. you will have to "settle" on writing things the C++ style. – David Haim Jul 20 '17 at 20:16
  • 1
    @DavidHaim, lack of designated initializers is a con, not a pro. – SergeyA Jul 20 '17 at 20:41
  • @SergeyA maybe in a statically compiled language, in a dynamic language? no way. – David Haim Jul 21 '17 at 07:43

1 Answers1

2

Current versions of C++ do not allow this syntax. However, one of the proposals which according to this post was adopted for C++20, will make it possible by using designated initializers (if, of course it will make it there).

The proposal, available here, borrows C99 syntax of nominating members when using aggregate initialization in C++ classes. Coupled together with C++11 feature of uniform initializaion, it will allow for very similar syntax to the one you are asking.

For example,

struct args {
    int x;
    int y;
};

void foo(args a);
...
foo({.x = 10, .y = 25});
SergeyA
  • 61,605
  • 5
  • 78
  • 137
  • Designated initializers is one of the things I'm looking forward to in C++20. You can do things like this, and it removes the need for quirky named arguments to functions; you can just have the named arguments be part of a struct whose purpose is to be used via a designated initializer. – Justin Jul 20 '17 at 21:11
  • @Justin, no argument here. I also think it's a useful feature, and I'd love to see it. In my view, it greatly reduces potential for errors. – SergeyA Jul 20 '17 at 21:14