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

Ideas for new features on C# 7.0 or beyond... #14549

Closed
Rekkonnect opened this issue Oct 16, 2016 · 20 comments
Closed

Ideas for new features on C# 7.0 or beyond... #14549

Rekkonnect opened this issue Oct 16, 2016 · 20 comments

Comments

@Rekkonnect
Copy link
Contributor

Rekkonnect commented Oct 16, 2016

I have a few features to suggest for the future of this language which I first learn and love.

  • All keyword: Basically a useful keyword used in multi-assignment. It should set the values of all the declared variables to the value specified on the right hand. Here is an example:
    int int1, int2, int3, int4, int5 = all 0;
  • Multi-comparison: Can't really find a good name for that one; I can only explain well with the following example:
    bool test = 1 < 2 <= 4 > 3 >= 2 == 2 != 3 // Should return true
    The operators that could be used like that are the following: ==, >, <, >=, <=, !=, since || and && already support such a use.

That's all I got for now; thanks for your time!

EDIT: As @svick said the second proposal is a duplicate, but this could maybe taken into account for multiple operators, too! (Example was even edited)

@svick
Copy link
Contributor

svick commented Oct 16, 2016

The second proposal is a duplicate of #136, which was closed by @gafter:

The syntax a < b < c is already syntactically valid, so we cannot co-opt that to mean something else.

@Rekkonnect
Copy link
Contributor Author

@svick Thanks for letting me know!

@dsaf
Copy link

dsaf commented Oct 17, 2016

Tuple masters, is it possible to do this?

(int1, int2, int3, int4, int5) = 0;

"Deconstruct" a single numeric value by copying it.

@dsaf
Copy link

dsaf commented Oct 17, 2016

@Kalfas an issue per suggested feature is usually preferred, especially when they are unrelated.

@ufcpp
Copy link
Contributor

ufcpp commented Oct 17, 2016

@dsaf No it doesn't but, with target typed default, might be possible:

(int1, int2, int3, int4, int5) = default;

This is equivalent to:

(int1, int2, int3, int4, int5) = default((int ,int ,int ,int int));

@orthoxerox
Copy link
Contributor

@ufcpp There are no target types in your example.

@ufcpp
Copy link
Contributor

ufcpp commented Oct 17, 2016

@orthoxerox Yes if int1 ~ int5 are new variables. I meant:

int int1, int2, int3, int4, int5;
(int1, int2, int3, int4, int5) = default

or

(int int1, int int2, int int3, int int4, int int5) = default

@Rekkonnect
Copy link
Contributor Author

@ufcpp Still you have to type too much... I suggest something like the example I gave; which pretty much makes your life easier, since it combines multi-assignment and multi-declaration!

@dsaf
Copy link

dsaf commented Oct 17, 2016

@Kalfas they are not going to introduce a new keyword all for such a miniscule use case. Maybe if majority of C# developers were writing math code most of the time... Also see #4294.

@gafter
Copy link
Member

gafter commented Oct 17, 2016

Tuple masters, is it possible to do this?

(int1, int2, int3, int4, int5) = 0;

"Deconstruct" a single numeric value by copying it.

Yes, but the syntax is a bit different:

int1 = int2 = int3 = int4 = int5 = 0;

@ufcpp
Copy link
Contributor

ufcpp commented Oct 18, 2016

I came up with an idea, but abuse:

using System;

static class TupleExtensions
{
    public static void Deconstruct<T>(this T x, out T x1, out T x2) { x1 = x; x2 = x; }
    public static void Deconstruct<T>(this T x, out T x1, out T x2, out T x3) { x1 = x; x2 = x; x3 = x; }
    public static void Deconstruct<T>(this T x, out T x1, out T x2, out T x3, out T x4) { x1 = x; x2 = x; x3 = x; x4 = x; }
    public static void Deconstruct<T>(this T x, out T x1, out T x2, out T x3, out T x4, out T x5) { x1 = x; x2 = x; x3 = x; x4 = x; x5 = x; }
}

class Program
{
    static void Main(string[] args)
    {
        var (a1, a2) = 2;
        var (b1, b2, b3) = 'a';
        var (c1, c2, c3, c4) = "abc";
        var (d1, d2, d3, d4, d5) = 1.23;
        Console.WriteLine(string.Join(", ", a1, a2, b1, b2, b3, c1, c2, c3, c4, d1, d2, d3, d4, d5));
    }
}

@Rekkonnect
Copy link
Contributor Author

Rekkonnect commented Oct 19, 2016

@dsaf "Maybe if majority of C# developers were writing math code most of the time..."

Not necessarily, my friend. You see, this is mostly useful for declaring fields on the main classes of the applications. For instance, I may have 15 or more variables with the exact same value assigned on startup; false. Wouldn't it be really useful to just say all false and end this thing up?

@dsaf
Copy link

dsaf commented Oct 19, 2016

@Kalfas what would be an example of such a main class?

@DavidArno
Copy link

@Kalfas,

For instance, I may have 15 or more variables with the exact same value assigned on startup

Really? Like @dsaf, I'd be interested in an example of such a method.

@Rekkonnect
Copy link
Contributor Author

Rekkonnect commented Oct 20, 2016

public static bool autoLoadSettings = true;
public static bool autoSaveSettings = true;
public static bool allowIDsAbove999 = true;
public static bool showExitProgramUnsavedChangesWarnings = true;
public static bool showExitHSVUnsavedChangesWarnings = true;
public static bool showAllWarnings = true;
public static bool showAboveID999Warnings = true;
public static bool showCustomObjObjsLimWarning = true;
public static bool showObjLimWarning = true;
public static bool showCustomObjLimWarning = true;
public static bool allowGroupIDsAbove999 = false;
public static bool allowColorIDsAbove999 = false;
public static bool changeObjLimMsg = false;
public static bool changeCustomObjLimMsg = false;
public static bool changeCustomObjsObjLimMsg = false;
public static string defaultSettingsPath = "settings.esf";
public static string objLimMsg = "";
public static string customObjLimMsg = "";
public static string customObjsObjLimMsg = "";
public static int undoRedoValues = 256;

Is that enough? @DavidArno @dsaf

@DavidArno
Copy link

@Kalfas,

public static bool ...

Um, apologies if this is rude, but seriously, you actually put real-life mutable, global variables in your code? In 2016?

Sorry, but I need a little lie down. I'm in shock. 😱

@Rekkonnect
Copy link
Contributor Author

@DavidArno How else could they be accessible from the other forms as well?

Plus, is there anything bad with making public variables? Lastly, the main point here is about the multi-assignment and not the fact that I use global variables in the main form so that they are accessible from other forms, too.

P.S. My experience is only a year; bear with me, okay?

@dsaf
Copy link

dsaf commented Oct 20, 2016

@Kalfas the code is fine for one year of experience, the suggestion is fine apart for requesting a new keyword/syntax. What matters is:

  1. this issue has already been marked with Discussion yellow label of death, which means it is not going to be seriously considered;
  2. the team is so opposed to introducing new keywords/syntax for use cases that are deemed minor, that even scoping rules were recently made quite inconsistent just to avoid adding Swift's guard:

#12597
#12939
#14615 (comment)

...so adding a new keyword all is just not going to happen.

@Rekkonnect
Copy link
Contributor Author

How about the ability to add custom keywords or something like that then?

@DavidArno
Copy link

DavidArno commented Oct 21, 2016

@Kalfas

How about the ability to add custom keywords or something like that then?

Whilst it's not universally true, a good rule of thumb in programming is that if something is proving painful, then you are likely doing it wrongly. There are numerous reasons why having many public static fields in a class that all need initialising is "doing it wrongly". This relates to lots of fancy buzzwords, such as the single responsibility principle, coupling, cohesion, ease of testing, avoiding mutability, encapsulation, dependency injection, "tell, don't ask" and the like. It's beyond the scope of Roslyn issues to teach you about them. But over time, you'll learn about these topics. As you do, you'll learn better ways of programming, the size of that list of fields will decrease, and the pain will go away.

Adding an all keyword (either officially, or somehow via custom keywords) might dull the pain, but it would be treating the symptoms; not the cause. You are better off - in the long term - without it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants