Skip to content

Add an option() type to Psl\Type to coerce to a Psl\Option\Option #418

Open
@devnix

Description

@devnix

I'll leave here an example given by @azjezz:

use Psl\Str;
use Psl\Type;
use Psl\Option;

/**
 * @var Type\TypeInterface<Option\OptionInterface<string>>
 */
$type = Type\option(Type\string());

$str = 'abc';
$arr = [];

// = 3
$a = $type->coerce($str)->map(Str\length(...))->unwrapOr(0);
// = 0
$b = $type->coerce($arr)->map(Str\length(...))->unwrapOr(0);

$val = Option\none();
$type->matches($val); // true? `matches` has `@psalm-assert-if-true T $value` so now psalm will think it's `Option<string>`, not an issue

$val = Option\some(123);
$type->matches($val); // false? this would require us to unwrap it if it's a some and check the value

I think I could work on this one

Metadata

Metadata

Assignees

Labels

Priority: MediumThis issue may be useful, and needs some attention.Status: AcceptedIt's clear what the subject of the issue is about, and what the resolution should be.Status: AvailableNo one has claimed responsibility for resolving this issue.Type: EnhancementMost issues will probably ask for additions or changes.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions