Hi,
On 10/12/2015 03:41 PM, Mark Overmeer wrote:
>
> Hi all,
>
> Liz and Tux demonstrate powerful Perl6 code at each monthly meeting of
> our Mongers in Amsterdam. Looking at their examples, I collected a few
> questions of which I want to discuss the first one in this thread.
>
>
> When I look at Merijns (Tux') code, I see a huge number of :D attributes.
> https://github.com/Tux/CSV/blob/master/lib/Text/CSV.pm
>
> Close to all scalar positional parameter (51x) carry the :D flag. I count
> only 3 where the parameter does accept undef and the method is able to
> handle it. I count another 3 where the :D is missing, but the method is
> not able the handle it.
>
> The same for examples Liz shows us in our core code: most scalar
> positional parameters have :D.
>
> Writing a sub which is able to handle undef is usually more work than
> implementing "I expect sane values for all of the parameters".
>
>
> Questions:
> . are they using :D correctly?
Yes, though not everybody uses :D as much as they do. Do you check that
all the parameters that your Perl 5 methods/subs receive are defined? If
not, you wouldn't use :D in Perl 6 either.
> . the simpelest code does not handle the undef case... but now needs
> the more complex prototype to be correct.
> . it feels like the wrong default: usually you have to do something
> extra for the unusual cases, not the 90%+ usual cases.
FWIW you can now (as of a few days ago) control the default with
use invocant :D;
and
use parameters :D;
which means all those :D annotations can go away, and you have to use :_
explicitly if you want to allow all.
That said, I agree that it's the wrong default. And the design documents
even mandate a default to :D, though at the time it was written, it
wasn't clear how to switch off that default, nor how to avoid having to
write
method new(MyClassHere:U: *@args) { ... }
in the constructor, which would be quite hostile to newbies. It's still
not clear to me how to avoid that.
And I don't know if we can change it now without creating a huge havoc
in the existing ecosystem.
Another concern is that if "everything" defaults to :D, then classes
(and other type objects) aren't really first class objects anymore,
which is a really neat thing to have.
> . :D looks really ugly, don't you think? Try to explain to students
> to add this smiley everywhere.
It's not uglier than a 'die "Must be defined" unless defined $x'
Cheers,
Moritz