http://qs321.pair.com?node_id=473584


in reply to How will you use state declared variables in Perl6?

Well, they aren't really quite the same as C's statics. You should really think of them more as persistent lexicals that also happen to know how to initialize themselves on first use. Unlike globals (and like lexicals) they are by default per-thread and per-clone. (They are not per-instance, even if used in methods. In a sense, "has" variables are state variables on the instance.)

If used in a class, the state variable does not generate a public accessor unless there is a dot twigil to indicate that. Without the accessor no other class can see the variable, not even derived classes. Only your class's methods can see it, and can therefore adjudicate any use of it by derived classes. The state belongs to your class. Given that (in general) class closures are not repeatedly cloned, there's really little to choose from between "my" and "state" in the class definition itself.

A given method may certainly declare a state variable and adjudicate its use as well. The state variable functions as a class variable that no other method can see, so it could be useful in the rare case that your method doesn't happen to trust the other methods in the class. Other than that, yes, a class attribute probably makes more sense.