-1

I would like to be able to access a constant value from a name generated from a variable, e.g. if the variable contains '123' then get the contents of a constant named 'C123'.

After searching on here I can see that PHP is capable of doing that, but as I'm currently playing with Java, OCaml and Fortran I wondered if there was an equivalent for any of these languages?

shakeshuck
  • 95
  • 1
  • 5
  • Perhaps this would be easier to answer if you provided a use case. It would be helpful to know what you need to do this for. – Jivings Mar 14 '12 at 14:10
  • I was looking for a speedy, dirty solution to a problem, not an elegant, correct way. A lot of respondees would probably have given me a 'you don't want to do it like that...' type of answer I was hoping to avoid. Thanks anyway. – shakeshuck Mar 14 '12 at 15:36

2 Answers2

8

This is generally referred to as reflection. (Here's in the Wikipedia article on it.)

Basically it refers to the process of "letting the program inspect the program".

If you search for reflection <your programming language of choice> you should be able to find plenty of guidance.

Btw, it is most often a sign of bad programming style to use reflection.

aioobe
  • 413,195
  • 112
  • 811
  • 826
  • +1: I don't think reflections is evil if you are developing a framework, but in this case, I agree with you, its a bad idea even if you can do it. – Peter Lawrey Mar 14 '12 at 14:16
  • Thanks for this. I was having difficulty finding keywords to search on, now I have a clue! Considering it's bad practise, it's interesting that Wikipedia lists some 30+ languages that allow you to do it. Somebody thinks it's a good idea. :-) – shakeshuck Mar 14 '12 at 15:30
  • I agree that reflection is a sign of bad programming style. In languages that do not support "variant types" (Java, C++, C#, etc.) you unfortunately cannot do without. E.g. in Java (..) if you want to have a list of objects of type "A" or "B" (only) then you must create a list of the common supertype "C" of "A" and "B" (worst case: Object) and cannot avoid someone stuffing other subtypes of "C" in the list. In this case the type system of you language is too weak and you would need to check at run-time -- with reflection... – lambdapower Mar 15 '12 at 11:16
  • While I agree that there are more powerful type systems than Javas, I disagree on the point that "*you can't do without reflection*". For the particular scenario you describe, see for instance my answer over [here](http://stackoverflow.com/questions/9653282/how-would-you-create-an-array-list-with-mixed-booleans-and-chars/). – aioobe Mar 15 '12 at 12:20
1

There is no equivalent in OCaml as far as I know, and I'm glad there isn't, because that would only complicate compilation and possibly decrease performances for a feature whose use is a code smell anyway.

gasche
  • 31,259
  • 3
  • 78
  • 100
  • If the question is: "Is there reflection in OCaml" then you are right: There is no such thing. OCaml throws away all types after compilation -- they are not needed anymore. – lambdapower Mar 15 '12 at 11:09
  • @lambdapower: the OP asked if it is possible to dynamically access to constants by their names as a string. There was no mention of "reflection", and this term is so vaguely defined -- and difficult to define -- that I'm not sure it's fruitful to use it at all. I appreciate the effort of the OP to formulate a precise question. – gasche Mar 15 '12 at 13:41
  • @gasche: You're right. After looking up "reflection" I now realise that it encapsulates a lot more than the simple situation I was asking about. I was looking for quick access to a constant; applying reflection would probably make the code too slow to be of any use to me. – shakeshuck Mar 24 '12 at 14:44