Skip to content

similar should accept a type as first parameter #17124

Closed

Description

The function similar accepts a container as first parameter, but not a container type, in the same way as e.g. eltype. (If dims is not specified, it should then construct an empty container.)

In some cases, for example in distributed computing, one wants to allocate a container to hold the result of a map-like calculation, without having access to the original container. However, the type of the original container is known, as well as the element type of the result. In this case, the interface offered by similar is insufficient -- it does not allow allocating a new container without creating an empty dummy container from the type.

A fall-back definition that could be added to Base is

similar{CT}(::Type{CT}, specs...) = similar(CT(), specs...)

although I assume that many container types will want to provide a more specialized version that avoid creating an empty container.

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

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions