xtensor-stack / xtensor-python

Python bindings for xtensor
BSD 3-Clause "New" or "Revised" License
348 stars 58 forks source link

Adding possibility to 'cast' or copy to `xt::xarray` etc #267

Closed tdegeus closed 3 years ago

tdegeus commented 3 years ago

Essentially this should be good to go. However, I had to introduce on additional check to check on rank for xtensor. In particular, I want to do this:

static auto get(pybind11::handle src)
{
    auto buf = pybind11::array_t<T, pybind11::array::c_style | pybind11::array::forcecast>::ensure(src);
    if (buf.ndim() != 2) {
        return false;
    }
    return buf;
}

however my compiler gets (rightfully) confused:

error: 'auto' in return type deduced as 'pybind11::array_t<int, 17>' here but deduced as 'bool' in earlier return statement
                return buf;
                ^

So I worked around with the extra check. I would like to get rid of it. But I don't know what I can return instead of false that allows me to later do

if (!buf) { 
    return false;
}

Alternatively on could try to get rid of the check statically. But this I don't know how to do easily.

tdegeus commented 3 years ago

Thanks @JohanMabille ! Good to go.