Rudy Velthuis 91 Posted February 11, 2019 5 hours ago, Lars Fosdal said: Close, its a Self-Contained Compilable Example, sometimes also called a SSCCE, i.e. a Short SCCE. I'll rewrite the article for better readability. Hmmm... I knew I had read something like it before, but couldn't find it anywhere (in a meaning that made sense). The missing S did it. Share this post Link to post
Rudy Velthuis 91 Posted February 11, 2019 On 2/8/2019 at 2:19 PM, Lars Fosdal said: Code that looks correct, and appear to compile alright, but which doesn't execute well. Can you spot the error? See my blog post for a link to a SCCE. var Handler: THandlerClass; hType: THandlers; begin Broker.Clear; for hType in [foo, bar] do begin case hType of foo: Handler := TFoo.Create; bar: Handler := TBar.Create; end; Broker.AddHandler(Handler.OnHandle); end; end; https://larsfosdal.blog/2019/02/08/delphi-pitfalls-of-anonymous-methods-and-capture/ FWIW, this is similar to the call-by-name problem (Jensen's device) in Algol 60. And similar problems can happen if people do: for I := 1 to 10 do begin MyRecord.Int := I; MyPlainTList.Add(@MyRecord); end; The entire plain TList is now filled with pointers to the same record, and that contains the value 10. <g> Share this post Link to post