Maybe it will be a good addition

Issue #178 resolved
Zigmund Bulinsh created an issue

Hi Stefan!

I just thought that maybe it will be a good addition to the Spring.pas unit. I have an Arc<T> type which actually performs ARC for the source object. You can check the code at https://github.com/zensan/pascal.today/blob/master/uArc.pas and description there: https://pascal.today/2016/08/02/arc-for-non-managed-types/ (sure it's inspired by Marco Cantu, but it was improved to make it really usable in real projects).

I am using this Arc<> thing and it is amazing (from my POV). And maybe (if you like the idea) that can be included in the spring (of course with some adjustments to support diff. versions).

Thank you for your time, Best regards!

Comments (9)

  1. Stefan Glienke repo owner

    Hi Zigmund,

    thanks for offering your implementation. We already have something like that, after some renamings we settled for the name Managed<T>.

    It's part of 1.2. It was inspired by several approaches in the past to implement these so called smart pointers in Delphi. However it does not only handle objects but also typed pointers. :)

    Several people blogged about it (including myself):

  2. Zigmund Bulinsh reporter

    But anyway Managed<TStringList>.Create is not as nice as just using the implicit operator (in uArc). So the type itself never constructs the source. That was an improvement from Memnarch (author of the Delphinius). And is much more convenient to use.

  3. Stefan Glienke repo owner

    Regarding release date: soon Regarding constructor: There are two ways to use it, via the anonymous method or via record type. Both have advantages and disadvantages that I explained in my blog post.

  4. Zigmund Bulinsh reporter

    Sure. I just mentioned that because I got a critics about having constructor constraint and allowing for the kind of Managed<TMyType>.Create...

    But that's not important for you anyway - as you have both ways of doing it and noone will criticize that :)

    In the first version I also had that Create() and etc, but then after critics I removed it and left only implicit operator.

    Sorry for unclear answer.

  5. Stefan Glienke repo owner

    There is no perfect way unless you are using a platform that has ARC enabled for objects.

    Otherwise there is always a trade off. If you want a "nice" creation and assignment, the record is the way to go but it comes with the disadvantage of always having to call .Value (or .This in your case) which can be a really bad if you actually just wanted to avoid the try/finally/Free block but now have to write that extra member access all the time.

    If you are using the anonymous method type then assignment is a bit more overhead but you can directly access it and call the members (but with a runtime overhead because you are actually calling the anonymous method every time).

    Another way would be to just use a regular object reference and call some function that returns the lifetime interface which then frees the wrapped object at the end of the method.

  6. Zigmund Bulinsh reporter

    You are absolutely right. So to make final word - I just wait for the Release 1.2 when it comes as stable! Then I can drop my Arc and use the Spring. But until that I am kind of forced to use my own impl..

    Thank you.

  7. Log in to comment