Archived

This topic is now archived and is closed to further replies.

Something that should be implemented in the next new language

This topic is 5668 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

while(1)
{
static int bob=runonce();
/* ... */
}

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites