Talk:Reflection (computer science)
From Wikipedia, the free encyclopedia
[edit] MOO code
I'm going to give a sentence to the MOO code just like all the other programming languages.
[edit] Self-modifying code
How does reflection relate to self-modifying code? The terms are used in quite different contexts, of course, but aren't they really forms of the same thing? Self-modifying code is usually done in assembly language, and is normally considered bad form; reflection is usually considered a good thing, especially if the language is set up for it. I might at least add a "see also" to the other one on both pages. Benhoyt 20:56, 22 December 2005 (UTC)
- I don't think that reflection is self-modifing code, they are two different things. With reflection, code does not change at runtime at all, it's only introspection, code knows about itself, but does not change at all. You could do reflection even in C (but it's not portalbe), as you can call functions via pointers, and you could search for the required pointer to be called by browsing through the debug symbol database for example. But the article DOES make confusion betweek reflection, that is, knowing itself, with self-modification, that is an entirely different concept! Most languages that support reflection do not support self-modification. IMHO the article is all wrong in this respect! AND there are some self-modifing languages where you can't query for the type of an object and thus, they don't have reflection. - AngeloPesce, Jan 2008
- Also every example there that uses code interpretation (that still is NOT QUITE full self-modifing code, because you're just invoking the interpreter to parse more text, but you are not allowed to modify existing code, or at least, not easily, that's something that is only doable in ASM or in languages that have code-as-data equivalence, like LISP) are _plain wrong_. There you don't have reflection, you are not allowed to know the type given a string, to query the interpreter database... you're only hacking the function call by interpreting it in runtime, instead of having it hardcoded. It's not, conceptually, the same thing. For example, if I have an interpreted language that makes me interpret strings in runtime, I surely can call methods given the string of the class and of the method, but still is not quite the same thing as getting the type object via a string. IMHO those examples are only confusing, and are one of the typical Wikipedia problems, users add everything just to add things to the article, even if the examples are not useful to clarify the concept, and rather they make it harder to be understood. This is really bad. - AngeloPesce, Jan 2008, please consider deleting those examples
[edit] Is reflection slow?
I removed "Depending on the implementation, code with reflection tends to run slower than that without it." Sure, it does depend on the implementation, but I thought this was unhelpful, because it only applies to a particular kind of reflection. With many types of reflection, such as macros and code compiling code, the whole point is to make the result run faster.
For example, in Forth you could add up the numbers from 0 to 9 with a loop, or with an unrolled, reflective-style loop:
: count ( -- ) 0 10 0 do i + loop . ; count
: unrolled-count ( -- xt ) :noname 0 postpone literal 10 0 do i postpone literal postpone + loop postpone . postpone ; ; \ effectively ":noname 0 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + . ;" unrolled-count execute
For a better, more "real-life" example, see Bruce Hoyt's Fast Pentomino Solver written in Forth using generated macros. Benhoyt 20:26, 22 December 2005 (UTC)
[edit] Emmited vs produced
Is the meaing of the word "emmited" here similar to the word "produced?" It might be easier for a non-programmer to understand the intended meaning if the word "produced" were used, since one reading this is probably already immersed in unfamiliar terminology that may have meanings beyond typical language.
Uh, aren't C# and Visual Basic.NET reflective languages? If not why not? They have runtime metadata. KellyCoinGuy 07:29, August 20, 2005 (UTC)
[edit] Reflection in C
Humm, in fact the GNU C language does have some reflection capabilities, through the dl library. Here is the code example for that:
#define _GNU_SOURCE 1 #include <dlfcn.h> int main(void) { typedef int (*printf_t) (const char *format, ...); printf_t the_printf; the_printf = dlsym (RTLD_DEFAULT, "printf"); the_printf ("Hello %s!\n", "world"); return 0; }
Behdad 03:47, 23 September 2005 (UTC)
- That's not reflection, that's dynamic linking, a form of late binding. It does allow you to do things you couldn't do statically, but it doesn't give you knowledge about the program's own structure like reflection does. You can't find out how many arguments a function takes or what procedures a library exports, for example, or dynamically create objects of a given type name, find out what members an aggregate type has, etc. If we can call this reflection at all, it's a very weak sort of reflection, and it's certainly not part of the language itself (support for dynamic linking comes from the environment; the language guarantees nothing). 82.92.119.11 15:09, 25 November 2005 (UTC)
-
- Like Behdad, I fail to see the distinction beyond that dlsym() is only a slightly weak implementation of the idea: add more meta-information to the shared library, and a small amount of infrastructure, and reflection in the "strong" sense will appear. That this is all possible at such a low level suggests that, indeed, this is a property of the environment, not of any particular language. That there are special hooks and syntax in language X to help it along is no more or no less than the aforementioned "infrastructure".
-
- Also note my use of "slightly". The fact you can dig out information about code and data structures doesn't help you much in the end, unless you know what you are looking for and how to use what you finally find. Behdad's printf() example is basically identical to all the examples in the article. They search for something in some meta-database and, upon success, make immediate use of it. mdf 13:10, 15 August 2007 (UTC)
-
-
- "Add more meta-information to the shared library, and a small amount of infrastructure, and reflection in the "strong" sense will appear." Sure, and add garbage collection, references, exceptions and runtime checks to the library (all of which can certainly be implemented with enough low-level grovelling) and you'll have C#. More or less. But that's not the point.
- The point is that the C programming language has no support for reflection. Of course the environment is very likely to provide some form of metadata discovery if you go low-level -- it would be hard not to. (For one thing, almost any environment will allow a program to inspect its own executable, even if it has to do all the gruntwork itself.) But calling this "reflection" is missing the point entirely -- namely, that such features are only relevant to the discussion if they're part of the language proper. dlsym() cannot be called part of the language proper, not even if we constrain ourselves to the particular dialect of C as implemented by the GNU Compiler Collection.
- If you want to call this "a weak form of reflection provided by the environment", then sure, go ahead, but calling this reflection does not change the fact that C is not a reflective language, so Behdad's statement is at best misleading, and your dismissal of native reflection as "hooks and syntax" reminds me of the old adage that "you can write FORTRAN in any language". The same is doubtlessly true for getting reflection, as long as you're willing to put in all the extra work yourself. But you're getting dangerously close to just reasserting the basics of the von Neumann architecture. 82.95.254.249 (talk) 01:41, 27 November 2007 (UTC)
-
-
-
-
- There is no such thing as GNU C language, there is a GNU C compiler only. And I think that it's not useful anyways to add information about a non-standard non-portable way of doing reflection in C. Of course it can be done. In a non portable way it can be done with each compiler that has debug symbols. It's even easy, there's nothing magic in GNU C. But how does this add to the concept and understanding of the reflection concept? If you explain the concept clearly, then of course a C programmer know that he can implement it in C. It's something that should not enter in the main article IMHO. -AngeloPesce —Preceding unsigned comment added by 24.80.96.229 (talk) 07:04, 9 January 2008 (UTC)
-
-
-
-
-
-
- This article has examples from many languages on how programs can peer into their own guts. Given it is possible to do this with C, C++ or anything else with access to dlsym(), or other mechanisms, then such examples can only serve to highlight the technique even more. To simply state that "C" or "C++" has no reflective abilities, or to re-label them as "late binding" (as if that isn't what reflection will boil down to in the end!) is deeply misleading. C#, Objective-C, Java, and other languages are no more special, beyond making the resulting code slightly less messy. mdf (talk) 20:00, 20 February 2008 (UTC)
-
-
-
-
-
-
- I don't dismiss reflection as "hooks and syntax": I assert that is what reflection ultimately is, assuming the relevant information is accessible. There is nothing inherently special about making glorified calls to dlsym() vs. directly calling dlsym() -- beyond the obvious lack of detail inherent in dlsym() implementations. "#include <dlfcn.h>" or "#include <libelf/libelf.h>" vs. something like "using System.Reflection" is neither here nor there, and I say from experience that both make for fairly horrid snippets of code. I suspect that when you speak of "language proper", you are confusing syntax and semantics of a language with a abilities of a run-time library. Or the "environment", as I said back in August. That, as you observe, any von Neumann machine is capable of this behavior should be a very strong hint that the language has little, if anything, to do with reflection. mdf (talk) 20:00, 20 February 2008 (UTC)
-
-
[edit] Paradigm
This article should include a section on reflective programming as a paradigm. And the list of languages should also be presented in a multi-column tabular form, for better readability --Soumyasch 10:00, 26 March 2006 (UTC)
- I will be trying to do so. --Soumyasch 10:03, 26 March 2006 (UTC)
- Done. Please review and comment. And I suggest that the list of languages be moved to a separate article, for reasons stated below. --Soumyasch 04:27, 27 March 2006 (UTC)
- The first paragraph of this makes very little sense. There is no need (and in my experience it's quite rare) for imperative or object oriented programs to be "pre-determined" in the way you describe. There is no mention anywhere else in the article of how reflection provides self-optimization. In all, I think this section doesn't actually describe reflection in the same sense as the rest of the article itself and should probably be moved. The keeping of meta-information for all compound statements in a program, including what the statement actually does sounds... intractable too. 84.19.238.82 09:39, 31 January 2007 (UTC)
- Done. Please review and comment. And I suggest that the list of languages be moved to a separate article, for reasons stated below. --Soumyasch 04:27, 27 March 2006 (UTC)
[edit] Title is incorrect
Per Wikipedia's manual of style for capitalization, this should be at computational reflection if anything. Making this correction would require fixing the double redirects. Fredrik Johansson 12:11, 26 March 2006 (UTC)
- Google hits show Computational Reflection with both capital C and capital R --Soumyasch 12:23, 26 March 2006 (UTC)
- Doesn't matter; it is Wikipedia's house style to capitalize only the first word in a title (with exception for proper nouns). See Wikipedia:Naming conventions (capitalization). Fredrik Johansson 12:47, 26 March 2006 (UTC)
JA: The move from Reflection (computer science) to Computational reflection was ill-advised (if advised at all). Analogous considerations have come up many times before, for example, with all discipline-specific usages in mathematics, for instance, Group (mathematics) versus Mathematical group. If you think a little bit ahead (Partial lookahead (computer science)), you will perhaps see what kind of mess you are getting into with this strategy for disambiguation. Jon Awbrey 13:46, 26 March 2006 (UTC)
- I did not do it as a means of disambiguation but because "Computational Reflection" is a more formal name to the technique. Calling it "reflection" is a colloquialism and the word "computational" is dropped only when referring to something such as "reflective programming" or "reflective system", but if the technique is mentioned, it is always formally introduced as "Computational Reflection", even though it might be referred to as "reflection" only.
You can also refer to these [1], [2], [3], [4], [5], and all references on these pages. --Soumyasch 15:26, 26 March 2006 (UTC)
JA: I am familiar with the literature. The intent of the adjective is to disambigaute the use of the term, but context already does that. However, putting the adjective up front introduces additional problems. For instance, if you consider the paradigm of parallel usage, like "computational process", the term "computational reflection" connotes an algorithm that carries out reflection. This is a useful concept, one that may even be discussed under this head, but it is not in general the concept that is being discussed here. Again, this sort of thing has come up very often before, and experience shows that putting the disciplinary category in parenthesis works best, especially as one begins to use terms in combination, supply quick wiki links, and so on. Jon Awbrey 15:42, 26 March 2006 (UTC)
- Well, now what? Should this title be kept or get it moved back? I have no problem with any name, its the content that matters to me. As posted earlier, I am going to expand the definition of reflection a bit, tabularize the list of languages and add a section for reflection as a programming paradigm (I know it will be bit theoretical, but I will try to make it as layman-ly as I can). I would appreciate comments in this respect as well. --Soumyasch 17:08, 26 March 2006 (UTC)
JA: As far as I can tell, what little I know, the move back to Reflection (computer science) is now blocked by previous history, so requires Admin assistance. I can go through the steps for that later tonight. I think that this falls under "moves that require discussion and consensus", so that's what I think happens next. Maybe somebody else knows an easier way though? Jon Awbrey 17:16, 26 March 2006 (UTC)
[edit] List of languages
I would suggest the list of reflective programming languages be forked off into a new article. The list is preventing the article from having a coherent structue and because, lists generally have their own page. Please support or oppose. --Soumyasch 03:36, 27 March 2006 (UTC)
- No one objected. Doing it. --Soumyasch 04:25, 28 March 2006 (UTC)
- The following discussion is an archived debate of the proposal. Please do not modify it. Subsequent comments should be made in a new section on the talk page. No further edits should be made to this section.
The result of the debate was move. —Nightstallion (?) Seen this already? 07:49, 1 April 2006 (UTC)
[edit] Requested move
- Computational reflection → Reflection (computer science) … Return to previous name now blocked by history, more standard practice to disambiguate a term with the discipline name in parentheses. — Jon Awbrey 04:52, 27 March 2006 (UTC)
- Add *Support or *Oppose followed by an optional one-sentence explanation, then sign your opinion with ~~~~
- Support --Soumyasch 05:03, 27 March 2006 (UTC)
- The above discussion is preserved as an archive of the debate. Please do not modify it. Subsequent comments should be made in a new section on this talk page. No further edits should be made to this section.
[edit] Example applications?
I'd be grateful if someone could put some example applications of Reflections - I mean an example where it's for example useful to use them, in more real-world situations.
I have an idea of an example, but I don't know if I understood the Reflections ideas properly. I can be completely wrong and the example stupid. Therefore, I write it here for someone to 'certify', or change, or maybe write his own one, taking from his experience - and put on the main page:
A computational program (physics problem for example) could use a library of procedures for solving a simple equation. Now, each procedure would have attributes, like:
- it's speed,
- it's accuracy.
Now, we write the program, which can solve problems of various sizes (from Jimmy's homework to Earth simulation). When it is started, it checks the size and type of the problem; now, knowing something more about it's nature, the program can browse through the library, aiming for:
- solving Jimmy's homework:
- average speed (1 equation, but needs it for tomorrow),
- good accuracy (Jimmy wants a good mark)
- solving Prof. Mastermind's Earth simulation:
- superb speed (zillions of equations, which must be solved before Proffessor's life ends),
- low accuracy (can ommit some butterflies... - speed's more important)
- solving Cancer-super-cure simulation:
- accuracy is crucial (we don't want people to get worse),
- speed is unimportant (we're a huge corporation with loads of money we can spend on new supercomputers).
Reflection allows for such a 'search'. Furthermore, it allows for simple ways of adding new procedures to the library, given they're attributed with some speed & accuracy ratings - and they'll work with the program.
Am I right? or not? —Preceding unsigned comment added by Akavel (talk • contribs)
- Yep, that would be possible. A program would load a Homework assembly, and search for methods which satisfy the requirements and use the one that meets the needs..
[edit] Clarification
As someone unfamiliar with reflection, this article does a poor job of explaining what it is. I have a decent idea from the example section, but the rest of the article reads like an editorial on reflection, not a description. I think for starters the summary could be improved. It doesn't need to be so long, and it only needs to summarize what reflection is in a short and concise way. It also contains some confusing grammar. For example, "More generally, reflection is an activity in computation that allows an object to have information about the structure and reason about its own computation." Information about the reason about its own computation? I have no idea what that is supposed to mean. It would be great if someone with the knowledge could make this article more clear to people not familiar with the topic. --JRavn 15:07, 14 August 2006 (UTC)
[edit] Weak examples
All the code examples of reflection just regard what's dynamic dispatch, but reflection is much more. Better example needed. Said: Rursus ☺ ★ 17:10, 2 June 2007 (UTC)
WP:SOFIXIT :D --soum (0_o) 12:27, 3 June 2007 (UTC)
[edit] C# Example doesn't fit
The C# example doesn't fall in line with the other examples, so I feel that it's less helpful as a comparison. Perhaps either the C# example should be simpler (ie the Java one), or all the examples should implement some common functionality. That would give more insight into how each language handles reflection.
- I'm thinking the following snippet (probably the simplest way to reflect in C#) might be more understandable to the lay-programmer. It minimizes the need to recognize the .NET System.Type and System.Reflection.BindingFlags classes, and it parallels the (create object -> call method on object) style that the other language examples use. Talyian (talk) 23:18, 8 April 2008 (UTC)
//With reflection
object foo = Activator.CreateInstance(null, "Foo");
foo.GetType().GetMethod("Hello").Invoke(foo, null);
[edit] Interpreted languages
The article stated: Interpreted programming languages, such as Ruby and PHP, are ideally suited to reflection, since their source code is never lost in the process of translation to machine language—the interpreter has the source readily available.
Ideally suited to reflection... what a strong statement, and sorry, that is definitely not the state of the art. Other implicit assumptions, such as source code being available as a string as opposed to an abstract syntax tree, have been negated more than 40 years ago, too. -- Zz 09:56, 17 September 2007 (UTC)
- Also there is no thing as "interpreted programming languages", a PL is a specification of a language, there you can implement it into an interpreter, a compiler or a JIT. As long as you can respect the specification, it's fine. Ruby has its compilers as well. And having the source code availabe is no useful at all, it would be the same to say that if you ship C programs with their sourcecode then it's easy to do reflection in C... But it's true that reflection is trivial to implement into an interpreter, but not because an interpreter has the source loaded everytime (it's not true, it gets tokenized and discarded) but because it usually has all the symbols loaded (but it could discard them as well and if so, then reflection would be impossible) —Preceding unsigned comment added by 24.80.96.229 (talk) 07:09, 9 January 2008 (UTC)
[edit] high level language
The classification of languages as 'high level' or 'low level' is almost never useful. This article is another example where it adds nothing except confusion. 150.101.166.15 08:47, 18 October 2007 (UTC)
- Yes it's really confusing, and it's wrong too. "With high-level languages, when program source code is compiled, information about the structure of the program is normally lost when low-level code (typically machine language code) is produced, unless, of course, the code is compiled into an Intermediate Language (IL)...". Who told that? Normally, every compiler retains the information about the types of the target code for debugging purposes, so actually, every compiler has a way to save that info, it's not a novelty of IL-languages and there is no reason for IL-languages to retain more info than non-IL ones. Of course, Java and C# do retain that info in the IL, but because they are reflective languages, it's a conseguence of language design, not IL use. I've deleted that statement - AngeloPesce —Preceding unsigned comment added by 24.80.96.229 (talk) 07:13, 9 January 2008 (UTC)
-
- Come now, there is absolutely nothing in (say) Java that makes reflection a "consequence of language design". Indeed, that you need to import the appropriate library before this computational navel-gazing can occur is good evidence the language is not inherently reflective. As I said above, the substrate itself is why this stuff works: the code-data duality is built right into the machinery. This is mentioned in the first paragraph of this article, but could use some highlighting, in my opinion. mdf (talk) 20:13, 20 February 2008 (UTC)
[edit] Smalltalk
Why invoke the compiler when you can do something like (Smalltalk at: className asSymbol) R4p70r (talk) 21:42, 2 June 2008 (UTC)