tag:blogger.com,1999:blog-29331675.post7680312518297838259..comments2024-03-05T17:37:00.995+01:00Comments on The Delphi Geek: The Future of Delphigabr42http://www.blogger.com/profile/06903558857617342477noreply@blogger.comBlogger14125tag:blogger.com,1999:blog-29331675.post-42255355758275781482017-08-06T08:44:59.295+02:002017-08-06T08:44:59.295+02:00Hello. Its very interesting code for me.
Primož p...Hello. Its very interesting code for me. <br />Primož please explain why you used interfaces<br />type<br /> IFuture = interface<br /> function Value: T;<br /> end;<br />?<br /><br />Why dont simply make "TFuture=class ...end" and free it when job is done?<br /><br />numPrimes := TFuture.Create(function: integer<br /> var<br /> iPrime: integer;<br /> begin<br /> Result := 0;<br /> for iPrime := 1 to 1000000 do<br /> if IsPrime(iPrime) then<br /> Inc(Result);<br /> end<br /> );<br />try<br /> lbLog.Items.Add(Format('%d primes from 1 to 1000000',<br /> [numPrimes.Value]));<br />Finally<br /> numPrimes.free;<br />end;<br /><br />I could not understand idea of using interfaces in Delphi.<br />For what are they used for?<br /><br /><br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-29331675.post-41038409342785885962010-06-17T20:50:06.971+02:002010-06-17T20:50:06.971+02:00@Chris: Thanks for the links!@Chris: Thanks for the links!gabr42https://www.blogger.com/profile/06903558857617342477noreply@blogger.comtag:blogger.com,1999:blog-29331675.post-42504540708336029032010-06-17T20:19:10.489+02:002010-06-17T20:19:10.489+02:00@gabr - http://docwiki.embarcadero.com/RADStudio/e...@gabr - http://docwiki.embarcadero.com/RADStudio/en/Anonymous_Methods_in_Delphi explicitly says methods are directly assignable to 'reference to' type variables. While only on blogs, Barry Kelly has also said in the past that assignment compatibility with both method and standalone routines was intentional (e.g. see his comment at http://sergworks.wordpress.com/2010/01/27/anonimous-methods-in-delphi-the-internals/#comment-5).Chrishttp://delphihaven.wordpress.com/noreply@blogger.comtag:blogger.com,1999:blog-29331675.post-61146470517503885422010-06-17T15:07:22.433+02:002010-06-17T15:07:22.433+02:00Also missing from the code above:
destructor TFut...Also missing from the code above:<br /><br />destructor TFuture.Destroy;<br />begin<br /> FreeAndNil(FWorker);<br /> inherited;<br />end;<br /><br />Without that, the code will leak if a future is released before the Value function is called.gabr42https://www.blogger.com/profile/06903558857617342477noreply@blogger.comtag:blogger.com,1999:blog-29331675.post-19691178672023831052010-06-17T14:19:56.717+02:002010-06-17T14:19:56.717+02:00@Babnik: It seems that function and methods are au...@Babnik: It seems that function and methods are automatically upgraded to delegates. (You can pass a "function: integer" or "function: integer of object" to a method that expects "reference to function: integer" and everything will work correctly.) So there's no need to enhance the base class, it should already accept such functions/methods. (I've tested this with the OTL version and everything works just fine.)<br /><br />BTW, does anybody know if this behaviour is documented?gabr42https://www.blogger.com/profile/06903558857617342477noreply@blogger.comtag:blogger.com,1999:blog-29331675.post-65546675178032689162010-06-17T12:42:36.000+02:002010-06-17T12:42:36.000+02:00Something similar will appear in the OTL version: ...Something similar will appear in the OTL version: Cancel, IsCanceled and IsDone.gabr42https://www.blogger.com/profile/06903558857617342477noreply@blogger.comtag:blogger.com,1999:blog-29331675.post-3686138495594966082010-06-17T12:36:41.991+02:002010-06-17T12:36:41.991+02:00Thanks Primoz!
A little (and dirty) improvement:
...Thanks Primoz!<br />A little (and dirty) improvement:<br /><br />type<br /> IFuture = interface<br /> function Value: T;<br /> function Available: boolean; //added method<br /> end;<br /><br />//implementation<br />function TFuture.Available: boolean;<br />begin<br /> Result := WaitForSingleObject(FWorker.Handle, 1) <> WAIT_TIMEOUT; //could be more robust<br />end;<br /><br /><br />so you can check the availability of the result<br /><br /> if not numPrimes1.Available then<br /> begin<br /> ListBox1.Items.Add('Still not available');<br /> Exit;<br /> end<br /> else<br /> ListBox1.Items.Add(Format('%d primes from 1 to 1000000',<br /> [numPrimes1.Value]));Unknownhttps://www.blogger.com/profile/04445257317986095729noreply@blogger.comtag:blogger.com,1999:blog-29331675.post-34528502817293012742010-06-17T10:45:43.663+02:002010-06-17T10:45:43.663+02:00BTW, if anybody can find a Future definition on th...BTW, if anybody can find a Future definition on the MSDN, can she/he please post the link here? If I try to search for "Future", Microsoft's searcher just barfs a page of errors back to me :(gabr42https://www.blogger.com/profile/06903558857617342477noreply@blogger.comtag:blogger.com,1999:blog-29331675.post-39838844543009582212010-06-17T10:29:46.087+02:002010-06-17T10:29:46.087+02:00@Mason: Thanks for the detailed answer.
@Colin: M...@Mason: Thanks for the detailed answer.<br /><br />@Colin: More info on futures in Task Parallel Library: http://www.devx.com/dotnet/Article/39204/1763/page/5. There's also a longer description at Wikipedia: http://en.wikipedia.org/wiki/Futures_and_promises.<br /><br />The implementation above is just a proof of concept and doesn't implement advanced features from the Java implementation. The OTL version will have more functionality.gabr42https://www.blogger.com/profile/06903558857617342477noreply@blogger.comtag:blogger.com,1999:blog-29331675.post-40270131927500214352010-06-17T06:20:38.021+02:002010-06-17T06:20:38.021+02:00Hi Mason,
Thanks for the explanation! I owe quite...Hi Mason,<br /><br />Thanks for the explanation! I owe quite a bit of my understanding of all things new (and sometimes old) in Delphi to your replies in stackoverflow. I'm re-reading Primoz's posts and now it's beginning to make a bit of sense now (Not because it was poorly worded but because of my previous lack of understanding!).<br /><br />Now if I can just get my head around the rest of the OTL... :)Colinhttps://www.blogger.com/profile/01962000164959532235noreply@blogger.comtag:blogger.com,1999:blog-29331675.post-73360225636276286192010-06-17T03:23:12.251+02:002010-06-17T03:23:12.251+02:00Colin:
A future is a function that calculates a v...Colin:<br /><br />A future is a function that calculates a value that doesn't need to be ready immediately.<br /><br />Most of the time, when you call a function, the flow of control transfers to it until the function returns. You could look at it as if the function call <i>blocks</i> the current procedure until it's finished.<br /><br />A future is basically a non-blocking function call. You create it and it starts to run in a separate thread, while you do other things in the current function. Then later on, you ask for the value of the future, which it's been calculating all this time. If it's not done, it will block at that point until it's finished, otherwise it gives you the value immediately.<br /><br />For example, if you need to calculate two different values and add them together, and they're both rather involved calculations, you can put one of them in a future and have it run in a different thread and on a different processor while you're calculating the other one. Then, instead of taking X + Y amount of time to do the whole calculation, it takes X or Y, whichever is longer. If X and Y are close to equal, then you've cut your processing time in half.Mason Wheelerhttp://tech.turbu-rpg.comnoreply@blogger.comtag:blogger.com,1999:blog-29331675.post-27094437362673242112010-06-16T21:34:39.307+02:002010-06-16T21:34:39.307+02:00Hi Primoz,
I'm not familiar with the threading...Hi Primoz,<br />I'm not familiar with the threading terminology of "futures". The only reference via Google that I can find was http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/Future.html. Can you either explain what futures are for us newbies or possibly point us to somewhere on the web for more details. I am particularly interested in knowing under what circumstances would you use futures and what problems do they solve. Thanks in advance and for further extending my curiosity of Delphi threading.Colinhttps://www.blogger.com/profile/01962000164959532235noreply@blogger.comtag:blogger.com,1999:blog-29331675.post-72092033611671939582010-06-16T20:55:18.437+02:002010-06-16T20:55:18.437+02:00TFuture.Create(function: T) should be possible, tr...TFuture.Create(function: T) should be possible, true.gabr42https://www.blogger.com/profile/06903558857617342477noreply@blogger.comtag:blogger.com,1999:blog-29331675.post-5239485702514547182010-06-16T16:55:11.476+02:002010-06-16T16:55:11.476+02:00Is it not possible/preferable to make TFutureDeleg...Is it not possible/preferable to make TFutureDelegate a reference to a named function. You then create a function called, say CalculatePrime and set up the future thus :<br /><br />numPrimes := TFuture.Create(CalculatePrime);<br /><br />I just think the use of anonymous methods here can be confusing. btw, I haven't tried this, it's just a thought.Babnikhttps://www.blogger.com/profile/04040986319920784464noreply@blogger.com