Skip to content

Add unsafeToArray to IArray #10730

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

mpilquist
Copy link
Contributor

This came up when integrating IArray with fs2, where I needed access to copyToArray. Rather than add copyToArray as an extension method to IArray, this PR provides an escape hatch to access the underlying mutable array without needing an explicit asInstanceOf.

@smarter
Copy link
Member

smarter commented Dec 10, 2020

Is the change to the dotty-cps-async submodule in this PR intentional?

@mpilquist mpilquist force-pushed the topic/unsafe-to-array branch from debfd87 to eda68bc Compare December 10, 2020 00:47
@mpilquist
Copy link
Contributor Author

@smarter No, thanks for spotting, fixed

@mpilquist mpilquist force-pushed the topic/unsafe-to-array branch from eda68bc to 9f82972 Compare December 10, 2020 01:00
@sjrd
Copy link
Member

sjrd commented Dec 10, 2020

I don't think this is a good idea. IMO if you "need" to cast IArray into Array, you should have to use asInstanceOf, so that you realize how dangerous it is.

IMO this direction of cast is significantly more hazardous than casting mutable array into an immutable one. In the latter situation, you already have ownership of the mutability of the array. You are choosing to give, most likely temporarily, a read only view of the array to someone who only needs to read. It's your responsibility not to mutate the array during that time, but that's more or less OK because you are the owner of the mutability.

In the scenario that this PR addresses, you are most definitely not owning the mutability of the array. You are granting yourself that capability although it was not yours to grant. This is qualitatively more unsafe, IMO.

I don't want to "judge" your use case. It may very well be that it's very important not to copy, and I trust that you know what you're doing, with great care. But that's what asInstanceOf is for: an escape hatch for great care needs.

@mpilquist
Copy link
Contributor Author

Closing in favor of #10743

@mpilquist mpilquist closed this Dec 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants