Talk:Callback (computer programming): Difference between revisions

Content deleted Content added
Ptyxs (talk | contribs)
 
(29 intermediate revisions by 15 users not shown)
Line 1:
{{WikiProject banner shell|class=Start|
This topic needs more general discussion of callbacks and their use, with simpler examples that explain the concept of a callback. I am rewriting most of this article to include much more information.
{{WikiProject Computing|importance=}}
}}
==[Untitled]==
This topic needs more general discussion of callbacks and their use, with simpler examples that explain the concept of a callback. I am rewriting most of this article to include much more information. -- ''Who said this and when? [[User:Mattsenate|Mattsenate]] ([[User talk:Mattsenate|talk]]) 01:07, 13 December 2012 (UTC)''
 
This article suggests that you can pass the name of a function to be used as a callback to another function for the languages JavaScript, Lua, Python, Perl and PHP. This is not true for at least Python and JavaScript; you must use the function objects themselves. There is a way to use the name of the function to denote the function itself in Python, but that is out of scope for this article. I will do some research for the other three languages before changing this. In the meantime, someone more knowledgeable, please change this!
 
A nice Python or Ruby callback example would be super, too. If no one gets to that before me, I will add that.
[[User:Deathweasel|Deathweasel]] ([[User talk:Deathweasel|talk]]) 22:01, 1 April 2013 (UTC)
 
== Suggestion ==
I think we should give simple example about this topic.coz this article is so COMPLICATED..THANKZ <span style="font-size: smaller;" class="autosigned">—Preceding [[Wikipedia:Signatures|unsigned]] comment added by [[Special:Contributions/202.60.56.249|202.60.56.249]] ([[User talk:202.60.56.249|talk]]) 00:24, 17 February 2009 (UTC)</span><!-- Template:UnsignedIP --> <!--Autosigned by SineBot-->
The definition at the top of the page is ambigious for me. Please could someone write something clearer. <!-- Template:Unsigned IP --><small class="autosigned">—&nbsp;Preceding [[Wikipedia:Signatures|unsigned]] comment added by [[Special:Contributions/79.69.3.69|79.69.3.69]] ([[User talk:79.69.3.69#top|talk]]) 12:03, 29 July 2022 (UTC)</small> <!--Autosigned by SineBot-->
 
 
Line 94 ⟶ 104:
 
:Too many platform specific references, ie Linux, Unix. Needs to be abstracted away from punks/hackers prosaic tendencies and their favorite languages and towards compsci academics. <span style="font-size: smaller;" class="autosigned">—Preceding [[Wikipedia:Signatures|unsigned]] comment added by [[Special:Contributions/71.15.154.180|71.15.154.180]] ([[User talk:71.15.154.180|talk]]) 04:37, 28 July 2010 (UTC)</span><!-- Template:UnsignedIP --> <!--Autosigned by SineBot-->
:LOL [[User:Stevebroshar|Stevebroshar]] ([[User talk:Stevebroshar|talk]]) 13:58, 11 April 2024 (UTC)
 
== <nowiki></nowiki> ==
Line 114 ⟶ 125:
== Example doesn't compile. ==
 
So you know. --[[User:Fs|<b><FONTspan COLORstyle="color:#336666;">f</FONTspan><FONTspan COLORstyle="color:#339999;">s</FONTspan></b>]] 07:16, 19 December 2010 (UTC)
 
: Compiles just fine now (for me, at least). I take it this talk thread could be deleted now. --[[User:Rs2|rs2]] ([[User talk:Rs2|talk]]) 21:47, 13 July 2011 (UTC)
Line 129 ⟶ 140:
:I think anyone interested in callbacks will know what rand() does. --[[User:Byteality|Byteality]] ([[User talk:Byteality|talk]]) 16:33, 6 January 2012 (UTC)
::I completely agree with Zbbentley. The less parasitic problems there is to read a given article, the better. [[User:Ptyxs|Ptyxs]] ([[User talk:Ptyxs|talk]]) 17:10, 3 August 2012 (UTC)
:Yeah. The C example is way too complicated. I'm going to simplify it. ... The original comment is from 2011 and no one has simplified it in the past 13 years! [[User:Stevebroshar|Stevebroshar]] ([[User talk:Stevebroshar|talk]]) 13:56, 11 April 2024 (UTC)
 
== function pointers page ==
 
Needs a link to [[function pointers]] <span style="font-size: smaller;" class="autosigned">— Preceding [[Wikipedia:Signatures|unsigned]] comment added by [[Special:Contributions/203.41.222.1|203.41.222.1]] ([[User talk:203.41.222.1|talk]]) 09:26, 30 May 2012 (UTC)</span><!-- Template:Unsigned IP --> <!--Autosigned by SineBot-->
 
:Sounds good. Add it. [[User:Stevebroshar|Stevebroshar]] ([[User talk:Stevebroshar|talk]]) 13:53, 11 April 2024 (UTC)
 
== C++11 ==
A brief discussion of the new std::function<> class, enabled by the recent C++11 addenda to C++ should be in order, don't you think so ?
See for instance : [http://en.cppreference.com/w/cpp/utility/functional/function]
 
[[User:Ptyxs|Ptyxs]] ([[User talk:Ptyxs|talk]]) 17:07, 3 August 2012 (UTC)
 
:To note: std::function objects make it possible to call : 1) a non member function 2) a static member function 3) a functional object 4) a lambda function 5) a non static member function (via std::bind). That is they can refer to any callable entity with compatible signature.[[User:Ptyxs|Ptyxs]] ([[User talk:Ptyxs|talk]]) 08:07, 4 August 2012 (UTC)
:To note: std::function objects make it possible to call : 1) a non member function 2) a static member function 3) a functional object 4) a lambda function 5) a non static member function (via std::bind). That is they can refer to any callable entity with compatible signature and are particularly useful to callback.[[User:Ptyxs|Ptyxs]] ([[User talk:Ptyxs|talk]]) 08:07, 4 August 2012 (UTC)
:I don't think the page should (or feasibly could) cover every syntax that supports callback. But, if you must, add an example for C++. [[User:Stevebroshar|Stevebroshar]] ([[User talk:Stevebroshar|talk]]) 13:53, 11 April 2024 (UTC)
 
