From 541922a0bad1b095eb56f374de636dd743bad408 Mon Sep 17 00:00:00 2001 From: Jeremy Studer Date: Sat, 28 Jul 2018 15:42:15 -0400 Subject: [PATCH 1/3] Add Callable indexing candidate for CArray Makes it possible to index CArrays with WhateverCode e.g, $my-carray[0..*-5] --- lib/NativeCall.pm6 | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/NativeCall.pm6 b/lib/NativeCall.pm6 index b0f9a3511cf..aae9875430f 100644 --- a/lib/NativeCall.pm6 +++ b/lib/NativeCall.pm6 @@ -588,6 +588,12 @@ multi sub postcircumfix:<[ ]>(CArray:D \array, $pos) is export(:DEFAULT, :types) multi sub postcircumfix:<[ ]>(CArray:D \array, *@pos) is export(:DEFAULT, :types) { @pos.map: { array.AT-POS($_) }; } +multi sub postcircumfix:<[ ]>(CArray:D \array, Callable:D $block) is export(:DEFAULT, :types) { + nqp::stmts( + (my $*INDEX = 'Effective index'), + array[$block.pos(array)] + ) +} multi trait_mod:(Routine $r, :$symbol!) is export(:DEFAULT, :traits) { $r does NativeCallSymbol[$symbol]; From 68ebe71e64ab5e2cf1034b67b61f96b9f8e5de17 Mon Sep 17 00:00:00 2001 From: Jeremy Studer Date: Sat, 28 Jul 2018 15:45:41 -0400 Subject: [PATCH 2/3] Add CArray Whatever and zen indexing candidates And Hyperwhatever (NYI exception as in List/Array slices). --- lib/NativeCall.pm6 | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/NativeCall.pm6 b/lib/NativeCall.pm6 index aae9875430f..94f7488a4c1 100644 --- a/lib/NativeCall.pm6 +++ b/lib/NativeCall.pm6 @@ -594,6 +594,15 @@ multi sub postcircumfix:<[ ]>(CArray:D \array, Callable:D $block) is export(:DEF array[$block.pos(array)] ) } +multi sub postcircumfix:<[ ]>(CArray:D \array) is export(:DEFAULT, :types) { + array[^array.elems] +} +multi sub postcircumfix:<[ ]>(CArray:D \array, Whatever:D) is export(:DEFAULT, :types) { + array[^array.elems] +} +multi sub postcircumfix:<[ ]>(CArray:D \array, HyperWhatever:D) is export(:DEFAULT, :types) { + X::NYI.new(feature => 'HyperWhatever in CArray index').throw; +} multi trait_mod:(Routine $r, :$symbol!) is export(:DEFAULT, :traits) { $r does NativeCallSymbol[$symbol]; From 2a803a399070735df295f309d1321143b80a8d52 Mon Sep 17 00:00:00 2001 From: Jeremy Studer Date: Sat, 28 Jul 2018 17:24:31 -0400 Subject: [PATCH 3/3] Fixed zen CArray candidate to return itself As zoffix++ pointed out, the zen candidates of other types always return themselves. Doing the same thing here and adding a Str candidate on CArray so that it stringifies consistent with other list-like types. --- lib/NativeCall.pm6 | 2 +- lib/NativeCall/Types.pm6 | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/NativeCall.pm6 b/lib/NativeCall.pm6 index 94f7488a4c1..e8ef8294022 100644 --- a/lib/NativeCall.pm6 +++ b/lib/NativeCall.pm6 @@ -595,7 +595,7 @@ multi sub postcircumfix:<[ ]>(CArray:D \array, Callable:D $block) is export(:DEF ) } multi sub postcircumfix:<[ ]>(CArray:D \array) is export(:DEFAULT, :types) { - array[^array.elems] + array.ZEN-POS } multi sub postcircumfix:<[ ]>(CArray:D \array, Whatever:D) is export(:DEFAULT, :types) { array[^array.elems] diff --git a/lib/NativeCall/Types.pm6 b/lib/NativeCall/Types.pm6 index 862ac9d052f..421b528ac19 100644 --- a/lib/NativeCall/Types.pm6 +++ b/lib/NativeCall/Types.pm6 @@ -218,6 +218,8 @@ our class CArray is repr('CArray') is array_type(Pointer) { $what; } + method Str { self.join(' ') } + method elems { nqp::elems(self) } method list {