为什么要设计一种具有唯一匿名类型的语言? [英] Why design a language with unique anonymous types?

查看:56
本文介绍了为什么要设计一种具有唯一匿名类型的语言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这一直是我困扰C ++ lambda表达式的功能:C ++ lambda表达式的类型是唯一且匿名的,我根本无法写下来.即使我创建了两个在语法上完全相同的lambda,也将结果类型定义为不同的.结果是:a)lambda只可以传递到允许编译时传递的模板函数,不可言传的类型与对象一起传递,以及b)lambda仅在通过 std擦除类型后才有用::function<> .

This is something that has always been bugging me as a feature of C++ lambda expressions: The type of a C++ lambda expression is unique and anonymous, I simply cannot write it down. Even if I create two lambdas that are syntactically exactly the same, the resulting types are defined to be distinct. The consequence is, that a) lambdas can only be passed to template functions that allow the compile time, unspeakable type to be passed along with the object, and b) that lambdas are only useful once they are type erased via std::function<>.

好吧,但这只是C ++的工作方式,我已经准备好将其编写为该语言的令人讨厌的功能.但是,我只是了解到Rust似乎也做同样的事情:每个Rust函数或lambda都有一个唯一的匿名类型.现在我想知道:为什么?

Ok, but that's just the way C++ does it, I was ready to write it off as just an irksome feature of that language. However, I just learned that Rust seemingly does the same: Each Rust function or lambda has a unique, anonymous type. And now I'm wondering: Why?

所以,我的问题是这样的:
从语言设计者的角度来看,将唯一的匿名类型的概念引入语言有什么优势?

So, my question is this:
What is the advantage, from a language designer point of view, to introduce the concept of a unique, anonymous type into a language?

推荐答案

许多标准(尤其是C ++)采取了使它们对编译器的需求最小化的方法.坦白说,他们已经要求足够了!如果他们不必指定某些东西才能使其正常工作,则倾向于将其实现定义为已定义.

Many standards (especially C++) take the approach of minimizing how much they demand from compilers. Frankly, they demand enough already! If they don't have to specify something to make it work, they have a tendency to leave it implementation defined.

是lambda而不是匿名的,我们必须对其进行定义.这将不得不说很多有关如何捕获变量的事情.考虑lambda [=](){...} 的情况.该类型必须指定lambda实际捕获了哪些类型,这对于确定而言可能并非易事.另外,如果编译器成功地优化了变量,该怎么办?考虑:

Were lambdas to not be anonymous, we would have to define them. This would have to say a great deal about how variables are captured. Consider the case of a lambda [=](){...}. The type would have to specify which types actually got captured by the lambda, which could be non-trivial to determine. Also, what if the compiler successfully optimizes out a variable? Consider:

static const int i = 5;
auto f = [i]() { return i; }

一个优化的编译器可以轻松地识别出可以捕获的 i 的唯一可能值为5,并将其替换为 auto f = [](){return 5;} .但是,如果类型不是匿名的,则可能会更改类型,或者迫使编译器进行较少的优化,即使实际上并不需要它,也存储 i .这是一整袋的复杂性和细微差别,而lambda打算做什么根本不需要.

An optimizing compiler could easily recognize that the only possible value of i that could be captured is 5, and replace this with auto f = []() { return 5; }. However, if the type is not anonymous, this could change the type or force the compiler to optimize less, storing i even though it didn't actually need it. This is a whole bag of complexity and nuance that simply isn't needed for what lambdas were intended to do.

而且,在实际上您确实需要非匿名类型的情况下,您始终可以自己构造闭包类,并使用函子而不是lambda函数.因此,它们可以让lambda处理99%的情况,而让您以1%的价格编写自己的解决方案.

And, on the off-case that you actually do need a non-anonymous type, you can always construct the closure class yourself, and work with a functor rather than a lambda function. Thus, they can make lambdas handle the 99% case, and leave you to code your own solution in the 1%.

Deduplicator在评论中指出,我没有像匿名性那样强调唯一性.我不太清楚唯一性的好处,但是值得注意的是,如果类型是唯一的,则以下行为很清楚(动作将被实例化两次).

Deduplicator pointed out in comments that I did not address uniqueness as much as anonymity. I am less certain of the benefits of uniqueness, but it is worth noting that the behavior of the following is clear if the types are unique (action will be instantiated twice).

int counter()
{
    static int count = 0;
    return count++;
}

template <typename FuncT>
void action(const FuncT& func)
{
    static int ct = counter();
    func(ct);
}

...
for (int i = 0; i < 5; i++)
    action([](int j) { std::cout << j << std::endl; });

for (int i = 0; i < 5; i++)
    action([](int j) { std::cout << j << std::endl; });

如果类型不是唯一的,我们将必须指定在这种情况下应发生的行为.那可能很棘手.在这种情况下,出于匿名性的考虑,一些关于匿名性的问题也显得有些丑陋.

If the types were not unique, we would have to specify what behavior should happen in this case. That could be tricky. Some of the issues that were raised on the topic of anonymity also raise their ugly head in this case for uniqueness.

这篇关于为什么要设计一种具有唯一匿名类型的语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