UnrealScript has always impressed me, somewhat, with it its intrinsic support for states (and latent functions) by grouping/overloading functions and fields into blocks like:
state() SomeState
{
...
function void Foo()
{
GotoState('SomeOtherState');
}
...
}
Which is quite a bit cleaner than using loads of switch-statements inside every function (it's almost some sort of design by contract).
Are there any other more general-purpose programming languages that intrinsically support state declarations similar to this (ignoring visual programming languages or tools like Workflow Foundation)?
Edit:
Some of the beauty of states in UnrealScript is that you can override stateful functions in subclasses, and even define new, named states. I think this is troublesome to do with enum-switches (where enums cannot be extended), delegates, or co-classes implementing different states, especially in languages like C# or Java that only support single-inheritance.
Any object oriented programming language enables you to create state-machines easily. But you might want to take a look at QT and it's http://labs.trolltech.com/blogs/2009/01/30/qt-state-machine-framework/. I haven't tried it though.
I prefere languages that enable me to create a variety of supporting structures of my choice to languages that offer me special functionality for all different kinds of special situations. C++ as shown in QT is a good example for that.
None that I know of, but language that support easy writing of domain-specific languages through metaprogramming (e.g., Ruby), can essentially pretend to. 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 the event
blocks, not just state transitions)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With