Skip to content

Add docs on how to write a function for both owned arrays and views #1212

Open

Description

@dstansby

I'm currently trying to write a function that can take either an owned array or a view. I'm currently relying on this stackoverflow post to do something like:

use ndarray::{ArrayBase, RawData, s, array, Ix1};

fn do_something_with_array<S>(a: &ArrayBase<S, Ix1>) where S: RawData{

}

fn main () {
    let a = array![1, 2, 3];
    do_something_with_array(&a);

    let slc = a.slice(s![1..3]);
    do_something_with_array(&slc);
}

I'm not sure if this is the best way though (certainly having to include a type parameter in the function definition seems clunky), so it would be good to get clarification and add a section to the docs explaining how to write these functions.

Activity

  1. added
    docsIssues relating primarily to documentation or API visibility.
    on May 26, 2023
  2. nilgoyette commented on May 26, 2023

    @nilgoyette
    Collaborator

    I had to write several generic functions with ndarray and it was really unclear at first! I agree that we should add more documentation.

    having to include a type parameter in the function definition seems clunky

    There's no way to avoid the type parameter. In Rust, when you don't know the exact type, you must be generic over a trait. This is exactly what's going on here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsIssues relating primarily to documentation or API visibility.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions