[swift-users] NSMutableData's init?(length length: Int) initializer

Jordan Rose jordan_rose at apple.com
Fri Apr 15 13:22:35 CDT 2016

> On Apr 15, 2016, at 10:36, Dmitri Gribenko via swift-users <swift-users at swift.org> wrote:
> On Fri, Apr 15, 2016 at 10:34 AM, Chris Lattner <clattner at apple.com <mailto:clattner at apple.com>> wrote:
>>> On Apr 14, 2016, at 11:22 PM, Dmitri Gribenko <gribozavr at gmail.com> wrote:
>>> On Thu, Apr 14, 2016 at 11:16 PM, Chris Lattner via swift-users
>>> <swift-users at swift.org> wrote:
>>>>> On Apr 14, 2016, at 10:45 AM, soyer via swift-users <swift-users at swift.org> wrote:
>>>>> Hello Guys, Girls,
>>>>> Do you know why is the init?(length length: Int) NSMutableData's initializer failable?
>>>>> The memory allocation can fail, but I think Swift doesn't handle that cases. (it is not a real issue in a modern OS)
>>>>> The code on github calls a non failable initializer.
>>>>> https://github.com/apple/swift-corelibs-foundation/blob/master/Foundation/NSData.swift#L904
>>>> Swift’s policy on memory allocation failure is that fixed-size object allocation is considered to be a runtime failure if it cannot be handled.  OTOH, APIs that can take a variable and arbitrarily large amount to allocate should be failable.  NSData falls into the later category.
>>> Does this principle apply to Array(repeating:count:)?
>>> Array.append(contentsOf:)?
>> As you know well enough, “no”. :-)
> Why?  These APIs also "take a variable and arbitrarily large amount to
> allocate".

I might split the difference here: trying to append a very large sequence would be a programmer error, but trying to allocate a very large array might not be.

UnsafeMutablePointer.init(allocatingCapacity:) is also currently non-failable.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160415/ad9f6103/attachment.html>

More information about the swift-users mailing list