Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:105210 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 14609 invoked from network); 10 Apr 2019 18:19:17 -0000 Received: from unknown (HELO mail-lf1-f51.google.com) (209.85.167.51) by pb1.pair.com with SMTP; 10 Apr 2019 18:19:17 -0000 Received: by mail-lf1-f51.google.com with SMTP id u17so2139698lfi.3 for ; Wed, 10 Apr 2019 08:16:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=8+Yf2UlvgKBQorLwJkJQ9sxhNv3men19v12SVQqLf2E=; b=qoD2yDKCoL83th30puMeeUUs7lm+n7VKzroHrn4cG8cD+B7FVvvEBuwviP5pcdISy6 Dc5bWfvGJ7RfIT0m3TyQoEaeDgXRIaoyaMpQF/DUYtpTh00KKAzo5KCEXLVPxESsjisw AYgE+VhD9brM2yoFnwbecPLS0G2tQQAJWqnbqP8GgdItlo7xVnWwfN+vpHvip+BCwpwT dp6vNGVF/va+Y5sdEGz6pbAl2kgpsUdJ5zs4OW5TUGKshxD9qOuDRH5DIaMcG76BHx0l /kERrfl2Bo5dP2nNlPwenLgqkZgYJJMm5e2LjqNyadOUb179v+SHzK78FkR80YFpb9Jg 5pwQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=8+Yf2UlvgKBQorLwJkJQ9sxhNv3men19v12SVQqLf2E=; b=jumOwwh3o7O7Z4gdE/4giJAlaD7GIBJJjA/fRaQZgslxbhmoDhrOHOIe09bwCEt1Ti Vcq8rZHKDGUmPtZwoDYe7+afEiDdWcGbpcp0hfzQmveB1pl82n3Nwg6nKa2IvGygB8XL qWYYhVkFCao0VrFV9P4v+WZB0TBvowaajxnIMbc0/ISU3CDm7iud1U89+bWwRU+xbKNQ HT1TtQ+wzVJ7gjBudIk7LWqs14iz0oPAxwg3WnRmF5x0Kf87OZRA2BWRMbK6lh7xGgMU xETajPImbJtV7fpDTIpiCZpYCGafR3fzFeSyNzs0orIHYSVDWrWjdPZLyS9ieRJT4yi1 bhnQ== X-Gm-Message-State: APjAAAU9mHm/qpZAyIUkYIf+97EA6ztTCQrwMLfDPhAQOUdKp9OSPsCo YJ00eBkYcMWCNFX6c4TuMWsF7KaenhoQa4Bbfg== X-Google-Smtp-Source: APXvYqxtQ8gpGcaDzGvPV3ZYPYQHHiPpKgTxCoBgtu0lpDGABx18o2OGtgIB77retdDbMlg5rPUTwEFh9LOAgImrIPg= X-Received: by 2002:ac2:5501:: with SMTP id j1mr22838797lfk.113.1554909386428; Wed, 10 Apr 2019 08:16:26 -0700 (PDT) MIME-Version: 1.0 References: <5cadfed8.1c69fb81.31f7d.1c49SMTPIN_ADDED_MISSING@mx.google.com> In-Reply-To: <5cadfed8.1c69fb81.31f7d.1c49SMTPIN_ADDED_MISSING@mx.google.com> Date: Wed, 10 Apr 2019 17:16:13 +0200 Message-ID: To: Mark Randall Cc: internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] [RFC] Nullable Casting From: guilliam.xavier@gmail.com (Guilliam Xavier) On Wed, Apr 10, 2019 at 4:34 PM Mark Randall wrote: > > (Continuing from my previous post in a different thread...) > > IMHO a cast such as ([?]type) would be better described as passing a > mixed value through a function which has a return type of whatever is in > the cast... the mechanics of that function determine what is returned, > so long as it conforms to the cast type. > > A cast should always either return a value that would be accepted by a > function argument of an identical type to the cast, or it should throw > an exception. > > function x(T $value) { ... } > x(T) - Always OK > x((T)$var) - Always OK > x($var) - Function may throw a TypeError if it cannot be converted in a > very strict manner. > > To my mind, (?int) is quite explicit, in that it returns an integer or > null, and it is quite logical to expect it to return null in lieu of > throwing a TypeError if a conversion is not possible. Naturally, > (?int)null would return null. > > I see a lot of potential use in (?int)$value ?? $default. > > Equally I see a lot of use in the following to nullify unexpected inputs > such as if $_GET['something'] was an array: > > $input = (?string)$_GET['input'] ?? ''; So, for example: ```php $obj = (object) ["foo" => 42]; (function (int $x) { var_dump($x); })($obj); // TypeError (function (?int $x) { var_dump($x); })($obj); // TypeError var_dump((function ($x): int { return $x; })($obj)); // TypeError var_dump((function ($x): ?int { return $x; })($obj)); // TypeError var_dump((int) $obj); // Notice, then prints `int(1)` var_dump((?int) $obj); // would you want it to print `NULL`? (function (string $x) { var_dump($x); })($obj); // TypeError (function (?string $x) { var_dump($x); })($obj); // TypeError var_dump((function ($x): string { return $x; })($obj)); // TypeError var_dump((function ($x): ?string { return $x; })($obj)); // TypeError var_dump((string) $obj); // Recoverable fatal error var_dump((?string) $obj); // would you want it to print `NULL`? ``` I have no clear answer myself (other than the current proposal)... -- Guilliam Xavier