Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:83107 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 18247 invoked from network); 18 Feb 2015 19:25:08 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 18 Feb 2015 19:25:08 -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.45 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 74.125.82.45 mail-wg0-f45.google.com Received: from [74.125.82.45] ([74.125.82.45:50925] helo=mail-wg0-f45.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id C5/45-25021-117E4E45 for ; Wed, 18 Feb 2015 14:25:08 -0500 Received: by mail-wg0-f45.google.com with SMTP id k14so3155123wgh.4 for ; Wed, 18 Feb 2015 11:25:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=I0lzvtwxnariPf3rhMomHprtmiSQy1IDI7CTZwiNb/I=; b=imzuTuqnBDXA4T9EwjxaY0pK8FMFv3QRPZNOpKfClbSxz+54AI9Wj6mye5z1Shv+Ja orOq0AhaVmTYbCTjO8lvhcHSEs4tA/32qJx4fSRB4HidjwCpANsrhVMNYiGpKhQU4QWz Y0AYMjVAN/r38YFmcwHlITjuZ0bkFGEMNJQ9svBbiylHo2PhirsKrHdy5Ysi4I9Q1ZCn QJFmzuJFliqS5LGEVdcokO6/tgpApDQWycdu+OJXU5UHtrqTeENn0jnLKkBkx7ra7JvV HYW2xvhAq6UhzRH5n3Uga1/buKEflAg9ic0DMYp1ogKvg8HAFZk7sU/41URk/WOI6tsk nytA== X-Received: by 10.194.192.4 with SMTP id hc4mr1504550wjc.59.1424287501349; Wed, 18 Feb 2015 11:25:01 -0800 (PST) Received: from [192.168.0.172] ([62.189.198.114]) by mx.google.com with ESMTPSA id fo15sm30799339wic.19.2015.02.18.11.25.00 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 18 Feb 2015 11:25:00 -0800 (PST) Message-ID: <54E4E6FE.8060904@gmail.com> Date: Wed, 18 Feb 2015 19:24:46 +0000 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.4.0 MIME-Version: 1.0 To: francois@php.net, internals@lists.php.net References: <032f01d04b92$2722e0d0$7568a270$@php.net> <54E4BEDA.70009@gmail.com> <036201d04ba5$6e09d6d0$4a1d8470$@php.net> In-Reply-To: <036201d04ba5$6e09d6d0$4a1d8470$@php.net> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Subject: Re: [PHP-DEV] Scalar Type Hints v0.4 From: rowan.collins@gmail.com (Rowan Collins) François Laupretre wrote on 18/02/2015 18:05: >> De : Rowan Collins [mailto:rowan.collins@gmail.com] >> >> What if we defined the types as names in the \PHP namespace, but defined >> a slightly different algorithm for resolving typehints vs other uses: >> >> function foo(\PHP\types\numeric $a) // unambiguous but unwieldy >> function foo(\My\Namespace\numeric $a) // unambiguously *not* a built-in >> type >> function foo(numeric $a) // ambiguous, resolved at compile time >> >> The name would be resolved as follows: >> >> Given a typehint $type: >> 1) If $type begins with '\PHP\types\', interpret it it as an internal type. >> 2) ElseIf $type contains '\', interpret it as a class name, and proceed >> with normal class resolution at runtime. >> 3) ElseIf \PHP\types\$type is the name of a built-in type, interpret it >> as that internal type. >> 4) Else, interpret it as a class name, and proceed with normal class >> resolution at runtime. >> >> Basically, this means you can do the following: >> >> class String {} >> function accept_scalar_string(string $string) { ... } >> function accept_string_object(\String $string) { ... } >> >> >> The nice thing about this is that if we ever allow users to define >> "basic" types - copy-on-write structs, range types, enums, etc - they >> could "extend" these built-in types. >> >> Does that make sense to anyone, or am I over-complicating things? > Well, that's a solution, at least theoretically speaking. > > Frankly, I find it unreadable and I don't see reserving \PHP\types as a clean solution. IMO, adding semi-virtual namespaces would be mostly confusing and would just hide the initial name clash issue. > > There are a lot of other ways to extend type hinting to user-defined types. Maybe we will reserve namespaces for this case. In your design, where would these user types go ? in \PHP\usertype\ ? or would they share \PHP\types with built-in types (which recreates the same issue) ? They'd use whatever (non-reserved) namespace the implementer wanted. e.g. namespace Symfony\Component\TypeChecking; basicType nonNegativeInt extends PHP\types\int { public function isValid(int $value) { return $value >= 0; } } basicType PositiveInt extends nonNegativeInt { public function isValid(nonNegativeInt $value) { return $value != 0; } } Obviously the format of the actual definition is made up off the top of my head, but it shows how the namespacing would work. There's no need to reserve a namespace for the user-defined types, because it's no worse a burden to say "you can't name both a type and a class Foo\Bar" than to say "you can't name two different classes Foo\Bar". Regards, -- Rowan Collins [IMSoP]