gpt4 book ai didi

metaprogramming - D对这些功能是否足够强大?

转载 作者:行者123 更新时间:2023-12-04 18:12:22 24 4
gpt4 key购买 nike

在最长的时间里,我想设计一种将可扩展性与效率(以及安全性,易用性等)结合在一起的编程语言,最近我重新发现了D,我想知道D 2.0是否真的是我想使自己成为自己的语言? 。我最喜欢的是元编程的潜力。从理论上讲,D的traits系统能否在编译时启用以下功能?

  • 运行时反射:编译时反射功能是否足以在Java/.NET中构建运行时反射系统?
  • 代码转换:使用元程序创建C#/C++/etc。每次编译D程序的版本(奖励点,如果可以传播doc注释)。
  • 特性。我不是说D中内置的metaprogramming traits,而是object-oriented traits for class composition。 D程序将指示要构成的一组特征,而元程序将构成它们。
  • 单位推断引擎:给出了一些可选的指示单位的符号,例如unit(value),D元程序可以检查以下代码,推断正确的单位并在最后一行发出错误消息吗? (我为boo编写了这样的东西,所以我可以向您保证在整个程序范围内这都是可能的):
    auto mass = kg(2.0);
    auto accel = 1.0; // units are strictly optional
    auto force = mass*accel;
    accel += metresPerSecondSquared(9.81); // units of 'force' and 'accel' are now known
    force += pounds(3.0); // unit mismatch detected
  • 最佳答案

    Run-time reflection: Are the compile-time reflection features sufficient to build a run-time reflection system a la Java/.NET?



    是的。您可以使用 __traits在编译时获取所需的所有信息,并生成运行时反射所需的运行时数据结构。

    Code conversion: Using a metaprogram, create C#/C++/etc. versions of your D program every time you compile it (bonus point if doc comments can be propagated).



    不,无论D多么强大,它都是根本不可能的。某些功能根本无法转移。例如,D有一个内联汇编器,不可能100%转换为C#。任何语言都可以无损地转换为所有其他语言。

    Traits. I don't mean the metaprogramming traits built into D, I mean object-oriented traits for class composition. A D program would indicate a set of traits to compose, and a metaprogram would compose them.



    您可以为此使用 template mixins,尽管它们不提供方法排除。

    Unit inference engine: Given some notation for optionally indicating units, e.g. unit(value), could a D metaprogram examine the following code, infer the correct units, and issue an error message on the last line? (I wrote such a thing for boo so I can assure you this is possible in general, program-wide):



    是的,这在D语言中很简单。已经有 at least one implementation

    关于metaprogramming - D对这些功能是否足够强大?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10962541/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com