Git Product home page Git Product logo

Comments (9)

ndench avatar ndench commented on September 26, 2024 3

Agreed LazyValue::memoize() it is.

RE

I mean: besides the current point, do you see any other valid use case?

Only other use case I can think of would be a more complicate construction than creating a single entity. However, the $factory is a callable, which should allow you to be as complicated as you like. You could even use a service with dependency injection and an __invoke() method defined.

So I don't think it's necessary to allow extension at this point. It can always be added later if a use-case arises.

from foundry.

kbond avatar kbond commented on September 26, 2024 2

What about using the term memoize? A bit computer science-y but perfectly describes the intent.

from foundry.

nikophil avatar nikophil commented on September 26, 2024 1

what about using a named constructor?

$owner = LazyValue::singleValue(fn () => UserFactory::createOne()); // or maybe a better name 😅

from foundry.

ndench avatar ndench commented on September 26, 2024 1

Perfect! Thanks for the idea @nikophil! I'll get a PR up soon.

from foundry.

kbond avatar kbond commented on September 26, 2024

Interesting use-case and solution! I'm all for a PR.

Add a flag to the existing lazy value object or new object?

from foundry.

ndench avatar ndench commented on September 26, 2024

A new object means we either have to make LazyValue non-final, or copy paste it's internals.
But a bool flag on the LazyValue is not very readable IMO.

$owner = new LazyValue(fn () => UserFactory::createOne(), true);

I'd probably lean towards making LazyValue non-final. This would then allow users to extend it themselves if they have other use-cases they'd like to handle.

However, I'm unsure if it was made final for a specific reason originally?

from foundry.

jeffreymueller avatar jeffreymueller commented on September 26, 2024

If we are trying to allow other projects to implement their own, what about using an empty interface and replace $value instaceof self checks with $value instanceof LazyValueInterface. Would look something like the following:

interface LazyValueInterface { }

final class LazyValue implements LazyValueInterface
{

...

    public function __invoke(): mixed
    {
        $value = ($this->factory)();

        if ($value instanceof LazyValueInterface) {
            return ($value)();
        }

        if (\is_array($value)) {
            return self::normalizeArray($value);
        }

        return $value;
    }

    /**
     * @internal
     */
    public static function normalizeArray(array $value): array
    {
        \array_walk_recursive($value, static function(mixed &$v): void {
            if ($v instanceof LazyValueInterface) {
                $v = $v();
            }
        });

        return $value;
    }
}

Then projects can implement something invokable for their own needs

final class MyLazyValue implements LazyValueInterface
{
    public function __invoke(): mixed
    {
    }
}

from foundry.

nikophil avatar nikophil commented on September 26, 2024

indeed, if we decide to allow users to define custom lazy values, we'd introduce an interface, but I'm really wondering if this would actually be used?
I mean: besides the current point, do you see any other valid use case?

from foundry.

nikophil avatar nikophil commented on September 26, 2024

I like it, it express well what it does, and it will be documented anyway

from foundry.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.