Req #80354 [Opn]: allow casting to void (no-op)

From: Date: Thu, 12 Nov 2020 15:30:58 +0000
Subject: Req #80354 [Opn]: allow casting to void (no-op)
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-230302@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=80354&edit=1 ID: 80354 User updated by: divinity76 at gmail dot com Reported by: divinity76 at gmail dot com Summary: allow casting to void (no-op) Status: Open Type: Feature/Change Request Package: Scripting Engine problem PHP Version: Next Minor Version Block user comment: N Private report: N New Comment: another usecase: when you want to ForEach() keys but don't care about the value: foreach($a as $key=>$unused){ (void)$unused; // ... } - sure some might argue that foreach(array_keys($a) as $key){} can be used instead.. but that comes at a performance penalty if nothing else Previous Comments: ------------------------------------------------------------------------ [2020-11-12 15:20:47] divinity76 at gmail dot com Description: ------------ i wish casting to void was legal, doing the same thing as casting to void in C: a no-op. (used to shut up linters about unused variables) there are some situations where you have to accept arguments that you don't use, or have to pass a variable by reference that you don't actually care about, and some linters may warn you about "this variable is declared but never used" or "this variable is written to but never read", for example CURLOPT_HEADERFUNCTION: the first argument is the curl handle and the 2nd argument is the header, some code only cares about the header, but have to accept the curl argument too just to match the CURLOPT_HEADERFUNCTION callback signature (in my experience, the handle argument is only useful when using the curl_multi api, and not useful when using the non-multi api) Test script: --------------- <?php $recieved_headers = []; $curlopts = array( CURLOPT_HEADERFUNCTION => function ($ch, string $header) use (&$recieved_headers): int { (void) $ch; $recieved_headers[] = $header; return strlen($header); } ); function f(&$val1, &$val2, &$val3) { // lets pretend this function writes to val1/val2/val3 } $data_i_need = null; $data_i_dont_need = null; f($data_i_need, $data_i_dont_need, $data_i_dont_need); (void)$data_i_dont_need; Expected result: ---------------- (blank) Actual result: -------------- Parse error: syntax error, unexpected '$ch' (T_VARIABLE) in /in/uWlu5 on line 5 Process exited with code 255. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=80354&edit=1

« previous php.bugs (#230302) next »