Open vbcodec opened 7 years ago
Agree with @agrapine, in practice when it comes to distinguishing class constructors and function cals, it doesn't really matter. They are both expressions with a value. Whether a function returns a value or a constructor expression evaluated to a value, a value must be created either way. If you're using the presence of a new keyword to "watch out" for allocations, then you will miss all the allocs hidden behind function calls anyway, and you'll get a lot of false positives from structs - you should be using a static analyzer for that instead.
Hi guys! I just looked into MAUI and realized that a declarative UI developing is terrible with a new
keyword. Just look:
readonly State<int> count = 0;
[Body]
View body() => new StackLayout
{
new Label("Welcome to .NET MAUI!"),
new Button(
() => $"You clicked {count} times.",
() => count.Value ++)
)
};
vs
readonly State<int> count = 0;
[Body]
View body() => StackLayout
{
Label("Welcome to .NET MAUI!"),
Button(
() => $"You clicked {count} times.",
() => count.Value ++)
)
};
I use Flutter to develop mobile/web apps and it's very good with an optional new
keyword (Dart team made it optional in language version 2.0). But I were a sceptic too before I tried it.
So, I think that the people who afraid of omitting feature for a new
keyword are living a little in the past. Because at the current moment smart IDEs (like Rider) are able to highlight any omitting parts (like visually add a text 'new' before the constructor calling) or made class creation bold or something else. An example of viewing target-typed new
feature:
This feature may be optional like null-safe
.
So it's really needed for MAUI and it'll be great if it will be a part of C# when MAUI will be released.
LOL. I misread that and thought you said omitting the new keyword was terrible. Looking at them both, I actually prefer the first because it makes it clear where the new components are.
But really, it's a moot point. You can't change the syntax so drastically without breaking backwards compatibility. And the supposed gain is so trivial as to be laughable. Especially when your argument is that the IDE will just display the omitted keyword.
If you have a static factory class, you can write the kind of syntax you want to write without needing new
everywhere or the ability to omit the new
keyword.
For example, if you add this when using Roslyn:
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
Then you can write code like this, which fits the MAUI / SwiftUI / etc. declarative style:
CompilationUnit()
.WithMembers(
SingletonList<MemberDeclarationSyntax>(
GlobalStatement(
ExpressionStatement(
InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName("Console"),
IdentifierName("WriteLine")))
.WithArgumentList(
ArgumentList(
SingletonSeparatedList<ArgumentSyntax>(
Argument(
LiteralExpression(
SyntaxKind.StringLiteralExpression,
Literal("Hello, World!"))))))))))
.NormalizeWhitespace()
(Courtesy of https://roslynquoter.azurewebsites.net)
There are lot situations where
new
keyword can be omitted.var p = new Point();
can be
var p = Point();
Keyword
new
will be required to resolve ambiguity, for example if Point() function and Point class are defined and for other such cases. Benefit is saved space, particularly with calling function and passing many new objects as arguments.