Skip Menu |
Report information
Id: 128090
Status: open
Priority: 0/
Queue: perl6

Owner: Nobody
Requestors: cpan [at] zoffix.com
Cc:
AdminCc:

Severity: (no value)
Tag: (no value)
Platform: (no value)
Patch Status: (no value)
VM: (no value)



Subject: [RFC] Easy Way to Export Only Specific Symbols At Compile Time
Download (untitled) / with headers
text/plain 864b
Currently, there seems to be no easy way to import only specific symbols from a module *at compile time*. For example, if I want to use a module that exports a whole ton of functions but I only one just one, I have to write something complicated like this: need Test; my &is := &Test::EXPORT::DEFAULT::is; This difficulty will result in many programmers trashing their namespaces out of convenience, resulting in hard-to-find bugs, especially when newer version of `use`d module introduces new subs. I propose we create another reserved import tag :SYM that will take a list of SYMbols we want to import, eg: use Test :SYM<&is &ok>; Although I'm ignorant of the implementation of the :ALL tag, I'd think implementing :SYM would be similar (you'd just grep for wanted symbols). Relevant IRC conversation: http://irclog.perlgeek.de/perl6/2016-05-07#i_12446098
Download (untitled) / with headers
text/plain 722b
To add: pmichaud pointed out on IRC that S11 already defines a way to export symbols: https://rt.perl.org/rt3//Public/Bug/Display.html?id=128090 However, that doesn't seem to work unless the module *author* defines extra EXPORT sub, which puts the feature out of the module user's control: <ZoffixWin> m: use Test <&is>; say is 2, 2 <camelia> rakudo-moar ac36d2: OUTPUT«===SORRY!=== Error while compiling /tmp/zm89zGOGgu␤Error while importing from 'Test':␤no EXPORT sub, but you provided positional argument in the 'use' statement␤at /tmp/zm89zGOGgu:1␤------> use Test <&is>⏏; say is 2, 2␤» So now, I don't know if a proposed :SYM is needed or something needs to be improved with the `is export` trait.
From: Lloyd Fournier <lloyd.fourn [...] gmail.com>
Subject: Re: [perl #128090] [RFC] Easy Way to Export Only Specific Symbols At Compile Time
To: perl6-compiler [...] perl.org, bugs-bitbucket [...] rt.perl.org
Date: Sun, 08 May 2016 13:53:33 +0000
Download (untitled) / with headers
text/plain 1.8k
Something like this has also passed through my head.

What about use Test :ALL<&is &ok> or use Test :SOME-TAG<&foo &bar>.

so you can filter out what you want with arguments to the tag. This seems to be better and more backwards-compy because with current rakudo use Test :ALL<&is &ok> will still work (it will just do a normal :ALL import).

Zoffix++ for bringing this up.

also, I think that require could be adapted to work like this. Right now require can only import from DEFAULT which seems like a needless restriction (the design docs seem a little confused on this point).

require Test <&is &ok>, :SOME-TAG<&foo &bar>;

I think it's a good feature and importing would be much more consistent.

LL


On Sat, May 7, 2016 at 11:12 AM Zoffix Znet <perl6-bugs-followup@perl.org> wrote:
Show quoted text
# New Ticket Created by  Zoffix Znet
# Please include the string:  [perl #128090]
# in the subject line of all future correspondence about this issue.
# <URL: https://rt.perl.org/Ticket/Display.html?id=128090 >


Currently, there seems to be no easy way to import only specific symbols from a module *at compile time*.

For example, if I want to use a module that exports a whole ton of functions but I only one just one, I have to write something complicated like this:

need Test;
my &is := &Test::EXPORT::DEFAULT::is;

This difficulty will result in many programmers trashing their namespaces out of convenience, resulting in hard-to-find bugs, especially when newer version of `use`d module introduces new subs.

I propose we create another reserved import tag :SYM that will take a list of SYMbols we want to import, eg:

use Test :SYM<&is &ok>;

Although I'm ignorant of the implementation of the :ALL tag, I'd think implementing :SYM would be similar (you'd just grep for wanted symbols).

Relevant IRC conversation: http://irclog.perlgeek.de/perl6/2016-05-07#i_12446098
Download (untitled) / with headers
text/plain 206b
There was a comment on IRC from someone who doesn't have an account (http://irclog.perlgeek.de/perl6/2016-06-30#i_12762654 ): use Module :ONLY<foo bar baz>; I do like :ONLY more than my original :SYM
RT-Send-CC: perl6-compiler [...] perl.org
Download (untitled) / with headers
text/plain 651b
On Thu, 30 Jun 2016 05:12:09 -0700, cpan@zoffix.com wrote: Show quoted text
> There was a comment on IRC from someone who doesn't have an account > (http://irclog.perlgeek.de/perl6/2016-06-30#i_12762654 ): > > use Module :ONLY<foo bar baz>; > > I do like :ONLY more than my original :SYM
Just got bitten by this in real-life code: https://github.com/zostay/p6-DOM-Tiny/issues/5 A module was overly lavish with its exports, exporting symbol `Text` (something I've no use for), which conflicted with `Text` namespace from another module I was trying to load. Not having a way to prevent those exports, I had to wrap the module into another module of my own making.


This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

For issues related to this RT instance (aka "perlbug"), please contact perlbug-admin at perl.org