php-slang icon indicating copy to clipboard operation
php-slang copied to clipboard

Using option makes it hard to determinate returned type

Open mieszkomalawski opened this issue 8 years ago • 2 comments

From examples:

public function findByEmail(string $email) : Option { return Option::of($this->findOneBy(['email' => $email])); }

We are returning option object. Client code has not way of telling what can be contents of option without investigating repository class ( and no support from IDE ). This can be especially problematic if there are many levels of indirection.

In examples client code looks like this:

public function userByEmailAction(string $email): Resource { return $this->userRepository->findByEmail($email)) ->map(function (User $user) { return new Response($user, HTTP_OK); }) ->getOrElse(new Response('', HTTP_NOT_FOUND)); }

But how can I know what should closure used in map type to ? You have to examine repository class to find out that it is some User class. On language and IDE level You can only see that Option is returned.

Unless I am missing something this can be huge problem in day to day development

mieszkomalawski avatar Sep 17 '17 11:09 mieszkomalawski

yes it is a big issue - in a current version there is no way to determine this types and ide won't help you

we think about some solution for this problem - the best would be to have generic types in a language which unfortunatelly is not there yet (but probably will be https://wiki.php.net/rfc/generics) For now I don't have a conclusion for this problem and the only reasonable solution seem to be running your apps on top of a HHVM which is PHP compatible and it has generic types (but we still have to investigate if it;s a good idea taking into account that symphony and other dependencies we use don't block us here).

wit3k avatar Sep 27 '17 10:09 wit3k

If Option had a filter method you could check if the value met your expectations and it would return a None if it doesn’t.

I’m working on an Option library that allows you to create Option types through extension which helps solve this problem. https://github.com/maarky/option

maarky avatar Feb 06 '18 22:02 maarky