Skip to content

action upon failure of new() isn't specified #243

@gopherbot

Description

@gopherbot

by JQBalter:

The language specification doesn't say what happens if new() fails to allocate
memory. Here are three ways to fix this:

1) Make new() return nil upon failure. This will introduce a bug into every current
call of new() (except those that checked new() for nil for no documented reason).
It also requires a coding style that is verbose and known to be error-prone. And it
wouldn't work well if nonnullable pointer types were ever added to the language.

2) Make new() return res, ok. Calls that don't check the second return value could
result in a fatal error if new() fails. This avoids introducing a bug into every current
call, but turns local failures into global ones -- not a good design for systems
programming or servers. This approach also requires a verbose coding style in 
code that checks for allocation failure (which rarely happens, discouraging
programmers from checking).

3) Introduce exceptions into the language and issue an exception if new() fails.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions