I have a few features to suggest for the future of this language which I first learn and love.
int int1, int2, int3, int4, int5 = all 0;
bool test = 1 < 2 <= 4 > 3 >= 2 == 2 != 3 // Should return true
==
, >
, <
, >=
, <=
, !=
, 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)
The second proposal is a duplicate of https://github.com/dotnet/roslyn/issues/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.
@svick Thanks for letting me know!
Tuple masters, is it possible to do this?
(int1, int2, int3, int4, int5) = 0;
"Deconstruct" a single numeric value by copying it.
@Kalfas an issue per suggested feature is usually preferred, especially when they are unrelated.
@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));
@ufcpp There are no target types in your example.
@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
@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!
@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.
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;
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));
}
}
@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?
@Kalfas what would be an example of such a main class?
@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.
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
@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. :scream:
@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?
@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
:
https://github.com/dotnet/roslyn/issues/12597
https://github.com/dotnet/roslyn/issues/12939
https://github.com/dotnet/roslyn/issues/14615#issuecomment-254938696
...so adding a new keyword all
is just not going to happen.
How about the ability to add custom keywords or something like that then?
@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.
Most helpful comment
Yes, but the syntax is a bit different:
int1 = int2 = int3 = int4 = int5 = 0;