develooper Front page | perl.perl5.porters | Postings from June 2022

Pre-RFC: namespace literal as first class citizen

Thread Next
From:
Branislav ZahradnĂ­k
Date:
June 30, 2022 06:34
Subject:
Pre-RFC: namespace literal as first class citizen
Message ID:
CAB=rbO=KdH0MwzfFX_3EcfpbXc0c2J3BjsAV8G80p1gDdT2PAg@mail.gmail.com
Idea:
Introduce namespace literal into language with it's own ref type and so.

Current state:
- any string when used as a reference in method invocation is treated as
namespace
- language already supports trailing :: syntax, producing string without
::, and warning, when namespace is not available yet

Other languages examples:
- Java and their java.lang.Class (or .class attribute)

Expected behaviour:
- namespace literal will be comparable with string
- only namespace literal will be allowed as reference
- new coercion syntax will be introduced -> ${ $scalar} ::
- ref will return namespace literals
  - or new keyword ?
  - or new UNIVERSAL method? (eg __package__)

There should be two possible coercions:
- one just coercing thing into namespace literal, eg: ${ ? $class }::
  - will warn/die when referencing non-existing namespace
- one checking it is existing namespace literal, eg: ${ + $class }::
  - will warn/die when coercing non-existing namespace

Interaction with existing use cases:

Static method call
- existing syntax rule should be used
  NAMESPACE LITERAL -> method

Examples:
  Class::Foo:: -> new
  ${ "Class::Foo" }:: -> new

Namespace literal (aka meta) method calls
- new to access methods provided by namespace literal itself new syntax
will be available
  NAMESPACE LITERAL -> * meta_method

Examples:
  Class::Foo:: -> * is_loaded
  Class::Foo:: -> * load

exist NAMESPACE LITERAL
- returns true if it exists (ie, related stash was created)

defined NAMESPACE LITERAL
- returns true if it exists and related include didn't fail
- returns true also when NAMESPACE LITERAL is part of current include

bless
- should accept only namespace literals

isa operator
- should be restricted to accept only  namespace literals
- can support pseudo literals representing internals

Example:
  $foo isa Class::Foo::

Ref::Util
- should support is_namespace_literal

__PACKAGE__
- should provide NAMESPACE LITERAL value instead of string

@ISA
- should accept only namespace literals

# __PACKAGE__ will be namespace literal
say ref (__PACKAGE__);  # Package

Follow-ups:

change will allow to distinguish between calling methods on scalar and on
namespace literal
eg, provide builtin methods as "non-blessed" (please just accept this as
possibility, without discussion here - there will be place for that in
another Pre-RFC) methods on any scalar, eg:
  " foo "->trim

change will allow to distinguish value as "known package"

change will allows to preserve indirect method calls on class literals
(at least new, throw -> these are most interesting)
  new Class::Foo:: (...);
  throw X::Foo:: (...);

change allows introduction of pseudo-packages representing non-blessed
values
  $foo isa CORE::Scalar::
  $foo isa CORE::Number::
  $foo isa CORE::Arrayref::

change allows introduction of namespace literal "calculus":
  X::Foo:: | X::Bar::
  X::Foo:: & X::Bar::
  X::Foo:: + Role::Bar::
  Class::Foo:: += Role::Bar::

inspiration taken from Java
  catch (Foo | Bar)

change allows to implement namespace related framework without polluting
class namespace
  Class::Foo:: -> * add_method
  Class::Foo:: -> * consume_role

change will allow namespace aliasing:
  our Foo:: = Real::Bar::;
  my $foo = Foo::->new;
  ok $foo isa Real::Bar::;
  ok $foo isa Foo::;

change allows anonymous class (package) syntax
  Class::Foo:: {
    # with @ISA = (Class::Foo::)
  }

  :: {
  }

Best regards,
Brano

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About