Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:101459 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 4528 invoked from network); 30 Dec 2017 21:55:34 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 30 Dec 2017 21:55:34 -0000 Authentication-Results: pb1.pair.com smtp.mail=tendoaki@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=tendoaki@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.213.181 as permitted sender) X-PHP-List-Original-Sender: tendoaki@gmail.com X-Host-Fingerprint: 209.85.213.181 mail-yb0-f181.google.com Received: from [209.85.213.181] ([209.85.213.181:34466] helo=mail-yb0-f181.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id FB/F1-47595-35B084A5 for ; Sat, 30 Dec 2017 16:55:34 -0500 Received: by mail-yb0-f181.google.com with SMTP id a82so4524064ybg.1 for ; Sat, 30 Dec 2017 13:55:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=hjTKGsRipDY0hexBYQvuuf53scoKXptoMSsBpJJNi+I=; b=CEGKDxpYcZjskWz3T7nPxTSxgkrrRdiMFkh4bgUN5mVRKPyyqJTWVXl1USSYD5PCHb e25tjzTwgWwCw1WWhblCugGriZDRYnB/HPOTt3LCN80XQ0GflTP6SWKouKRQU/mO+3lO 4zMqioRNeqo96/rs/qHaJxUTRcPsiDYZwZnBjnN5IT3x18u/3AoC9LgMSLzqM2QwAHUm yLhY9ddq8Oa+AdGnGpF+gR/a8+qZl7/aNXhWNlArN97bg7wzEJ/jjTWS/HiZGyU/8JPH wlhkNgWwIAatFcUK43qFGD5al90RPZO+j/bbMPrfCjEERJkUxGk8RrA9Wug3HKGSqzt3 rToA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=hjTKGsRipDY0hexBYQvuuf53scoKXptoMSsBpJJNi+I=; b=KPmjfHC3u0opQ16j2QOKPvp+sdYCG5XEJoVKT/UkHv8ZcCsftK8Mxo9GzLzFT30tmk 7u17i2a+/sWHuO+a+zML0ibRh09uF8k+7zZdi+jkah7upC8MsJL1gi+vP28yf7hFqIPu eqY4pmX89IiQbANOMqdlRGzl3SjCXGfME1wcQMIucY6K9LxIl31wxGTulgeCTOBM+raf 8YTFSVnYgOtv67FoJ48IqGl1Z7FBLfhxcMi7YlUf9nnptF8crIMy6YvRqpampmPOrtPn gGl6qOBaZZuZpiv+YzIbMEM4Ey52lAVTULpIuU73SeYu7Ojqi7BkEHsAmu0AHXd9vRoO +BIQ== X-Gm-Message-State: AKGB3mLECueYy06gCJ9lggg02PIMLuTha7MuR6KdAfQAt54jB0OnKNyx tMeRDvBPxMNJLYVI/U/46cbPeQE6o1dGUapdiDE= X-Google-Smtp-Source: ACJfBosEU1/8OJFewqOpcRfFskiD/D1+lcuaCyYYzP/BRG6NNO8rCRZMFmlwkrSuw88xz5oDLZuKWdoEiPJX3b5pBg0= X-Received: by 10.37.146.131 with SMTP id y3mr27894682ybl.468.1514670929238; Sat, 30 Dec 2017 13:55:29 -0800 (PST) MIME-Version: 1.0 Received: by 10.37.201.7 with HTTP; Sat, 30 Dec 2017 13:55:28 -0800 (PST) In-Reply-To: <50caeb2d-62b6-167d-726e-e0e11ea201f3@lsces.co.uk> References: <72392123-d37b-26df-6886-218f48205f8a@fleshgrinder.com> <58A5ABDF-AA25-46D4-83E7-4DE72E3DFF5E@gmail.com> <757270790.33iDQ9MZ2V@vulcan> <4b55eed1-8656-ff70-e4e9-ad5e40213405@rhsoft.net> <73.EE.47595.179574A5@pb1.pair.com> <50caeb2d-62b6-167d-726e-e0e11ea201f3@lsces.co.uk> Date: Sat, 30 Dec 2017 16:55:28 -0500 Message-ID: To: PHP internals Content-Type: multipart/alternative; boundary="089e0832dea0b05e12056195ce57" Subject: Re: [PHP-DEV] [RFC] [DISCUSSION] Scalar Pseudo-type From: tendoaki@gmail.com (Michael Morris) --089e0832dea0b05e12056195ce57 Content-Type: text/plain; charset="UTF-8" On Sat, Dec 30, 2017 at 5:37 AM, Lester Caine wrote: > > Not being able to vote, many of us have no option to complain about the > way things are going. Currently there seems to be several styles of PHP > form the nice and simple untyped version I moved to from very strictly > typed hard compiled code I'd been using up until then, through to > current code which is reliant on third party things like PSR and > Composer and expects only strictly typed PHP. > > This is born of the fact that while ignoring datatype makes learning PHP easier, it makes using it harder - especially when testing. Mark me as against union types. First, it places the burden of getting the type right on the calling programmer. Stock PHP code doesn't do this and if popular 3rd party libraries start doing this too much it makes the language harder to learn. Second it blocks this proposal, which is a way to give people who want to control types the ability to do so in their own code without creating BC breaks in PHP or interoperability problems with other libraries. Doing this requires declaring variable types as an OPTION. The default type of a variable will be scalar. Under what I propose putting what is now a type hint on the function line will become a type declaration, and a casting operation will result if the wrong type is passed in. So function foo( int $a ) {} Obviously this path becomes unavailable to us with union types - and by comparison union types is an ineffective band-aid. The above is a shorthand for this function foo ( $arg ) { var int $a = $arg; } When a variable is declared this way it will autocast any assignment to the declared type. The only way to get around this is redeclare the type. Now some might be wondering, why not use this shorter statement: int $a = 3; The reason is this - I propose that syntax will lock the type down, but it won't autocast assignments - it iwll instead throw a TypeError if the assignment type doesn't match. Anyway, there is one BC break - see it? function foo( int $a ) { $a = 'hi'; } Currently $a becomes a string of 'hi'. Under this proposal $a remains an it and it is assigned the cast result of 'hi', which I believe is 0. Since the format is relatively new - PHP 7 - changing it in 8 won't be as disruptive as something that's been around a long time. Also, changing a var type deliberately after declaring it is a bad practice, further reducing the scope of code affected, but it's certainly not 0, so this is PHP 8.0 minimum. Now next, class instance creation. SomeClass $a = new SomeClass(); Doing this locks $a to SomeClass, and trying to assign it to something else will trip a type error. var SomeClass $a = new SomeClass(); Since there's no way to cast arbitrary arguments to SomeClass I'm not sure how this should be handled. Within classes.. class SomeClass { public $a = null; // Creates a scalar. public var int $b = 3; // Creates an autocasting int protected int $c = 5; // Creates an int. Errors will pitch on bad assign. protected OtherClass $b; } This syntax locks down, or *really* locks down types, but it's entirely opt-in. If we're going to go down the road of sticter types this has to be done. It's also an example of why pasting in functionality without an over-arching plan is a bad idea. If Union types is included part of this solution will be forever lost since it's likely incompatible with union types (or at least union types with scalars - union types with objects wouldn't be affected). --089e0832dea0b05e12056195ce57--