Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: A simple for loop #1356

Closed
gulshan opened this issue Mar 6, 2018 · 15 comments
Closed

Proposal: A simple for loop #1356

gulshan opened this issue Mar 6, 2018 · 15 comments

Comments

@gulshan
Copy link

gulshan commented Mar 6, 2018

This is a tiny feature request and does not actually solve any specific problem. When encountered for loops in C, I did not get that much resemblance between the name for and the looping mechanism it provides. In my mind, to bear the resemblance, for would look something simpler, like this-

for(n)
{
    // This loop will run "for" "n" times
    // n can be short/int/long
}

So, hereby, I am requesting this feature in C#.

@orthoxerox
Copy link

How often do you need a loop that will run n times with no way to determine the current iteration?

@magol
Copy link

magol commented Mar 6, 2018

I agree that current C-inspired for-loop is not the best. It's clumsy to write and unnecessarily verbose. The reason it looks like it does is for flexibility. But in C#, we can not take advantage of all the flexibility that C offers.

When you use for-loops, it is common to increase by 1. So in most cases, it is unnecessary to write i++.

I agree that there should be a shorter notation for counted loops, but the index variable must be included. We maybe can get inspection from other languages.

@Unknown6656
Copy link
Contributor

Unknown6656 commented Mar 6, 2018

@magol: With slices/ranges (#185, #198) one should be able to write this code here soon (I think C#7.3 or greater):

foreach (var _ in 0..10)
{
    // ...
}

It is not super-compact, but I think that is OK. I do not think, that there is a real need for more optimization/compaction

@gulshan
Copy link
Author

gulshan commented Mar 6, 2018

I already said, this does not solve any specific problem. May be new comers will be more comfortable to start with this simple for loop. That's all.

@magol
Copy link

magol commented Mar 6, 2018

@Unknown6656 ah, I had forgotten about that. Thanks for the reminder about slices/ranges :-)
It is not super compact, but it is easier to read and write then the for-syntax.

@bondsbw
Copy link

bondsbw commented Mar 6, 2018

@Unknown6656 Honestly though that isn't strictly better than

foreach (0..10)
{
    // ...
}

It might not hit the +100 point mark but I think it's worth considering.

@Unknown6656
Copy link
Contributor

Unknown6656 commented Mar 7, 2018

other thought -- relying on "extension operators"(#515) and copying C's implicit int to bool conversion:

int count = 10;

while (--count)
{
    // ....
}

with the following operator somewhere defined in the background:

internal static implicit operator bool (int v) => v != 0;

@bondsbw
@magol

@bondsbw
Copy link

bondsbw commented Mar 7, 2018

That makes me sad.

@Unknown6656
Copy link
Contributor

Unknown6656 commented Mar 8, 2018

@bondsbw I know ....... me too ......

But it is a brutal world out there ....
I've heard there are even languages out there, where you do not define a variable's type, as it could be anything at runtime...... Or even worse: change it's type at runtime............


If you see signs of Javascript, Python or PHP - it is not yet too late.
We are here to help you.
#notice_the_signs

@mburbea
Copy link

mburbea commented Mar 8, 2018

I've personally always been a fan of this looping construct::

int count = 10;
while(count --> 0){
  // ...
}

The --> or goes to operator makes it so explicit.

@jnm2
Copy link
Contributor

jnm2 commented Mar 8, 2018

Why not scope the iteration variable?

for (var count = 10; count --> 0;)
{
  // ...
}

@lachbaer
Copy link
Contributor

lachbaer commented Mar 9, 2018

for (var i = 1 to 10 step 2) { ... }

just kidding... 🤣

@MkazemAkhgary
Copy link

This is my preferred one.

for (object boxedCounter = Activator.CreateInstance(Type.GetType("System.Decimal"), Activator.CreateInstance(Type.GetType("System.Int32")));
        (Type.GetType("System.Int32").GetRuntimeMethods().Where(m => m.Name.SequenceEqual("Equals")).ToArray()[0].Invoke(Type.GetType("System.Decimal").GetRuntimeMethods().Where(m => m.Name.SequenceEqual("CompareTo")).ToArray()[0].Invoke((decimal)boxedCounter,
        new object[] { Activator.CreateInstance(Type.GetType("System.Decimal"), Type.GetType("System.Int32").GetRuntimeMethods().Where(m => m.Name.SequenceEqual("Parse")).ToArray()[0].Invoke(null, new object[] { "10" })) }), new object[] {
            Type.GetType("System.Int32").GetRuntimeMethods().Where(m => m.Name.SequenceEqual("Parse")).ToArray()[0].Invoke(null, new object[] { "-1" })})).ToString().Cast<object>().SequenceEqual(new object[] { 'T', 'r', 'u', 'e'});
        boxedCounter = Type.GetType("System.Decimal").GetMethod("op_Addition").Invoke(null, new object[] { (decimal)boxedCounter, Activator.CreateInstance(Type.GetType("System.Decimal"), Type.GetType("System.Int32").GetRuntimeMethods().Where(m => m.Name.SequenceEqual("Parse")).ToArray()[0].Invoke(null, new object[] { "1" })) }))
{
    var i = (int)(decimal)boxedCounter;
}

@Nukepayload2
Copy link

What about this?

#pragma language using VB
For i = 0 To 10
#pragma language restore
    Console.WriteLine(i);
}

@AustinBryan
Copy link

namespace MyNamespace
{
    public static void For<T>(int length, Func<T> body)
    {
        for (int i = 0; i < length; i++)
             body();
    }
}

...

using static MyNamespace.For;

public static Main() => For(10, () => 
{
    Console.WriteLine();
});

@gulshan gulshan closed this as completed Jul 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests