Skip to end of metadata
Go to start of metadata

A slicing operation is a simple way to extract a range of elements from a container. The boo compiler supports native slicing operations on lists, arrays and strings. Support for user defined slicing operations is planned but currently not implemented.

General Syntax

A slicing operation is applied to a container through the following syntax:

When firstIndexWanted is omitted it is assumed to be 0.

When firstIndexNotWanted is omitted it is assumed to be equals to len(container).

When step is omitted it is assumed to be 1.

List Slicing

Array Slicing

String Slicing

Differences between collection types

Consider the following example:

You must specify a parameter is a sliceable type when using it with methods:

the above code will result on compile time error The type 'System.Object' does not support splicing'. This is because the boo compiler cannot predict that you want to pass a collection to the method, and since the type Object is neither a collection or array, we got the error. To handle that, explicitly tell the compiler that you will pass a collection to the method:

  • No labels

1 Comment

  1. Why cant the "slice type" be inferred as the rest of the types are?  I think C++ can do this using template partial specialization. For example

    t.example([1, 2, 3, 4, 5])
    should generate a template instance which takes an IList for itens in the above (wiki) example

    t.example((1, 2, 3, 4, 5)) 
    should generate a template instance which takes Int32[]

    I think templates are not supported in boo as of now but adding a C++ template like mechanism will take boo's type inferencing mechanism to new places. IMHO adding such a system should be high on the list of priorities.