是否可以像 LINQ 一样创建 C# 语言修改?

发布于 2024-12-02 04:17:53 字数 836 浏览 3 评论 0原文

我一直在查看 先生。 Skeet 关于如何重新实现 LINQ 的博客

他特别指出,代码:

var list = (from person in people
        where person.FirstName.StartsWith("J")
        orderby person.Age
        select person.LastName)
       .ToList(); 

被转换为 LINQ 库提供的扩展方法:

people.Where(person => person.FirstName.StartsWith("J"))
  .OrderBy(person => person.Age)
  .Select(person => person.LastName) 

由编译器。

我的问题是,如何一个库是否给大佬们留下了足够的印象,让他们允许改变语言来支持这个库?或者这些词在 LINQ 出现之前就已经被保留了?

I've been looking quite a bit at Mr. Skeet's blog on how to re-implement LINQ.

In particular, he states that the code:

var list = (from person in people
        where person.FirstName.StartsWith("J")
        orderby person.Age
        select person.LastName)
       .ToList(); 

is translated to methods that are extension methods that are provided by the LINQ library:

people.Where(person => person.FirstName.StartsWith("J"))
  .OrderBy(person => person.Age)
  .Select(person => person.LastName) 

BY THE COMPILER.

My question is, how does one impress the bigwigs enough with a library to cause them to allow the language to change to support the library? Or were those words already reserved before LINQ came along?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不疑不惑不回忆 2024-12-09 04:17:53

使用 Mono C# 编译器 - 它是开源的,您可以进行任何您想要且 .net 支持的语言修改,例如,使用枚举作为泛型约束、创建返回值类型引用的方法 (public ref int Max(ref int x, ref int y) { if (x>y) return ref x; else return ref y; }),它们具有受保护或内部可见性等

。 C# 的不兼容派生,但如果你足够努力,那么人们可能会喜欢它。

另一个选择:创建一个博客,为其提出一些非常好的用例,可能是 .net 语言的示例实现或使用自定义编译器,展示它解决了什么问题以及为什么这将是一个巨大的胜利,证明了它的合理性指定、设计、开发、测试和记录功能的成本。

Grab the Mono C# Compiler - it's open source and you can do whatever language modifications you want and which .net supports, e.g., use enums as generic constraints, create methods that return references to value types (public ref int Max(ref int x, ref int y) { if (x>y) return ref x; else return ref y; }), that have protected or internal visibility etc.

Of course, you are then creating an incompatible derivate of C#, but if you push it hard enough then people might like it.

The other option: Start a blog, come up with some really good use cases for it, possibly a sample implementation in a .net language or using a customized compiler, show what problem it solves and why this would be a big win that justifies the cost that goes into specifying, designing, developing, testing and documenting of the feature.

红衣飘飘貌似仙 2024-12-09 04:17:53

我非常怀疑编译器开发人员是否会为任何库实现语法扩展。如果您确实想要语言级集成,您可以开发一个预处理器,将自定义语法转换为有效的 C#。无论如何,这本质上就是编译器对 LINQ 所做的事情(正如您在问题中指出的那样)。

当然,您会失去 Visual Studio 中的自动完成和语法突出显示等功能,但这可以通过扩展来修复。

I highly doubt the compiler developers would implement syntax extensions for just any library. If you really wanted language-level integration, you could develop a pre-processor that transforms your custom syntax into valid C#. That's essentially what the compiler does with LINQ anyway (as you pointed out in your question).

Of course, you would lose things like auto-complete and syntax highlighting in Visual Studio, but that could be fixed with an extension.

浅沫记忆 2024-12-09 04:17:53

某些语言允许扩展其语法和语义。最接近 C# 的是 Nemerle (它甚至支持 C# 的安全子集,您可以,反过来,按照你喜欢的方式扩展),但是几乎任何 Lisp 都可以做到这一点。因此,如果您使用的语言足够强大,则无需“打动”任何人 - 任何库都可以向语言本身添加新功能。

有传言称下一个 C# 还将提供一些基本的元编程支持,但我找不到任何具体细节。

Some languages allow to extend their syntax and semantics. The closest to C# is Nemerle (and it even supports a safe subset of C#, which you can, in turn, extend as you like), but the same can be done with almost any Lisp, for example. So, if you're using a language powerful enough, you don't need to "impress" anyone - any library can add new functionality to a language itself.

There were rumors that the next C# will provide some rudimentary metaprogramming support as well, but I could not find any specifics.

秋凉 2024-12-09 04:17:53

这是可能的,但会很困难,他们可能会重新实现这个想法以更好地适应他们的语言结构。新的异步功能类似于名为 AsyncEnumerator 的库,但他们正在构建一切以更好地适应该语言。 LINQ 的关键字没有提前保留,但它们是上下文关键字,这意味着可以存在与 LINQ 上下文之外的该关键字匹配的标识符。当编译器检测到 LINQ 构造时,它会进入 LINQ 模式,其中这些关键字是实际的保留字。

It is possible but it will be hard and they'll probably reimplement the idea to better fit their language constructs. The new async feature is similar to a library called AsyncEnumerator for example but they are building everything to better suit the language. The keywords for LINQ were not reserved in advance but they are contextual keywords meaning that there can be identifiers that match this keywords out of the LINQ context. When the compiler detects a LINQ construct it goes into LINQ mode where these keywords are actual reserved words.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文