Replies: 6 comments 7 replies
-
In practice, source generators are shipped in NuGet packages that allow participation by including a .props file that sets the project file property. The only thing you couldn't do this way is condition the global usings based on the compilation object model. |
Beta Was this translation helpful? Give feedback.
-
If this implies "compared to cli option", does it mean we need to always use
For namespace imports, it's probably not a good thing, but it'd be really helpful to be able to define global type aliases in generators.
As for the discovery, the "Imports" name is already familiar from Razor. Also you'd usually use
If the interesting scenario involves only one file, there is no point to use namespace directives because that means you need to import immediately. Why would anyone do this? namepsace N;
using N;
var c = new C();
class C {} |
Beta Was this translation helpful? Give feedback.
-
Given that top level statements tend to be used in executables (not libraries) with a handful of files, the namespace doesn't really matter all that much and so I don't think it matters if it's easily missed. Also I don't see any readability issues with it myself? |
Beta Was this translation helpful? Give feedback.
-
I think a syntax form of a global using makes sense, and should be easy enough to understand what's going on with proper tooling. I am still puzzled by this "beginner scenario" you guys seem to be focused on the past few years. C#/.NET is 20 years old and consistently ranks as one of the most used platforms in the world, clearly there is not a real problem introducing it to beginners. If the goal is to compete equally with Python or JavaScript here I don't think that's ever going to happen, as static typing will always add complexity that seems obtuse to those who are just learning. I think the best thing for beginners (and the reason .NET became successful in the first place) is the tooling stack. The fact that VB6 and then .NET Windows Forms and ASP .NET Forms shipped with WYSISYG editors made them unbeatable as tools for starting developers. Microsoft has largely forfeited that advantage as the industry has largely moved to client-side web technology stacks (though Xamarin for mobile does fill a small niche there). I strongly disagree in the LDT's statement that the vast majority of .NET code is in simple projects. If you guys are just looking at public Github repos this introduces a huge sampling bias, as the vast majority of companies keep their code private. The people keeping C# alive are professional developers who write complex applications everyday (and actually pay Microsoft's bills via Azure, Office, and Windows licenses). |
Beta Was this translation helpful? Give feedback.
-
The C# official tutorial seems to use global usings substantially: I think online playground is the best thing for beginner scenario and the C# tutorial is awesome in this sense. However, current implementation of the tutorial is dreadful - just adding the following code before an edited code. using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Program
{
class Program
{
static void Main()
{ Thus we can write a weird code like this: We need top-level statements and global usings to avoid this kind of weird code. |
Beta Was this translation helpful? Give feedback.
-
I just had this issue with top-level statements: Due to the generated class being in the global namespace rather than the project's root, you get different binding on extension methods. using System;
namespace Root
{
class Program
{
public static void Main() => Console.WriteLine(new object().M()); // prints 'Root'
}
}
namespace Root { static class E { public static string M(this object o) => nameof(Root); } }
namespace System { static class E { public static string M(this object o) => nameof(System); } } Using top-level statements, the same program prints |
Beta Was this translation helpful? Give feedback.
-
https://github.com/dotnet/csharplang/blob/master/meetings/2021/LDM-2021-01-13.md
Agenda
Beta Was this translation helpful? Give feedback.
All reactions