Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Substitutions return Bools, not the resulting Str in Rakudo #2325

Closed
p6rt opened this issue Jan 11, 2011 · 8 comments
Closed

Substitutions return Bools, not the resulting Str in Rakudo #2325

p6rt opened this issue Jan 11, 2011 · 8 comments
Labels

Comments

@p6rt
Copy link

p6rt commented Jan 11, 2011

Migrated from rt.perl.org#82108 (status was 'resolved')

Searchable as RT82108$

@p6rt
Copy link
Author

p6rt commented Jan 11, 2011

From @masak

<masak> rakudo​: sub y2i($adjective is copy) { s/y$/i/ given $adjective
}; sub compare($adjective) { [$_, .&y2i ~ "er", .&y2i ~
"est"].fmt("%s", ", ") given $adjective }; say compare("funny")
<p6eval> rakudo ea8c88​: OUTPUT«funny, funnier, funniest␤»
<tadzik> s/y$/i/ given $adjective – nice one
<tadzik> but why not just $adjective ~~ s/y$/i/?
<masak> rakudo​: sub y2i($adjective is copy) { $adjective ~~ s/y$/i/ };
sub compare($adjective) { join(", ", $adjective, $adjective.&y2i ~
"er", $adjective.&y2i ~ "est") }; say compare("funny")
<p6eval> rakudo ea8c88​: OUTPUT«funny, Bool​::Trueer, Bool​::Trueest␤»
<masak> o.O
<jnthn> lol
* masak submits rakudobug
<jnthn> masak​: Is it bug?
<masak> jnthn​: I declare it a bug.
<jnthn> rakudo​: my $x = "lol"; say $x ~~ s/o/0/;
<p6eval> rakudo ea8c88​: OUTPUT«Bool​::True␤»
<masak> based on tadzik's expectations, and the lack of spec.
<jnthn> That's...curious. :)
<tadzik> it certainly isn't DWIM
<jnthn> No
<jnthn> wtf. :/
<jnthn> rakudo​: my $x = "lol"; say ($x ~~ s/o/0/).WHAT;
<p6eval> rakudo ea8c88​: OUTPUT«Bool()␤»
<jnthn> Wow
<jnthn> I thought it was doing something with a match object that it
was getting back somehow
<jnthn> But...not even that.
<masak> definitely something for RT.
<jnthn> aye
<jnthn> Not sure what the spec is, but the above outcome is awfulest.
<moritz_> p5 people want to write if $x ~~ s/foo/bar/ { ... }
<masak> I understand moritz_' explanation, but I don't like it.
<tadzik> anyways, won't Match object evaluate to true anyway?
<tadzik> and if the match fails, there will be no object, no?
<jnthn> Yes
<tadzik> jnthn​: good too
<masak> tadzik​: I don't want it to return the match object. I'd like
it to return the resulting Str.
<masak> tadzik​: if it returned the match object in my case above, it'd
stringify to 'y'
<jnthn> masak​: I'm sure you know the counter case though. $x = 'x'; if
$x ~~ s/x// { ... }
<masak> right. [22​:31]
<jnthn> It successfully substituted, but the resulting string is empty
and thus false.
<masak> I see that.
<jnthn> Maybe we just gotta choose our wart.
<masak> and an obvious patch to that would be to do 'but True' on the Str...
<masak> ...but I am reluctant to propose that.
<jnthn> I was gonna suggest that but then didn't because I worry about
it propagating, or confusion over "the return value of it is now true
but $x isn't...wtf" :)
<masak> right.
<jnthn> And because I don't really want to create a mixed-in string
for every substitution.
<jnthn> Given that but is a clone.
<masak> nodnod
<jnthn> (yes, it'd re-use the string buffer, but still...)
<masak> honestly, I could live with the Str sometimes boolifying to false.
<masak> that might even come in handy once in a while.
<jnthn> True
<masak> while s/(.)$// { ... }
<jnthn> I can imagine use cases where that's actually nice.

@p6rt
Copy link
Author

p6rt commented Oct 8, 2011

From @coke

Same(ish) behavior in rakudo 33f1cf.

--
Will "Coke" Coleda

@p6rt
Copy link
Author

p6rt commented Oct 8, 2011

@coke - Status changed from 'new' to 'open'

@p6rt
Copy link
Author

p6rt commented Feb 26, 2014

From @coke

On Sat Oct 08 14​:20​:46 2011, coke wrote​:

Same(ish) behavior in rakudo 33f1cf.

And 974d00.
--
Will "Coke" Coleda

@p6rt
Copy link
Author

p6rt commented Jul 22, 2014

From @peschwa

On Tue Feb 25 17​:23​:38 2014, coke wrote​:

On Sat Oct 08 14​:20​:46 2011, coke wrote​:

Same(ish) behavior in rakudo 33f1cf.

And 974d00.

A PR for this exists at [1].

[1] rakudo/rakudo#295

@p6rt
Copy link
Author

p6rt commented Jul 23, 2014

From @peschwa

On Tue Jul 22 13​:26​:16 2014, peschwa@​gmail.com wrote​:

On Tue Feb 25 17​:23​:38 2014, coke wrote​:

On Sat Oct 08 14​:20​:46 2011, coke wrote​:

Same(ish) behavior in rakudo 33f1cf.

And 974d00.

A PR for this exists at [1].

[1] rakudo/rakudo#295

[1] and onwards has some discussion on this issue. The spec didn't seem completely clear on this, but has been updated in [2]. The thus specified behavior is implemented in [3]. There's two tests in S05-substitution/subst.t that will have to be update to match the new behavior.

[1] http://irclog.perlgeek.de/perl6/2014-07-22#i_9064394
[2] Raku/old-design-docs@eca5f2f2df
[3] rakudo/rakudo#295

@p6rt
Copy link
Author

p6rt commented Feb 9, 2015

From @Mouq

The current behavior is for s/// to return the match object of what was matched to be substituted for, per spec​:

$ perl6 -e'sub y2i($adjective is copy) { $adjective ~~ s/y$/i/ }; sub compare($adjective) { join(", ", $adjective, $adjective.&y2i ~ "er", $adjective.&y2i ~ "est") }; say compare("funny")'
funny, yer, yest

(in this case, .subst or .=subst is probably what is really wanted)

As peschwa++ said, this is tested against in S05-substitution/subst.t

Marking this issue as resolved :)

On Tue Jul 22 19​:24​:02 2014, peschwa@​gmail.com wrote​:

On Tue Jul 22 13​:26​:16 2014, peschwa@​gmail.com wrote​:

On Tue Feb 25 17​:23​:38 2014, coke wrote​:

On Sat Oct 08 14​:20​:46 2011, coke wrote​:

Same(ish) behavior in rakudo 33f1cf.

And 974d00.

A PR for this exists at [1].

[1] rakudo/rakudo#295

[1] and onwards has some discussion on this issue. The spec didn't
seem completely clear on this, but has been updated in [2]. The thus
specified behavior is implemented in [3]. There's two tests in S05-
substitution/subst.t that will have to be update to match the new
behavior.

[1] http://irclog.perlgeek.de/perl6/2014-07-22#i_9064394
[2] Raku/old-design-docs@eca5f2f2df
[3] rakudo/rakudo#295

@p6rt
Copy link
Author

p6rt commented Feb 9, 2015

@Mouq - Status changed from 'open' to 'resolved'

@p6rt p6rt closed this as completed Feb 9, 2015
@p6rt p6rt added the Bug label Jan 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant