Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:106856 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 49955 invoked from network); 4 Sep 2019 18:33:25 -0000 Received: from unknown (HELO out3-smtp.messagingengine.com) (66.111.4.27) by pb1.pair.com with SMTP; 4 Sep 2019 18:33:25 -0000 Received: from compute7.internal (compute7.nyi.internal [10.202.2.47]) by mailout.nyi.internal (Postfix) with ESMTP id A464321AFB for ; Wed, 4 Sep 2019 12:07:21 -0400 (EDT) Received: from imap26 ([10.202.2.76]) by compute7.internal (MEProxy); Wed, 04 Sep 2019 12:07:21 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm3; bh=a3G3f1 u71ODtbaWmp2wJiewoXECwq/EDcQdjNsEc29E=; b=RBMheCkwYfPf0XFHAM/z8A jL1h8IwNZmzrTsYyYqMLa5jE+qcsy3AiFfCB4aDf9SSglK03MxwFgTKv51bmBVvX eF1zu7Oz68T2YwYg4QcPlLXXoQWnjwULNK4ttQr1kwtMv/WZuC3WawhfIbxO5teO 0yYj/OWLlkPspUn7FfkIUMBua5C6uqSQ/Ht9pTJ/gcRD7DKPENtCqL4HP0XNoP+J yuljBHED59R19aZK1/MPTLA/YkGgU6fmdnXKl/lEhlfdU2IO4rYzfoMjX9e9y/iG tki0n9ivepyfpke6ttQgIVgU9FCFX9IMmJyvGKrlLpcDbCdHQCl6/gNoScl6cskQ == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduvddrudejhedgleegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepofgfggfkjghffffhvffutgesthdtredtreertdenucfhrhhomhepfdfnrghr rhihucfirghrfhhivghlugdfuceolhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtoh hmqeenucfrrghrrghmpehmrghilhhfrhhomheplhgrrhhrhiesghgrrhhfihgvlhguthgv tghhrdgtohhmnecuvehluhhsthgvrhfuihiivgeptd X-ME-Proxy: Received: by mailuser.nyi.internal (Postfix, from userid 501) id 0EA6614200A1; Wed, 4 Sep 2019 12:07:21 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.1.7-186-gf4cb3c3-fmstable-20190904v1 Mime-Version: 1.0 Message-ID: In-Reply-To: References: Date: Wed, 04 Sep 2019 11:06:58 -0500 To: "Steffen via internals" Content-Type: text/plain Subject: Re: [PHP-DEV] Silent Types From: larry@garfieldtech.com ("Larry Garfield") On Wed, Sep 4, 2019, at 2:21 AM, Lynn wrote: > Hi, > > Would this warrant a having mixed types? By making every variable of type > "mixed" when no type is defined, regardless of what value it receives or is > initialized with, this would make an opt-in system. This behavior would > cater developers who prefer only strict types, only dynamic types, and > those that want to type their code bit by bit without putting declares that > affect whole files. > > ``` > $foo = 'foo'; > // same as > mixed $oneHundred = 100; > // difference being: > // $foo is accepted when 'string' is typed in the signature > // $oneHundred is accepted when 'int' is typed in the signature > > // no longer mixed, only accepts their defined type as value > // and is accepted only as their defined type in signatures > string $foo = 'foo'; > int $oneHundred = 100; Yes, an implicit "mixed" type would be the only way it could be done, just like object properties that are not explicitly typed are implicitly "mixed". Whether there is a "mixed" type for explicit declaration is an open question; if it were added, it would IMO make sense to add for properties and parameters/returns, as well, for consistency. Whether or not that's wise in general I don't know. > // does imply we might need custom type definitions in the future to not > have to work with mixed > typedef Stringable = string | MyToStringInterface; > Stringable $foo = (bool) rand(0, 1) ? 'foo' : new MyStringableObject('foo'); > ``` There's a long list of reasons we want that functionality even before we get to variable types. :-) IIRC, it falls into the popular category of "this would be really useful but is harder than it sounds for complex implementation reasons, and a few people feel it's too much ceremony for a loose language." There's a number of features stuck in that bucket. > Note that this behavior would require making some decisions whether or not > in the future this opt-in behavior should change when a default value is > given, such as with C# and type inference when declaring a variable, based > on its assigned value. I could see arguments either way, but BC would probably dictate that types are not set implicitly. Otherwise, this currently valid code would break: $things = 'foo'; ... $things = ['foo', 'bar']; if (is_array($things)) { ... } else { ... } (Whether or not that's good practice is beside the point; there's ample code like that out there and breaking it when we don't have to is a no-no.) --Larry Garfield