Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:88286 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 39899 invoked from network); 17 Sep 2015 14:52:51 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 17 Sep 2015 14:52:51 -0000 Authentication-Results: pb1.pair.com header.from=ircmaxell@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=ircmaxell@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.212.181 as permitted sender) X-PHP-List-Original-Sender: ircmaxell@gmail.com X-Host-Fingerprint: 209.85.212.181 mail-wi0-f181.google.com Received: from [209.85.212.181] ([209.85.212.181:35633] helo=mail-wi0-f181.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 95/E5-33947-1C3DAF55 for ; Thu, 17 Sep 2015 10:52:50 -0400 Received: by wicge5 with SMTP id ge5so122409967wic.0 for ; Thu, 17 Sep 2015 07:52:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=UIN0xXhNGs/aWEbJiraAGEPMSg5QqlF3bJkDGI6HFwU=; b=vPZU/c0bjS1sHhP6lI7xm86b1uuTYwcEyvPVr+a2TD7skl4YbJCEvOGqVYLWjagZ7h cmjayt4hV/siz/nQpmYxiJAqNtIwd5+IalZvRWJY8NHE2V7Vp/1pst5lrMbCBOz18QJs 1Tx71To/I48yEkvv+AIXhXUgiefj5u3ujIsiGDzPnBfabIc9rciwXNFEUf0lKtSkpmhs A9j/E3udGBvWemRt8LZCB+L3qW52Vud8QjtKn4Q7XDJIQNq2y+uBm2Mf9RvCfgywTv/G obRVMWcBJdQqN9xNEBCyAcjaADoMynYdDspn6mHBbqmWVhTRZwnX8MZq414P4mPpUKLR AiIA== MIME-Version: 1.0 X-Received: by 10.180.91.131 with SMTP id ce3mr31437422wib.84.1442501567156; Thu, 17 Sep 2015 07:52:47 -0700 (PDT) Received: by 10.28.55.18 with HTTP; Thu, 17 Sep 2015 07:52:47 -0700 (PDT) In-Reply-To: References: Date: Thu, 17 Sep 2015 10:52:47 -0400 Message-ID: To: Yasuo Ohgaki Cc: "internals@lists.php.net" Content-Type: text/plain; charset=UTF-8 Subject: Re: [PHP-DEV] Make strict mode more strict? From: ircmaxell@gmail.com (Anthony Ferrara) Yasuo, On Wed, Sep 16, 2015 at 6:10 PM, Yasuo Ohgaki wrote: > Hi all, > > PHP 7 has strict_types mode for function parameters/return values and > these are binded to certain type strictly. > https://wiki.php.net/rfc/scalar_type_hints_v5 > > Why not make strict_types mode more strict? > The idea is as follows: > > declare(strict_types=1); > > function foo(int &$i) { > $i = "string"; // Raise error > $i = function_returns_string(); // Raise error > $i = 1234.5678; // Raise error > $i = function_returns_float(); // Raise error > > $n = 123; > // do something > $n = array(); // Raise error > } > ?> > > Assigning different type to already initialized variable is a bug most > likely. There may be cases that variable should have several types, > e.g. return INT for success and FALSE for failure, but programmers can > use different variable or return value directly or make BOOL/NULL > exception. > > This is useful with reference especially. For example, > > declare(strict_types=1); > > function foo(int &$i) { > $i = 'string'; > } > > $i = 0; > foo($i); > var_dump($i); > ?> > > outputs > > string(6) "string" > > > > Just an idea. Any comments? > First, as mentioned by others, this really doesn't have overly much to do with strict_types. It's introducing a new concept (which could potentially be a new declare statement). Second, i think the biggest limitation here is technological. If you can find a way internally to type variables properly while keeping copy-on-write semantics, then we can start that conversation on whether we really want to do that or not. But so far, everyone I've seen try to do something like that (namely for typed properties) has wound up causing all sorts of weird edge-cases around references or copy-on-write. Which is one reason we haven't seen a full proposal yet for it. So if you can come up with a patch that works, let's definitely talk about it. But as Stas said, it's really not just making types more strict, but it's fully blown variable typing. Which we can talk about, but let's talk about explicitly (and preferably with an implementation approach). Thanks!!! Anthony