Jump to content

  • Log In with Google      Sign In   
  • Create Account


programming language feature idea


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
13 replies to this topic

#1 JohnnyCode   Members   -  Reputation: 95

Posted 26 March 2013 - 07:54 PM

Hi,

 

I got an idea about code language idea and it fascinates me, so I would be happy to share it and get critics since I think it is a reasonable feature.

C was a language that promissed runtime variables allocations, as opposed to Pascal. Leading noob programmers to pit of despair. but utilizing a rather fast and absolute control over RAM. Java and C# took some hidden managment of this practice to save programmer and production from ML bugs. Those two languages also eased function definition. And this is my idea -"Inline Function Definition".

Many times I was dived in a large function full of code and I decided I need a function, precisely for the scope of the function I was in, or the class I was in. For example 

public void Instructions()
{
.
.
.
string thelogiclyvarying="constblaconstblacnsta"+ id+"ocnstbla"+onto+"asfloaksf"+wouldlike+"constbla"; /* those variables on right side do not exist and exist and vary in cycle */
//so I would do
public string Compose(string id, string onto,string wouldlike)
{
return "constblaconstblacnsta"+ id+"ocnstbla"+onto+"asfloaksf"+wouldlike+"constbla"; 
}
while (sqlreader.Read())
{
m_sContent+=Compose(sqlreader["odID"],sqlreader["ontorequest"],sqlreader["wouldlike"]);
}
}

You could say I could just write the line- defintion of the function, to the cycle, but I am using this string in more places of the code, plus, if you got habit of doing it, you could clear the code much, plus the coding enviroment could hide the definitions or such.

 

So would you want this compiler feature?

Would it lead to mess or is it against code writing culture?



Sponsor:

#2 slicer4ever   Crossbones+   -  Reputation: 2600

Posted 26 March 2013 - 07:56 PM

perhaps i misunderstand, but isn't this what lambda's do?

Edited by slicer4ever, 26 March 2013 - 11:13 PM.

Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

#3 SiCrane   Moderators   -  Reputation: 9124

Posted 26 March 2013 - 08:00 PM

This is usually called nested functions.

#4 JohnnyCode   Members   -  Reputation: 95

Posted 26 March 2013 - 08:25 PM

great, thanks! what a shame it is not featured in C#, anonymous methods seem too inpractic for me.



#5 Alpha_ProgDes   Crossbones+   -  Reputation: 4506

Posted 26 March 2013 - 08:44 PM

great, thanks! what a shame it is not featured in C#, anonymous methods seem too inpractic for me.

 

How is an anonymous method too impractical? Isn't it exactly what you described?


Beginner in Game Development? Read here.
 
Super Mario Bros clone tutorial written in XNA 4.0 [MonoGame, ANX, and MonoXNA] by Scott Haley
 
If you have found any of the posts helpful, please show your appreciation by clicking the up arrow on those posts Posted Image
 
Spoiler

#6 Nypyren   Crossbones+   -  Reputation: 3415

Posted 26 March 2013 - 09:31 PM

You mean like this?
var m_sContent = string.Join("\n", sqlreader.Select(x => Compose(x.odID, x.ontorequest, x.wouldLike)));
Or this?
var m_sContent = string.Join("\n", sqlreader.Select(x => string.Format("constblaconstblacnsta{0}ocnstbla{1}asfloaksf{2}constbla", x.odId, x.ontorequest, x.wouldLike)));
Or this?
Func<dynamic,string> compose = x => string.Format("{0} {1} {2}", x.odId, x.ontorequest, x.wouldLike));
var m_sContent = string.Join("\n", sqlreader.Select(compose));
Seriously. C# has got you covered.

Edited by Nypyren, 26 March 2013 - 09:45 PM.


#7 Alpha_ProgDes   Crossbones+   -  Reputation: 4506

Posted 26 March 2013 - 09:41 PM

You mean like this?
 

 

var m_sContent = string.Join("\n", sqlreader.Select(x => Compose(x.odID, x.ontorequest, x.wouldLike)));

//Or this?

var m_sContent = string.Join("\n", sqlreader.Select(x => string.Format("constblaconstblacnsta{0}ocnstbla{1}asfloaksf{2}constbla", 
               x.odId, x.ontorequest, x.wouldLike)));

//Or this?

Func<SQLElement,string> compose = x => string.Format("{0} {1} {2}", x.odId, x.ontorequest, x.wouldLike));
var m_sContent = string.Join("\n", sqlreader.Select(compose));
 

 




Seriously. C# has got you covered.


Edited by Alpha_ProgDes, 26 March 2013 - 09:41 PM.

Beginner in Game Development? Read here.
 
Super Mario Bros clone tutorial written in XNA 4.0 [MonoGame, ANX, and MonoXNA] by Scott Haley
 
If you have found any of the posts helpful, please show your appreciation by clicking the up arrow on those posts Posted Image
 
Spoiler

#8 Cornstalks   Crossbones+   -  Reputation: 6882

Posted 26 March 2013 - 11:07 PM

