Git Product home page Git Product logo

Comments (3)

devnix avatar devnix commented on August 20, 2024

I'll be sure to leave here an idea. As we probably want to have fine control of what is some and what is none and each view will likely differ, why not require a callable to decide it?

$myType = Type\option(
    Type\string(),
    static fn (string $value) => '' !== $value
);

$myType->coerce('Hello world')->isSome(); // true
$myType->coerce('')->isSome(); // false

WDYT @azjezz @veewee? I will have a couple of spare weeks so I can start working on this idea if you like it

from psl.

veewee avatar veewee commented on August 20, 2024

Hello @devnix,

I was thinking : wouldn't is make more sense to let the underlying type determine wether it is some or none?
For example:

$type = option(string());
$type->coerce('some'); // Some('some');
$type->coerce(''); // Some('');

// But

$type = option(non_empty_string());
$type->coerce('some'); // Some('some');
$type->coerce(''); // None();

The logic would be as followed:

  • coerce($data)
    • If inner type coerces, then it is some($data)
    • If inner type throws exception, then it is none()
  • assert($data)
    • Validates if $data is of type Option
    • In case of some, validates if inner type asserts on unwrapped option value
  • matches($data)
    • Validates if $data is of type Option
    • In case of some, validates if inner type matches on unwrapped option value

I have not validated every possible type to make sure this logic is 100%, but think it should do the job just fine.

This way, you could use the base types to determine it is a some|none, and a converted() type (instead of a predicate) for very specific scenarios.

WDYT?

from psl.

devnix avatar devnix commented on August 20, 2024

Hi @veewee, sorry for the late response!

It is true that converted() would be a better candidate for the case I presented.

However, the coerce() behavior looks too forgiving for me. I was surprised by the first time I used shape() when I saw it failed even when the shape was correct, and then I saw that the given shape I was coercing had more keys than my documented shape, and then I learned about the allow_unknown_fields parameter.

I think that it's easier to offer stricter behaviors by default with opt-ins for a more lenient logic. For example:

$type = option(string());
$type->coerce('some'); // Some('some');
$type->coerce(''); // Some('');

$type = option(non_empty_string());
$type->coerce('some'); // Some('some');
$type->coerce(null); // None();
$type->coerce(''); // CoercionException

$type = option(
    non_empty_string(),
    none_if_not_coercible: true
);
$type->coerce('some'); // Some('some');
$type->coerce(''); // None();

However, I also doubt about performing a logic like from_nullable() with code like this:

$type = option(nullable(string()));
$type->coerce('some'); // Some('some');
$type->coerce(null); // Some(null) or None()?

from psl.

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.