== Should mention / link to the [[futures and promises]] article ==
 
This article goes into deferred callbacks, but it should go further and mention the article on that very concept, '''[[futures and promises]]'''. &mdash; [[User:Hippietrail|Hippietrail]] ([[User talk:Hippietrail|talk]]) 16:32, 8 August 2012 (UTC)
 
:Well, it's not going to add itself :) IOW A good way to get something done is to do it yourself. [[User:Stevebroshar|Stevebroshar]] ([[User talk:Stevebroshar|talk]]) 13:50, 11 April 2024 (UTC)
 
== Added JavaScript Example, Re-ordered==
 
Hey all, not an expert, but learning a lot and wanted to improve this article as I did so. I provided a basic JavaScript example that dovetails into the more complicated (but awesome!) C examples below it. I also re-ordered so that the explanation of differences between synchronous and asynchronous callbacks is made explicit early on. Next comes the implementation list, and finally some concrete examples under "Use". Happy to see improvements on this, leave a note if there are other low-barrier-to-entry examples we should tack on? -- [[User:Mattsenate|Mattsenate]] ([[User talk:Mattsenate|talk]]) 01:07, 13 December 2012 (UTC)
 
:I just modified/simplified the JS example. It was good, but still more complicated than I think it could/should be. ... FWIW, I find the C example (which may or may not be what it was in 2012) to be poor (far from awesome). I'm planning to work on it, but don't know where to start. [[User:Stevebroshar|Stevebroshar]] ([[User talk:Stevebroshar|talk]]) 13:49, 11 April 2024 (UTC)
 
== Do not over-complicate examples ==
 
Please avoid using irrelevant concepts in the examples. For example, the python example on this page was,
 
<syntaxhighlight lang="pycon">
>>> def my_square(val):
... """ the callback """
... return val ** 2
...
>>> def caller(val, func):
... return func(val)
...
>>> for i in range(5):
... j = caller(i, my_square)
... print("{0} ** 2 = {1}".format(i, j))
...
0 ** 2 = 0
1 ** 2 = 1
2 ** 2 = 4
3 ** 2 = 9
4 ** 2 = 16
</syntaxhighlight>
 
