xp-framework/rfc

Cast and nullable types

thekid opened this issue · 2 comments

Scope of Change

This RFC suggests extending the core functionality cast to support casting to nullable types.

Rationale

Completing the type system

Functionality

cast($value, 'string')  // throws on NULL
cast($value, '?string') // returns NULL if NULL is passed in

Security considerations

None

Speed impact

Overhead of checking '?' === $str{0}

Dependencies

None, nullable types are currently ignored

Related documents

HHVM interestingly does not support this and yields Uncaught Error: syntax error, unexpected '?' in /home/thekid/cast.php:4 for (?string)$value.

The current function has a flag for this, which would be deprecated:

// {{{ proto var cast (var arg, var type[, bool nullsafe= true])
//     Casts an arg NULL-safe
function cast($arg, $type, $nullsafe= true) {
  // ...
}