Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:92293 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 19903 invoked from network); 14 Apr 2016 10:18:32 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 14 Apr 2016 10:18:32 -0000 Authentication-Results: pb1.pair.com smtp.mail=me@daveyshafik.com; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=me@daveyshafik.com; sender-id=unknown Received-SPF: error (pb1.pair.com: domain daveyshafik.com from 209.85.215.46 cause and error) X-PHP-List-Original-Sender: me@daveyshafik.com X-Host-Fingerprint: 209.85.215.46 mail-lf0-f46.google.com Received: from [209.85.215.46] ([209.85.215.46:36135] helo=mail-lf0-f46.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 4D/F7-12455-67E6F075 for ; Thu, 14 Apr 2016 06:18:31 -0400 Received: by mail-lf0-f46.google.com with SMTP id g184so102986078lfb.3 for ; Thu, 14 Apr 2016 03:18:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daveyshafik-com.20150623.gappssmtp.com; s=20150623; h=mime-version:sender:in-reply-to:references:date:message-id:subject :from:to:cc; bh=3FEbcAIO0k/UqGPXkC8iBpz1mQaUV6AgXTM8mRVqwj4=; b=kLAv44EoDfrLXjWnr6KN1s2FYHQdZA2boT03mXurJF07gsmEixnDdfGPvAXkqJvIR4 Gah43LK1PLRUMOaEjL/y3ML9nsCZU96mhz3gCybexfE1dM6CucbVG8UY/I+Z/q6XTFCy oDHuj9bQF4k4kWvP0KytPeQZpFmduUSYjKf8Bf09k7u7iywTbt3gluqhdzTC2Zwz9tcB RF3oQmxR23EBBrHVtdq9iXBSMMn7VEnRUyoJ3g9qdJjKvgWR/JSAjKFLXVedBLGoLPlk Sqd7K04Z79sZV285MctH1iUE+T6Qaw5DPbRCHPVOwHPEsZKEQU9ElqJSf+teIjre1tbz hkiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:sender:in-reply-to:references:date :message-id:subject:from:to:cc; bh=3FEbcAIO0k/UqGPXkC8iBpz1mQaUV6AgXTM8mRVqwj4=; b=fcWuPqaQrfPuq2MFqPqFqHNy0MjTRTGTeaMby1I+lSnR0ObAkcIxmgUCy8T9BR/Enq ZFwwg4sBb6XbrQ9RRsybxblJIvMRp8tWhS9AeRgVulBcff4Mks6vs+066XQzMM01b2WI 7gnn285QXXyGwletlACi9oAgMnZKw3tmZ1azHzWakrqxqxbexEi9UjfS4en5XMIJ0Bpi UOo2oVIPf2twymAmtOCpxHx3BmWA4RcaXSiSLqw7ha4vT7RtbzQCUzBvi2XhgKKhVu7f 2i120HfubGDKhOf0HECXtSpv79MioVWAmplSSspbfIOIghTBZpWZIq2XD7NCEtZxIsyz SK0Q== X-Gm-Message-State: AOPr4FVwuqW5r4vyCc/cCvG9oZhG5uJhheCEZM+9UYocgE04Cpf5xaweVH86dJTdRq2uV4QvRZOJxbtfCinji58I MIME-Version: 1.0 X-Received: by 10.25.91.76 with SMTP id p73mr6265825lfb.67.1460629108139; Thu, 14 Apr 2016 03:18:28 -0700 (PDT) Sender: me@daveyshafik.com Received: by 10.25.125.196 with HTTP; Thu, 14 Apr 2016 03:18:28 -0700 (PDT) In-Reply-To: References: Date: Thu, 14 Apr 2016 03:18:28 -0700 X-Google-Sender-Auth: MdsYUtpOe3E6KifmzAjxbJ8TvQI Message-ID: To: Derick Rethans Cc: Lin Yo-An , Levi Morrison , internals Content-Type: multipart/alternative; boundary=001a1141153623c20e05306f3788 Subject: Re: [PHP-DEV] [RFC] Union Types From: davey@php.net (Davey Shafik) --001a1141153623c20e05306f3788 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable As mentioned in my nullable types comment, I think that we should NOT add a Null type unless the nullable types RFC fails to pass. We should not introduce both options, and I favor the nullable types over this for that purpose. With regards to Dericks comment on type conversion, I think either simply casting to the first type always, or we're going to have to come up with some potential complex rules. For example, what if you have: int|float, and pass in (string) "0.5", if those were separate types you'd get (int) 1, or (float) 0.5 respectively, and as a user I'd definitely expect it to be passed in as (float) 0.5 =E2= =80=94 maybe we cast to the least lossy type? So if you have int|float, then a numeric string always becomes a float. The flip-side is that if we just decide first-always then it's up to the developer to just define it as "float|int" instead. I think Derick has definitely hit upon the trickiest part of this. Also, just to clarify, if you combine this with the nullable types syntax (assuming prefix), then the nullable applies to the entire union type: ?int|float =3D=3D=3D int | float | null I do find the shorter syntax confusing TBH. Which actually leads me to a different thought: defining custom types and using them instead. I very much like Hacks type/newtype stuff, if we extended that to support union types: newtype Numeric =3D int|float; Then you end up with simply: ?Numeric as your type (it should be namespaced). You could further expand that with casting rules: newtype Numeric =3D int|float { string as float; bool as int; } or even go so far as: newtype Numeric =3D function($value): int|float { if ($value typeof int) { return $value; } return (float) $value; } FTR: I hate that last one, just spitballing. - Davey P.S. if someone is willing to tackle the code for custom types I'm willing to write an RFC On Thu, Apr 14, 2016 at 2:25 AM, Derick Rethans wrote: > On Thu, 14 Apr 2016, Lin Yo-An wrote: > > > On Thu, Apr 14, 2016 at 5:12 PM, Derick Rethans wrote: > > > > > I think what I am missing in the RFC is behaviour with scalar (weak) > > > typehints, and which type the variable in a class would be converted > to. > > > Take for example: > > > > > > function foo(int|bool $var) { echo get_type( $var ), "\n"; } > > > > > > foo(5); I guess int(5) > > > foo(false); I guess bool(false) > > > foo(0.5); It could be either int(1) or bool(true) > > > > > > And what if the hint would be "bool|int" ? > > > > I think type conversion shouldn't be done internally, implicitly. > > > > Implicit conversion leads more confusion in the language. when you > > pass variables, you have to remember these conversion rules. > > Sorry, we already have this with our weak scalar types. You can't make > union types not work with that. > > cheers, > Derick > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > --001a1141153623c20e05306f3788--