Why should we assume that the reader knows
* for loops
* the method range
* string formatting (especially in a style quite unique to python)
 
I have since changed the above to:
<syntaxhighlight lang="pycon">
>>> def get_square(val):
... """ the callback """
... return val ** 2
...
>>> def caller(func, val):
... return func(val)
...
>>> caller(get_square, 5)
25
</syntaxhighlight>
 
'''Caveat''': The example is complex because it demonstrates how callbacks can be implemented to achieve some complex behaviour; however, the program written to demonstrate this still should not include ''unnecessary'' concepts and be as simplistic as possible.
 
The Lua example seems to suffer from this. It seems like it was copy-pasted from some project's source code. Besides concerns about the license of the project, I believe that the program should be stripped down to something as simple as possible, while still demonstrating the behaviour it aims to demonstrate. Can someone proficient in Lua take a look?
[[User:NingOTI|NingOTI]] ([[User talk:NingOTI|talk]]) 15:29, 9 January 2018 (UTC)
 
:Amen (to the desire for simplicity). Thanks for KISSing it. [[User:Stevebroshar|Stevebroshar]] ([[User talk:Stevebroshar|talk]]) 13:44, 11 April 2024 (UTC)
 
== Why not mention delegates in C# ? ==
 
Dear all,
 
I don't understand why in the C# section for [[Callback (computer programming)]] there is not a mention to delegates, that are a usual way of creating callbacks in C#.
 
All the best,<br/>
--[[User:Hgfernan|Hgfernan]] ([[User talk:Hgfernan|talk]]) 11:59, 2 December 2019 (UTC)
:I agree, it is part of the expected and well-typed way of defining callbacks in C#. [[User:Rp|Rp]] ([[User talk:Rp|talk]]) 13:39, 2 December 2019 (UTC)
:You are not ''directly'' asking a question or proposing something. I suggest a more direct approach; otherwise your comment will not result in action. IMO, the best way to get action on WP (any wiki) is to roll up your sleaves and edit. [[User:Stevebroshar|Stevebroshar]] ([[User talk:Stevebroshar|talk]]) 13:43, 11 April 2024 (UTC)
 
== Sorry, I couldn't resist and edited page ==
 
You don't understand what it is because the term itself is misleading. (Evgeniy)--[[Special:Contributions/85.140.3.98|85.140.3.98]] ([[User talk:85.140.3.98|talk]]) 12:01, 14 September 2024 (UTC)
 
"Callback" - is noob term in [[computer programming]] into [[Abstraction (computer science)|abstraction layer]], implying supposedly a [[Function (computer programming)|function]] that sended as a parameter to another function and may be called by it. Although in fact it is not the function itself that is transferred, but only its address of ___location in memory (pointer), as a rule, this is 32-bit or 64-bit address. Calling a function means going to this address, as a rule, using the assembler command call and returning after performing all operations using the assembler command ret. Thus, this term misleads programmers, since the actual transfer of the code itself does not occur, but the transfer of the address occurs, and the call itself is not reverse, but direct.
 
:I find what you're writing difficult to follow, and I think it is too implementation specific, but I do agree that the opening sentence is confusing and needs to be changed.
:It says: ''a callback is a function that is stored as data (a reference) and designed to be called by another function – often back to the original abstraction layer.'' I have two objections to this:
:First, "a function that is stored as data (a reference)" is nonsense. Either you pass a function itself, or you pass a reference to the function; both are possible, but they are not the same thing.
:Second, what is this "original abstraction layer" doing there? Callbacks are simply a mechanism; they may or may not be used to create an abstraction layer of some kind, but no such layer needs to exist for a callback to be created.
:This is much like starting an article on ''Apple'' with the sentence: ''an apple is a fruit that is stored as organic matter (a word) and designed to be eaten by man - notably, in the creation of original sin''. No, sir or madam, either you pass an apple or you pass the word "apple", but they are not the same thing; and while passing an apple was part of the creation of original sin, apples exist completely separately from it and there is no need to mention it in the first sentence on apples. [[User:Rp|Rp]] ([[User talk:Rp|talk]]) 20:49, 16 September 2024 (UTC)