Disclaimer

The content of this blog is my personal opinion only. Although I am an employee - currently of Nvidia, in the past of other companies such as Iagination Technologies, MIPS, Intellectual Ventures, Intel, AMD, Motorola, and Gould - I reveal this only so that the reader may account for any possible bias I may have towards my employer's products. The statements I make here in no way represent my employer's position, nor am I authorized to speak on behalf of my employer. In fact, this posting may not even represent my personal opinion, since occasionally I play devil's advocate.

See http://docs.google.com/View?id=dcxddbtr_23cg5thdfj for photo credits.

Thursday, September 08, 2016

Fat commas / fat arrows in series

I just got bitten by a bug caused by two fat commas / fat arrows in series:

$ bash $> perl -e 'use strict; use warnings; my @v = ( a=> b => 1 )'
✓ 
actually in a function; actually in a constructor for an object (blessed hash), so I was thinking {} when it was new( a=>b=>1).

$ bash $>  perl -e '
    use strict; use warnings;
    sub kwargs_func{ print "inside\n"; my %kw = $_[0] ;};
    kwargs_func( a=> b => 1 )
'
inside
Odd number of elements in hash assignment at -e line ##.
✓ 
Obviously I found the bug fairly quickly - but I would prefer to have had a compile-time error or warning rather than a run-time error.



Q: are there any good uses for fat commas in series?



I am surprised that there was not a 'use warnings' warning for this.



---



I like functions with keyword arguments.  In Perl there are two main ways to do this:

func_hash_as_array_arg( kwarg1=>kwval1, kwarg2=>kwval2 )
func_hashref_as_scalar_arg( { kwarg1=>kwval1, kwarg2=>kwval2 } )
which can be mixed with positional in a reasonably nice way
func( posarg1, posarg2, kwarg1=>kwval1, kwarg2=>kwval2 )
func( posarg1, posarg2, { kwarg1=>kwval1, kwarg2=>kwval2 } )
and also in less nice ways
func( { kwarg1=>kwval1, kwarg2=>kwval2 }, varargs1, vargags2, ... )
Although I prefer f(k1=>v1) to f({k1=>v1}) - less clutter - the fact that the hashref "keyword argument group" gives a slight bit more compile-time check is interesting. I may flip.



Of course, the real problem is that Perl needs a proper syntax for keyword arguments.



Perl6 does it better.



---



For grins, some related code examples with 2 fat commas in series.

$ bash $>  perl -e 'use strict; use warnings; my %v = ( a=> b => 1 )'
Odd number of elements in hash assignment at -e line 1.
$ bash $>  perl -e 'use strict; use warnings; my $e = { a=> b => 1 }'
Odd number of elements in anonymous hash at -e line 1.
✓ 
$ bash $>  perl -e 'use strict; use warnings; my $e = [ a=> b => 1 ]'
✓ 
$ bash $>  perl -e '
    use strict; use warnings;
    sub kwargs_func{ print "inside\n"; my %kw = $_[0] ;};
    kwargs_func( a=> b => 1 )
'
inside
Odd number of elements in hash assignment at -e line ##.
✓ 
$ bash $>  perl -e '
   use strict; use warnings;
   sub kwargs_func{ print "inside\n"; my %kw = %{$_[0]} ;};
   kwargs_func( {a=> b => 1} )
'
Odd number of elements in anonymous hash at -e line ##.
inside
✓ 
---

Not the same problem, but along the same lines: When a fat comma is confusing | Samuel Kaufman [blogs.perl.org]


'via Blog this'

No comments: