Rules / Types, classes & roles / basic

StrDistance Reference

Contains the result of a string transformation.

What it is #

Routines #

Signatures are reproduced from the documentation, including their declared return types. A routine listed here is part of the type's published interface; whether Raku++ implements it is a separate question, answered by the examples below.

method before #

This is actually a class attribute, and called as a method returns the string before the transformation:

method after #

Also a class attribute, returns the string after the transformation:

method Bool #

Returns True if before is different from after.

method Numeric #

Returns the distance as a number.

method Int #

multi method Int(StrDistance:D:)

Returns the distance between the string before and after the transformation.

method Str #

multi method Str(StrDistance:D: --> Str)

Returns Str.

Returns an after string value.

Examples, run three ways #

Every example below comes from the official documentation, together with the output that documentation asserts. Each was then executed by Rakudo and by Raku++ when this page was built. Where the three agree, one result is shown; where they do not, all three are — because which of them is wrong is exactly the information worth having.

1 doc-drift · 4 ok

say (($ = "fold") ~~ tr/old/new/).^name;  # OUTPUT: «StrDistance␤»
Output
StrDistance

Documentation, Rakudo and Raku++ all agree.

my $str = "fold";
my $str-dist = ($str ~~ tr/old/new/);
say ~$str-dist;  # OUTPUT: «fnew␤»
say +$str-dist;  # OUTPUT: «3␤»
Output
fnew
3

Documentation, Rakudo and Raku++ all agree.

say $str-dist.before; # OUTPUT: «fold␤»
Output
fold

Documentation, Rakudo and Raku++ all agree.

say $str-dist.after;  # OUTPUT: «fnew␤»
Output
fnew

Documentation, Rakudo and Raku++ all agree.

=begin code :preamble<my $str = "fold">
my $str-dist = ($str ~~ tr/old/new/);
say $str-dist.Str; # OUTPUT: «fnew␤»
say ~$str-dist;    # OUTPUT: «fnew␤»
=end code
Documentation
fnew
fnew
Rakudo
Raku++

Both engines agree; the documentation states something else. Trust the engines.