develooper Front page | perl.perl5.porters | Postings from December 2021

Re: No implicit coercion?

Thread Previous | Thread Next
From:
H.Merijn Brand
Date:
December 28, 2021 09:05
Subject:
Re: No implicit coercion?
Message ID:
20211228100520.27068eef@pc09
On Mon, 27 Dec 2021 18:35:05 +0000 (UTC), Ovid via perl5-porters <perl5-porters@perl.org> wrote:

> Hi all,
> 
> This is not a pre-RFC, but I think most of us have been bitten at times by bugs like this:
> 
>     my @values = (in => "data.csv");

I guess you mean `my @values = csv (in => "data.csv");`
                               ^^^^

>     $values[2]++;
> 
> Except that $values[2] was the string "n/a" and now it's "1".
> 
> The core of the idea is simple. it would be lovely to have something to prevent implicit coercion in a given lexical scope:
> 
>     use explicit;
>     my $num = 3;   # integer
>     $num += .42;    # fatal because it creates a float

I would oppose. *IF* I would want this, and yes, that happens, I want
strongly types variables and not something more global that changes all
variable behavior.

  my Int $num = 3;
  $num += .42; # Fatal

> Instead, we have to do this:
> 
>     use explicit;
>     my $num = 4;
>     $num = float($num);
>     # or `$num = 4.0;`
>     $num += .42; # works just fine
> 
> This would also be fatal:
> 
>     use explicit;
>     my $value = {};
>     say ++$value;
> 
> Is this possible? I realize there are many edge cases.

Even *if* possible, I would rather see that energy put into typed
variables instead. And I would use them. Typed variables also open
up to multi-subs as in Raku.

> Ovid

-- 
H.Merijn Brand  https://tux.nl   Perl Monger   http://amsterdam.pm.org/
using perl5.00307 .. 5.33        porting perl5 on HP-UX, AIX, and Linux
https://tux.nl/email.html http://qa.perl.org https://www.test-smoke.org
                           

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