Documentation for routine Int assembled from the following pages:

Class: IntStr §

From IntStr

(IntStr) method Int §

method Int

Returns the integer value of the IntStr.

Class: List §

From List

(List) method Int §

Defined as:

method Int(List:D: --> Int:D)

Returns the number of elements in the list (same as .elems).

say (1,2,3,4,5).Int# OUTPUT: «5␤»

Class: Map §

From Map

(Map) method Int §

Defined as:

method Int(Map:D: --> Int:D)

Returns the number of pairs stored in the Map (same as .elems).

my $m = Map.new('a' => 2'b' => 17);
say $m.Int;                                       # OUTPUT: «2␤»

Role: Rational §

From Rational

(Rational) method Int §

Defined as:

method Int(Rational:D: --> Int:D)

Coerces the invocant to Int by truncating non-whole portion of the represented number, if any. If the denominator is zero, will fail with X::Numeric::DivideByZero.

Enum: Bool §

From Bool

(Bool) routine Int §

multi method Int(Bool:D --> Int:D)

Returns the value part of the enum pair.

say False.Int;                                # OUTPUT: «0␤» 
say True.Int;                                 # OUTPUT: «1␤»

Class: IO::Path §

From IO::Path

(IO::Path) method Int §

Defined as:

method Int(IO::Path:D: --> Int:D)

Coerces .basename to Int. Fails with X::Str::Numeric if base name is not numerical.

Class: Str §

From Str

(Str) method Int §

Defined as:

method Int(Str:D: --> Int:D)

Coerces the string to Int, using the same rules as Str.Numeric.

Class: Num §

From Num

(Num) method Int §

method Int(Num:D:)

Converts the number to an Int. Fails with X::Numeric::CannotConvert if the invocant is a NaN or Inf/-Inf. No rounding is performed.

Role: Enumeration §

From Enumeration

(Enumeration) method Int §

Defined as:

multi method Int(::?CLASS:D:)

Takes a value of an enum and returns it after coercion to Int:

enum Numbers ( cool => '42'almost-pi => '3.14'sqrt-n-one => 'i' );
say cool.Int;           # OUTPUT: «42␤» 
say almost-pi.Int;      # OUTPUT: «3␤» 
try say sqrt-n-one.Int;
say $!.message if $!;   # OUTPUT: «Cannot convert 0+1i to Int: imaginary part not zero␤»

Note that if the value cannot be coerced to Int, an exception will be thrown.

Role: Real §

From Real

(Real) method Int §

method Int(Real:D:)

Calls the Bridge method on the invocant and then the Int method on its return value.

Class: StrDistance §

From StrDistance

(StrDistance) method Int §

Defined as:

multi method Int(StrDistance:D:)

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

Class: Cool §

From Cool

(Cool) method Int §

Defined as:

multi method Int()

Coerces the invocant to a Numeric and calls its .Int method. Fails if the coercion to a Numeric cannot be done.

say 1+0i.Int;             # OUTPUT: «1␤» 
say <2e1>.Int;            # OUTPUT: «20␤» 
say 1.3.Int;              # OUTPUT: «1␤» 
say (-4/3).Int;           # OUTPUT: «-1␤» 
say "foo".Int.^name;      # OUTPUT: «Failure␤»

Class: Match §

From Match

(Match) method Int §

Defined as:

method Int(Match:D: --> Int:D)

Tries to convert stringified result of the matched text into Int.

say ('12345' ~~ /234/).Int;       # OUTPUT: «234␤» 
say ('12345' ~~ /234/).Int.^name# OUTPUT: «Int␤» 
# the next line produces a warning about using Nil (result of a no match) in numeric context 
say ('one-two' ~~ /234/).Int;     # OUTPUT: «0␤» # because Nil.Int returns 0