实现使用&QUOT状态机收率"关键词 [英] implementing a state machine using the "yield" keyword

查看:89
本文介绍了实现使用&QUOT状态机收率"关键词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可行使用yield关键字来实现一个简单的状态机如下所示。对我来说,它看起来像C#编译器为你做的辛勤工作,因为它内部实现一个状态机来使yield语句的工作。

Is it feasible to use the yield keyword to implement a simple state machine as shown here. To me it looks like the C# compiler has done the hard work for you as it internally implements a state machine to make the yield statement work.

你能否编译器已经做了工作的首位背驮式并把它落实大部分的国家机器的吗?

Can you piggy-back on top of the work the compiler is already doing and get it to implement most of the state machine for you?

有没有人这样做,是不是技术上是可行的?

Has anyone done this, is it technically possible?

推荐答案

这是可行的,但它是一个坏主意。创建迭代器块来帮助你编写的集合,不是解决执行状态机的通用问题定制迭代器。

It's feasible but it is a bad idea. Iterator blocks were created to help you write custom iterators for collections, not for solving the general-purpose problem of implementing state machines.

如果你想要写一个状态机,只写一个状态机。它并不难。如果你想要写了很多状态机,写有用的辅助方法,让你重新干净present状态机,然后用你的库库。但不要滥用语言结构,旨在完全不同的东西,只是碰巧使用状态机作为实现细节。这使得你的状态机code难以阅读,理解,调试,维护和扩展。

If you want to write a state machine, just write a state machine. It's not hard. If you want to write a lot of state machines, write a library of useful helper methods that let you cleanly represent state machines, and then use your library. But don't abuse a language construct intended for something completely different that just happens to use state machines as an implementation detail. That makes your state machine code hard to read, understand, debug, maintain and extend.

(顺便一提,我做了双重考虑读你的名字的时候,其中一个设计师的C#也被命名沃伦·马特!)

(And incidentally, I did a double-take when reading your name. One of the designers of C# is also named Matt Warren!)

这篇关于实现使用&QUOT状态机收率"关键词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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