develooper Front page | perl.perl5.porters | Postings from March 2023

Re: Pre-RFC Deep constants

Thread Previous | Thread Next
From:
Branislav Zahradník
Date:
March 12, 2023 09:53
Subject:
Re: Pre-RFC Deep constants
Message ID:
CAB=rbOkX44rM6rRUNrytgDOsFzagfz3i=Oyq6zk4E7bBOEKbkQ@mail.gmail.com
On Sat, 11 Mar 2023 at 16:50, demerphq <demerphq@gmail.com> wrote:

> On Sat, 11 Mar 2023 at 15:20, Matthew Persico <matthew.persico@gmail.com>
> wrote:
>
>> How about an MVP that creates just scalar constants?
>>
>
> Huh? We have two ways to create "constant" scalars:
>
> use constant var => "foo";
> Internals::SvREADONLY((my $var = "x"),1);
>
> Am I missing something?
>
>
>
>> That's got to be 90% of what people need, and then we can add
>> constant hashes later?
>>
>
> We have constant (aka read-only) hashes as well. It is just that they are
> *restricted*, which renders them nearly useless for most purposes.
>
> use Hash::Util qw(lock_hash_recurse);
> lock_hash_recurse(\%hash);
>
>
try:
my $foo = { bar => 1 };
my %hash = (foo => $foo);

use Hash::Util qw(lock_hash_recurse);
lock_hash_recurse(%hash);

$foo->{baz} = 2;


such MVP is imho idea, why not exporting existing behaviour via unified
:const ?

sub var :const { "foo" }
my $var : const = "x";

my %hash : const = ...
my @array : const = ...;

For mentioned MVP, constant expression should be only:
- literal values

my $foo : const = { bar => { baz => 1 } };

- another already constant structure

my @foo : const = (1, 2, 3);
my $bar = { foo => \ @foo };

- it is constant function or its product

sub foo : const { ... }
my $bar : const = { foo => \& foo, result => [ foo ] };

for MVP, const ffunction may imply :prototype () (like use constant does)
later it may accept const arguments only

Brano

Thread Previous | 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