Front page | perl.perl5.porters |
Postings from March 2023
Re: Pre-RFC Deep constants
Thread Previous
|
Thread Next
From:
demerphq
Date:
March 12, 2023 11:20
Subject:
Re: Pre-RFC Deep constants
Message ID:
CANgJU+VaUT0XGamBzbuN08NjWUqFFrj5vsuRsDCCNFG2HLQnNg@mail.gmail.com
Sorry for the premature send.
On Sun, 12 Mar 2023 at 12:12, demerphq <demerphq@gmail.com> wrote:
> On Sun, 12 Mar 2023 at 10:53, Branislav ZahradnÃk <happy.barney@gmail.com>
> wrote:
>
>>
>>
>> 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;
>>
>
> This does what I expect:
>
Attempt to access disallowed key 'baz' in a restricted hash at t.pl line 7.
Is that not what you wanted? Am I missing something still?
>
>> such MVP is imho idea, why not exporting existing behaviour via unified
>> :const ?
>>
>
BY "MVP" do you mean "Minimum Viable Product"?
>
>> 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 } };
>>
>
Not sure I follow you here. I suspect you and I are using different
definitions of "literal values". To me the 'bar' key is being assigned a
reference, so I wouldnt call it a literal value. If you mean that we should
restrict constants to unreferenced objects we could probably do that if we
wanted, but typically perl does not distinguish between a value containing
a hashref and an anonymous hash constructor. The only way we could would
be to check its refcount, which we tend to avoid.
>
>> - another already constant structure
>>
>> my @foo : const = (1, 2, 3);
>> my $bar = { foo => \ @foo };
>>
>
Yeah so that would blow away using the refcount to determine if the value
was legal.
> - it is constant function or its product
>>
>> sub foo : const { ... }
>> my $bar : const = { foo => \& foo, result => [ foo ] };
>>
>
Yeah, now you are getting into the realm of turing completeness. I can't
imagine us ever implementing this.
>
>> for MVP, const ffunction may imply :prototype () (like use constant does)
>> later it may accept const arguments only
>>
>
I think it is unlikely anyone would ever be able to code this, and I dont
think it would be reasonable to base an RFC on expecting to achieve it.
Much more likely would be to simply say "we deep copy the entire constant
structure at initialization time, and make the copy read-only/constant for
the lifetime of the process".
Yves
--
perl -Mre=debug -e "/just|another|perl|hacker/"
Thread Previous
|
Thread Next