Something that should be implemented in the next new language

Started by
5 comments, last by goomyman 21 years, 10 months ago
I dont know if there is something implemented like this but I always run into the same tedious task. I want a RunOnce function or object type implemented into the language. I know i could write my own but this is something im sure is common with every program. If you want something to only run once but you put it in a section of code that gets read over and over, Exactly like static but for function calls. HMM maybe you can use static that way. I end up using a static bool value = false.. and then when i want to run the object once I do this: if(value == false) { foo(); // this is the function i only want to call once value = true; } There has to be a better way to do this. I dont know the best way to fix it but im sure someone out there has an idea. Its so common especially in game development for me when I am playing sounds. I think it should be implemented into a language . Maybe it is in some language already i dont know. What do you guys think? [edited by - goomyman on June 3, 2002 12:01:03 AM]
Advertisement
have a static variable in the function.
called something like static int TimesCalled = 0;
everytime it''s called go
TimesCalled++;

if (TimesCalled >= MAX)
return;

so max could be 1,2,3...etc.

one problem of this is for class functions, since every instance would have this and it might causes problems.
-Pac "The thing I like about friends in my classes is that they can't access my private members directly." "When listening to some one tell about their problem (whether it's code or not), don't listen to what went right or wrong, but what they assumed....."
You can use a static class object in which constructor does the requred work:

class RunOnce {
RunOnce() { /* code here */ };
] g_runOnce;
---visit #directxdev on afternet <- not just for directx, despite the name

while(1)
{
static int bob=runonce();
/* ... */
}
Well, if a new language was created that had this feature perhaps the syntax would be something like this:

static
{
...
}

This would cause the code to be executed once, or if you wanted it to be executed a limited number of times (but more than once), you could do something like this:

static (n)
{
...
}

Where n is the number of times to execute (though in this case perhaps a new keyword should be invented?).

Another thing I think languages should have is the ability to return more than one value from a function. Note: Passing in variables using pointers ISN'T the same as returning multiple values (although it has the same effect). The syntax would be:

(int, int, char) my_func(...)
{
int a, b;
char c;
...
return (a, b, c);
}

int main(void)
{
(int a, int b, char c) = my_func();
(int d,,) = my_func();
return 0;
}

Of course if there was only one variable being returned it would look like normal. Does anybody else have ideas for new syntax?

Give a man a fish and you feed him for a day; teach him to use the Net and he won't bother you for weeks.


[edited by - thooot on June 9, 2002 12:16:59 PM]
c++ has the power to code everything you want in a sweet way..

for the multiple returns, look up in the boost library for tuple, for the other one, write a baseclass with templated parameterlist or so and you get it.. what you''re looking for is functors, or function objects. you can write a functor base class wich has a callcounter..

"take a look around" - limp bizkit
www.google.com
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia

My Page davepermen.net | My Music on Bandcamp and on Soundcloud

Lua handles multiple returns. Or was that Python? Maybe both.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files ]

This topic is closed to new replies.

Advertisement