Yup, C# already has those, as others have mentioned. Nypyren has posted some decent idomatic solutions, but I'll give a more "direct" translation from your posted code to real C# code (assuming I don't make a mistake):
public void Instructions()
{
    string thelogiclyvarying="constblaconstblacnsta"+ id+"ocnstbla"+onto+"asfloaksf"+wouldlike+"constbla"; /* those variables on right side do not exist and exist and vary in cycle */
    // And all the magic happens in the next line...
   Func<string, string, string, string> compose = (id, onto, wouldlike) =>
   {
      return "constblaconstblacnsta"+ id+"ocnstbla"+onto+"asfloaksf"+wouldlike+"constbla"; 
   }

   while (sqlreader.Read())
   {
      m_sContent += compose(sqlreader["odID"],sqlreader["ontorequest"],sqlreader["wouldlike"]);
   }
}
Other languages have lambda functions too. C++, C#, every functional language, etc.
[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

#9 Ravyne   Crossbones+   -  Reputation: 5670

Posted 27 March 2013 - 03:47 PM

Lambda is what we get in C++11, and aside from the somewhat odd syntax it does everything you're asking. In Pre-C++11 days you could accomplish the same by nesting a functor class, although the generally-large amount of boiler-plate code needed to do so was such that it was often impractical--works exactly the same though, I once had to backport some early C++11 code to a compiler that didn't support lambda syntax, so I became quite familiar with that boilerplate unfortunately.

 

I'd like to have plain old local functions though, and lots of languages have them (D is the only C-like language with direct support). Its not even that they're all that useful--most of the places you'd use them you want a lambda anyway--its more a matter of orthogonality to me, and of giving programmers the means to manage scope and such as they see fit.



#10 BGB   Crossbones+   -  Reputation: 1458

Posted 27 March 2013 - 05:31 PM

Lambda is what we get in C++11, and aside from the somewhat odd syntax it does everything you're asking. In Pre-C++11 days you could accomplish the same by nesting a functor class, although the generally-large amount of boiler-plate code needed to do so was such that it was often impractical--works exactly the same though, I once had to backport some early C++11 code to a compiler that didn't support lambda syntax, so I became quite familiar with that boilerplate unfortunately.

 

I'd like to have plain old local functions though, and lots of languages have them (D is the only C-like language with direct support). Its not even that they're all that useful--most of the places you'd use them you want a lambda anyway--its more a matter of orthogonality to me, and of giving programmers the means to manage scope and such as they see fit.

 

C++11 lambda does, of course, have its slightly funky syntax and semantics though, though it has been done worse... (*cough* Java *cough*).

granted, I suspect the absence of GC may play into this some (as well as keeping it parsable).

 

as for lambda vs nested functions:

for a lot of languages where lambdas are supported, the compiler is smart enough at least to generally figure out where a cheaper (non-closure) nested-functions can be used instead.

 

or, if you mean the ability to declare nested functions like normal functions/methods (vs having to use a variable and store a lambda into it), then yes, this is a useful feature.



#11 JohnnyCode   Members   -  Reputation: 95

Posted 27 March 2013 - 09:15 PM

You mean like this?

var m_sContent = string.Join("\n", sqlreader.Select(x => Compose(x.odID, x.ontorequest, x.wouldLike)));
Or this?
var m_sContent = string.Join("\n", sqlreader.Select(x => string.Format("constblaconstblacnsta{0}ocnstbla{1}asfloaksf{2}constbla", x.odId, x.ontorequest, x.wouldLike)));
Or this?
Func<dynamic,string> compose = x => string.Format("{0} {1} {2}", x.odId, x.ontorequest, x.wouldLike));
var m_sContent = string.Join("\n", sqlreader.Select(compose));
Seriously. C# has got you covered.

Thanks, but it seems too unreadable to me, and my production leaders would not be happy about this habit, plus, I myself am not quite getting it, I think I am getting old :(. Also, the last example seems to be a rather instance of function that gets disposed out of the block, if I am not mistaken, I would rather have possibility to immediately define a function in the scope of the class, so it would not be:
 redefintiion able, instance utilized... and so on. Thanks, it seems that this practice is not very common upon programers, and it did not get maintained to level of effective and reasonable usage .



#12 JohnnyCode   Members   -  Reputation: 95

Posted 27 March 2013 - 09:17 PM

My mistake I posted in Lounge, cannot rate you up, this was very profesional help! Thanks!



#13 tstrimple   Prime Members   -  Reputation: 1712

Posted 27 March 2013 - 09:41 PM

Thanks, but it seems too unreadable to me, and my production leaders would not be happy about this habit, plus, I myself am not quite getting it, I think I am getting old sad.png. Also, the last example seems to be a rather instance of function that gets disposed out of the block, if I am not mistaken, I would rather have possibility to immediately define a function in the scope of the class, so it would not be:

 redefintiion able, instance utilized... and so on. Thanks, it seems that this practice is not very common upon programers, and it did not get maintained to level of effective and reasonable usage .

 

It's actually quite common with modern C# programmers. I use lambda's quite frequently. 



#14 Ravyne   Crossbones+   -  Reputation: 5670

Posted 27 March 2013 - 10:29 PM

lambdas like those are increadibly common already, and gaining steam. I would also suggest that any 'production leaders' affraid of a little new syntactic sugar every now and then ought to go back amongst the dinosours and take their stone-age coding practices with them. Lambdas are not just modern fluff, or an odd way of defining functions, they enable one to write the kind of code we always meant to but never did before because it was too verbose.

 

Anyway, what's the advantage your hypothetical but non-existant approach holds over the new-to-you but here-today one?






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS