Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: OO design: returning string and/or special value

by daxim (Curate)
on Oct 08, 2019 at 10:38 UTC ( [id://11107197]=note: print w/replies, xml ) Need Help??


in reply to OO design: returning string and/or special value

Don't complicate the code, just make more methods. In OO they are cheap to add. (Re: OO design: returning string and/or special value (peephole Object) preempted me.)

I don't like the suggestion from Re: OO design: returning string and/or special value, flags are a code smell.

use Moops; use Data::Dx; class Special; class Foo { method _actual_work { return 'foo', Special->new } method my_method(--> Any) { return $self->_actual_work } method my_method_special(--> InstanceOf['Special']) { return ($sel +f->_actual_work)[1] } method my_method_string(--> Str) { ($self->_actual_work)[0] } } my $f = Foo->new; Dx $f->my_method; Dx $f->my_method_special; Dx $f->my_method_string;
I also experimented with multi-methods, but unfortunately Perl is too irregular to allow dispatch on return types.

PS: Curate GET

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11107197]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2025-03-16 13:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When you first encountered Perl, which feature amazed you the most?










    Results (54 votes). Check out past polls.