What programming languages ​​support state-level conditions?

UnrealScript has always impressed me, with its internal support for states (and hidden functions) by grouping / overloading functions and fields into blocks such as:

state() SomeState { ... function void Foo() { GotoState('SomeOtherState'); } ... } 

This is a little cleaner than using the load of switch statements inside each function (it is almost something like a contract design ).

Are there any other general-purpose programming languages ​​that essentially support similar state declarations (ignoring visual programming languages ​​or tools like the Workflow Foundation)?

Edit:

Some beauty of states in UnrealScript is that you can redefine state functions in subclasses and even define new, named states. I think this is difficult to do with toggle enums (where enumerations cannot be extended), delegates or co-classes that implement different states, especially in languages ​​like C # or Java that only support single inheritance.

+4
source share
10 answers

No, of which I know, but a language that supports the easy recording of domain languages ​​through metaprogramming (e.g. Ruby) can essentially pretend. From the acts_as_state_machine plugin for Rails:

 class Nonprofit < ActiveRecord::Base acts_as_state_machine :initial => :created, :column => 'status' # These are all of the states for the existing system. state :submitted state :processing state :nonprofit_reviewing state :accepted event :accept do transitions :from => :processing, :to => :accepted transitions :from => :nonprofit_reviewing, :to => :accepted end event :receive do transitions :from => :submitted, :to => :processing end # either a CTP or nonprofit user edits the entry, requiring a review event :send_for_review do transitions :from => :processing, :to => :nonprofit_reviewing transitions :from => :nonprofit_reviewing, :to => :processing transitions :from => :accepted, :to => :nonprofit_reviewing end end 

(you can also include any arbitrary code in event blocks, not just state transitions)

+3
source

Any object-oriented programming language makes it easy to create state machines. But you can take a look at QT and http://labs.trolltech.com/blogs/2009/01/30/qt-state-machine-framework/ . I have not tried, though.

I prefer languages ​​that allow me to create various support structures of my choice in languages ​​that offer me special functionality for all different situations. C ++, as shown in QT, is a good example for this.

+3
source

UnrealScript is not used, but could you possibly achieve the same thing in any language that supports first-class / lambda functions?

+2
source

there is a boduch library in python that allows you to declare states ... but this is not internal

+2
source

There is no programming language that I know of that provides the same functionality as “states” in UnrealScript. States in UnrealScript are not like ordinary state machines. They are more like layers that you can place on top of objects. Layers that intercept the call method and have access to the internal state of the object.

Starting with UnrealEngine3, you can also stack states, thus having more than one active layer. For instance:

 function bar() { // print quux } state S1 { function foo() { // print foo } } state S2 { function foo() { // print bar } function bar() { // print bar } } 

Now, when you go to state S2 and call foo () and bar (), you will see "bar bar" When you go to state S1 (from start state or S2) and call the same methods, you will see "foo quux". However, when you are in S2 and push S1 to the status stack, you will see "foo bar" when you call foo () bar () instead of "foo quux".

In any case, return to the original question. One way to get the same state functions as in UnrealScript is to use the AOP language, which provides a dynamic way to enable / disable aspects at runtime.

+2
source

The languages ​​that I know with internal support for state and state machines are usually divided into two classes: Lexers and Expert Systems.

Lexers tend to be text-oriented, but can often be adapted to other uses. Examples of Lexers (f) lex , Antlr , Quex . I heard stories about people using lex to control a robot.

Expert systems are designed to make decisions (presumably as an expert) based on a set of rules and the situation with which you represent them. Examples of expert systems that implement their own stateful processing languages, make and Clips . make is designed to create software, so it works best if your view of the world can be based on file dates. Clips are much more flexible, but not innately have a very good idea of ​​an external OS like make.

+1
source

Lua also supports state classes, provided you are ready to program on top of Lua tables.

Or you use my library: MindState . I designed it so that it mimics the UnrealScript class system and specifies as much as I can, from ordinary class inheritance to stackable states.

+1
source

I'm also interested in how to do this in C #, not least because I use UnrealEngine 3 as the basis for developing my own model of object objects. (By the way, UDK is a great way to prototype functions.) In addition to using an external approach such as egarcia (which can, in fact, be used in .NET through LuaInterface), I suggested two possibilities:

  • Explicit interface implementations with an interface for each state. Then you transfer the object to the appropriate interface (that is, "state") and call the method; CLR does the rest. You can even wrap this call with a generic method and pass an interface ("state") as a type parameter, but this can be overwhelming.

  • Create a dictionary with a key consisting of delegates as values. Offers great flexibility at the cost of a hack-ish view.

Personally, I prefer method 1 in terms of coding, because I'm all about strong types, but method two is easier to implement and update.

On the side of the note, if you are interested in the egarcia Lua approach and think that LuaInterface is too slow for you, I modified LuaInterface to support LuaJIT. It uses its own import, but hopefully LuaJIT's performance will compensate for the send call. Put me a line if you want a source.

+1
source

My naive understanding is that a “type state” provides this functionality for object-oriented languages:

What is typical?

and can be easily implemented in any language that supports features:

Mixins vs. Traits

I am still learning about all these issues, so I would like to hear a more knowledgeable answer.

+1
source

Source: https://habr.com/ru/post/1285744/


All Articles