Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

i heard that these(say for example Groovy) languages have the capability of changing the variable name or call methods dynamically in runtime! What you meant by dynamic languages? And what is the real need for changing any values during runtime? Is that doen't lead to confusion, because at runtime if you change any value(or your programming constriants change anything), then whats the need for compilation(because it decides and confirms these values will be used, then there is no meaning of changing it in dynamically)? And i know there should be something useful, so only people have introduced these concepts!

I guess i'm clear about my question! And i need some brief explanation :)

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

Dynamic languages give you flexibility and expressiveness in your code. But can also make things difficult to follow sometimes, and definately opens up the potentials of abuse as @TheLQ points out but it has its benefits as well.

As the question was around groovy, I want to point out that there is a guide specifically on Dynamic Groovy.

First of all, Using metaclasses I can augment my classes at runtime to change behaviours of individual instances or of all instances that will ever be declared, even static methods. These metaclasses can be used for convience (think extending final classes with methods that you've always wanted... Groovy in fact has a number of convient augmentations all of their classes), and is also handy for mocking parts of classes for various tests or hacks you need.

Additionally you can implement methodMissing and propertyMissing to use the signature of the method invoked to dynamically drive content. For example, GORM has Dynamic Finders takes advantage of this to make querying more concise. Instead of having to construct a HSQL query, or other hoops I can do object.findAllNameAndVersion("name",123) As a side note Rails also has this ability.

Hope this helps!

share|improve this answer
    
+1 for explaining:) and it helps me :) thank you :) –  Ant's Mar 5 '11 at 17:14
1  
No problem @Anto :) As I thought about it more, some additional benefits that the dynamic language provides that you may be interested in. Starting with some of the above, mix in Groovy's implementation of Closures, and you have the basis for some slick Builder patterns (for example JsonBuilder and MarkupBuilder), and the ability to quickly make other DSLs –  Charlie Mar 5 '11 at 20:50
add comment

The only thing I can think of that comes close to what your talking about is PHP's (horrible) variable variables.

$var = "foo";
$$var = "bar";
echo $var; //echo's foo
echo $foo; //echo's bar
echo ${$var}; //echo's bar

The "dynamicness" of this is if $var was assigned a value, for example, from input, the variable for "bar" would be completely different.


As a side note, don't ever do this

share|improve this answer
    
But i need to know why its useful? –  Ant's Mar 5 '11 at 16:28
    
@Anto This has some niche usages, but I've never found any. The only place I can point you is the php docs for this: php.net/manual/en/language.variables.variable.php –  TheLQ Mar 5 '11 at 16:31
    
anyways thanks for your reply :) –  Ant's Mar 5 '11 at 16:31
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.