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

Pre-RFC: namespace literal as first class citizen

Thread Next
Branislav ZahradnĂ­k
June 30, 2022 06:34
Pre-RFC: namespace literal as first class citizen
Message ID:
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
- 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

  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

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

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

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

- should accept only namespace literals

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

  $foo isa Class::Foo::

- should support is_namespace_literal

- should provide NAMESPACE LITERAL value instead of string

- should accept only namespace literals

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


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
  $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,

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About