programming language feature idea

Started by
12 comments, last by Ravyne 11 years ago

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?

Advertisement
perhaps i misunderstand, but isn't this what lambda's do?
Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.
This is usually called nested functions.

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

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. And read here.

 

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.

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.

Beginner in Game Development?  Read here. And read here.

 

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.
[size=2][ 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 ]

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.

throw table_exception("(? ???)? ? ???");

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.

This topic is closed to new replies.

Advertisement