views:

62

answers:

3

Could anyone give me a good use case for ActionScript dynamic classes?

Because it really looks like a bad pratice, for me, in every case.

+2  A: 

URLVariables, for example.

You could store the data in a dictionary / object / array too, but you don't gain much in this case, I think, and you cut down some boilerplate.

Juan Pablo Califano
In dynamic classes you can add fields/methods at runtime, what looks ugly to me. What would be different in this URL class if it was not dynamic? I can't see the advantage..
Tom Brito
What does or doesn't looks ugly to you, isn't something I can reasonably argue about. This class is meant basically as a dictionary of name/value pairs (plus a method for url encoding / enconding and I think, nothing else). As I said, you could expose some storage through the istance instead: `variables.data.add("key","value")` or something like that, instead of `variables.key = "value" or variables["key"] = "value"`. I see no real benefit in the former, though.
Juan Pablo Califano
PS: 'In dynamic classes you can add fields/methods at runtime'. I understand your point and I like to have objects clearly defined too, but sometimes, it's darn useful.
Juan Pablo Califano
I think the URLVariables is a bad example, because the class also has valid instance functions. You can't have `variables.decode = true` for example, at least not without potential confusion. A better choice for URLVariables (and similar classes) is to have a bare `Object` exposed, which is implicitly dynamic. This would make `variables.key = value` become `variables.data.key = value`, which isn't all that wordy and lets you stop worrying about name collisions.
Cory Petosky
@Cory Petosky. That's a valid point. Though I must say, in practice, I've never had problems with name clashes.
Juan Pablo Califano
@Juan: that's a valid point too; I'm not sure I have either. :)
Cory Petosky
+3  A: 

Anything that uses Proxy must by extension be dynamic. I use Proxy fairly regularly; for example, here's a replacement syntax for ExternalInterface using Proxy.

Cory Petosky
Here is another good use of dynamic class.
__dominic
cool.. comparing with what I know, its useful like the reflections concept are in Java..
Tom Brito
+1  A: 

Well you could use an Object object, but using a dynamic class ensures that it is typed. That's the way I see it, and it's the only reason I would use them. What Juan Pablo is saying is a good reason too.

__dominic