Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:101713 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 31659 invoked from network); 27 Jan 2018 14:14:14 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 27 Jan 2018 14:14:14 -0000 Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.54 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 74.125.82.54 mail-wm0-f54.google.com Received: from [74.125.82.54] ([74.125.82.54:54025] helo=mail-wm0-f54.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id EA/A0-24062-4398C6A5 for ; Sat, 27 Jan 2018 09:14:12 -0500 Received: by mail-wm0-f54.google.com with SMTP id t74so6260362wme.3 for ; Sat, 27 Jan 2018 06:14:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=LaamBlp4GRqTV3ByrgtVQK4Guh6EaikkgLNDP4bgBV4=; b=tMM81LhtCXW6f0pepYe0uuYki4U3JF6XEI/6HNDBY8hySINI9IbSnYClj7lgChRXsX wCLOf4eL9QU8IsAnLeh//uUl7JRFwBtYErRjgTVKj1n/zfrr/+CzJncFcRy/TiELxOdB FnbljtW0zH32W+ktiTPGLBUhdAIGG1I8SYcLFDkscwcz6Pqx9heM1IOq0ir0My5ZLfxZ jocCgyqUuKyRCiM/OusTyl/l7GNwfVdr9XmQHd2+jhssa39GZMgEDgTx7wYD46d6rZaC vUsgdtzhpkWBMlVg4+wLIA4rcCI7u17BIGmmstfKgTBsLfZAp5X5aUcuwZY1ijEMM/Ho 2E0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=LaamBlp4GRqTV3ByrgtVQK4Guh6EaikkgLNDP4bgBV4=; b=YwwRdVfnw9zNTepuALLe3PVROT2YRAFgPKcbm4FVP99Bg6xl7wOn+ffYmdm9CaxBsf L6rcjYlloJ1AArBUNDDR3vdO90OX8MQtUbPWJnGr7w2ASW/c/6tWt3Ehan/BlWXfL/ux sZDVsNnv0N13+8S2KGbJjMXCQFDdggcy6Qwi8LWHkmS5c08NKNuuS0N7isoZIfxFKoQr bRrhq1aaClgIMsaqs8RU3VxCJfK5xBuQTqOTd5iK76Tx2gJVzbR1yt9Z7VtpTczdDn2B f9WSpLekga3BZloSkkzcgGuKWuS+1fwGOB2jY2T9wc8nWYQDzXaz2+G6SKPB782hXvsm oD0A== X-Gm-Message-State: AKwxytdzHNgytN/7KcKYyvDiZd/ymy0IXbYrA+QDF1kOYs/ZIUYWZJht rCaiu1DA2WzdiAHjZrmsxUXLUA== X-Google-Smtp-Source: AH8x224ZKjt5krVyFvwJH6OMLpWLA+BKnCjXbNFwR7ly4tE8AXZYuRxw/n8iEJPVn4f0tJFl0Y4WgQ== X-Received: by 10.80.148.217 with SMTP id t25mr39335929eda.121.1517062449323; Sat, 27 Jan 2018 06:14:09 -0800 (PST) Received: from [192.168.1.253] (host86-173-114-42.range86-173.btcentralplus.com. [86.173.114.42]) by smtp.googlemail.com with ESMTPSA id b46sm4070598edd.73.2018.01.27.06.14.08 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 27 Jan 2018 06:14:08 -0800 (PST) To: internals@lists.php.net References: <517F0F7B-CD01-4F35-9021-8336B7BA06E1@cschneid.com> Message-ID: <4d699e5d-3fa9-8de3-ba97-26d1460be7fc@gmail.com> Date: Sat, 27 Jan 2018 14:14:04 +0000 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.5.2 MIME-Version: 1.0 In-Reply-To: <517F0F7B-CD01-4F35-9021-8336B7BA06E1@cschneid.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-GB Subject: Re: [PHP-DEV] Shorthand proposal for associative arrays From: rowan.collins@gmail.com (Rowan Collins) Hi! On 26/01/2018 18:16, Christian Schneider wrote: > Hi there, > I have a proposal for a shorthand notation of associative arrays borrowed from another language: > :$foo > would be equivalent to > 'foo' => $foo > and would work with array, list or [] I'm always a little suspicious of features that give meaning to the name of a variable, because I consider variable names to be an important part of the readability of code, and the encapsulation of an implementation. As such, I want to be able to rename local variables as part of tweaking how a particular function is written. For instance, I'm not in favour of introducing named parameters based on existing function signatures, because it makes names which were previously local into part of the function's public signature. While this particular proposal doesn't prevent refactoring in the way named parameters would, it adds one more thing to trip up on: when renaming $foo to $bar, [:$foo] needs to be changed to ['foo' => $bar]. > 1) Emulating named parameters with associative arrays like > html::img([ 'src' => $src, 'alt' => $alt ]); > could be written as > html::img([ :$src, :$alt ]); > which encourages consistent naming of variables and parameters This smells of bad code to me: it either means your functions are very closely coupled, and the variables have exactly the same meaning in both scopes; or that one or other name is poorly chosen, and doesn't reflect its meaning in the scope where it's used. In this example, $alt would probably have come from some other string, which could more appropriately be labelled $productName, or $bookTitle, or whatever the image is showing. If the values are constructed just for this function call, I would prefer to read:     html::img([          'src' => $imageDomain . $coverImagePath,          'alt' => "$bookTitle by $authorName"     ]); as opposed to:    $src = $coverImagePath . $coverImagePath;    $alt = "$bookTitle by $authorName";    html::img([ :$src, :$alt ]); > 2) Simplifying list destructuring with non-integer keys, example taking from http://php.net/manual/en/migration71.new-features.php#migration71.new-features.support-for-keys-in-list > foreach ($data as ["id" => $id, "name" => $name]) { > becomes > foreach ($data as [ :$id, :$name ]) { > which reduces redundancy. This example feels a bit more compelling, but again it assumes that you want the local name to match the array key, rather than having a name appropriate to the current task. For instance, you might well be dealing with other things which have IDs and names, and want to differentiate:    foreach( $bookList as ["id" => $bookId, "name" => $bookName ] ) { I'm not dead against this proposal, but it's not one I'd be using often myself, and personally I think it would lead to more bad code than good. Regards, -- Rowan Collins [IMSoP]