Skip to content

[feature] an infix isa operator #17200

Closed
Closed
@leonerd

Description

@leonerd

It is quite common in Perl code to want to know if a given object instance is derived from a given class. Such code as

if( $obj->isa( "Some::Class" ) ) ...

is however insufficient because it relies on invoking a method on what may not in fact be an object at all. Sufficiently-defensive code solves this by such constructs as:

use Scalar::Util 'blessed';

if( blessed $obj and $obj->isa( "Some::Class" ) ) ...

There are also CPAN-based solutions that try to make this nicer:

use Safe::Isa '$_isa';

if( $obj->$_isa( "Some::Class" ) ) ...

Such an omission in core Perl appears especially unfortunate in the wider context of thoughts around bringing in more of an OO system.

It would be nice if core Perl provided a basic infix operator to perform this test safely. Such an operator could additionally parse a bareword package name correctly in the common case, thus not requiring a "quoted" name:

use feature 'isa';

if( $obj isa Some::Class ) ...

In addition it may be useful to have a does operator that tests ->DOES instead, though I will admit to not fully understanding the subtle semantic difference is between the two.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions