Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:105348 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 23861 invoked from network); 23 Apr 2019 12:16:50 -0000 Received: from unknown (HELO mail-it1-f172.google.com) (209.85.166.172) by pb1.pair.com with SMTP; 23 Apr 2019 12:16:50 -0000 Received: by mail-it1-f172.google.com with SMTP id k64so22581481itb.5 for ; Tue, 23 Apr 2019 02:17:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=uTH3QXgmkzeuCpkkUZDJyyo0idZS2oxnYdzz8tpqf7U=; b=BqWUnlZtWNXwwaPycGuKJOZ3rCPBapen22Wo9tNymgnd0ZL7Wz7aPm9OyF79/yofP2 2M+3X5QiOal6mFE+jwXa8sUMVH1oXJLYiVjrHT4wOzW6OiXuo1DuCnu9Aoq8qx3KGEVh rHpaxwkNlVpVIgu98QRi+IRZ4GhPh1fKySWEJWI68YZkeajBW7xxxyqRznkLeb37sVPL dOfmxKjCDSDUEcEf1KxFm+Fml3F6tXBQPfQqf2XmB60g6eNrvcR0WR6zzUB/j3VgfCV9 tsZJEEn1v+KnFwS2+qxQAAZytmUwS511WGRvcjVAaKjVxGnZ083Xct6GGDoosL8RfCNu jFFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=uTH3QXgmkzeuCpkkUZDJyyo0idZS2oxnYdzz8tpqf7U=; b=InJbO9XryiRr5g8EbvOkgQihtfqDIoM8T+c7Pn4MJmsNfEMqs5pJOJaMK2z2DR1EOU Lp+NRYrGYtm/jGM0LC78VNmkdyWxdySIl9jN2YGpXzqTkRDi2QNk86O1IR/q7fdKGw4w FHUVmw7LI7+smpGKZ03P7FMevrTECt7rrOJgEhazBgeZr6+QvUcU35hf2LgFDnI4kDZ8 us/cZb9FEVfQbMcNYagVulRdc4EQg+xauvwrLtO/eMVhOEXJ9COVvhbWkfRoclXw7E/t Wog7o5q3IHc5CWZwBRxlccv8d+Kf5EN/6ztVId5YfWkP83fnmeI5LcuMA+m44lD1AFZr /TDw== X-Gm-Message-State: APjAAAVmRxStuhnNaXM2gw/xSq3f6frIxaZpODaZUb/irjK7Iad6iK+c OU1/6Wk7K4kj+L/0dqu0l0CETLfIGGWISWWDgs0= X-Google-Smtp-Source: APXvYqy/zm2tw6YPbTR3MEQGiUVk1aQVNFt5hY/OSeXGWYBoaCrup034WkPvlLswu3zgLhmYSoi8EBLcqej2I4Qt3rY= X-Received: by 2002:a02:2a8a:: with SMTP id w132mr4881090jaw.101.1556011031689; Tue, 23 Apr 2019 02:17:11 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 23 Apr 2019 11:16:55 +0200 Message-ID: To: Benjamin Morel Cc: PHP Internals Content-Type: multipart/alternative; boundary="000000000000cf860205872f0c4d" Subject: Re: [PHP-DEV] Object Type Casting Reloaded From: nikita.ppv@gmail.com (Nikita Popov) --000000000000cf860205872f0c4d Content-Type: text/plain; charset="UTF-8" On Mon, Apr 22, 2019 at 11:48 PM Benjamin Morel wrote: > Hi internals, > > I'd like to revive an old discussion > about > object type casting. > > The idea would be to allow (ClassName) casting: > > $service = (EmailService) $diContainer->get('email.service'); > > The above code would throw a TypeError if the value is not an instance of > the given class. I see the following advantages: > > - Type safety: we can be sure that the value is of the correct type or that > we'll get an Error. This syntax allows to fail early if the variable > happens to not be of the expected type, and avoids much more verbose > checks; > - Static analysis: IDEs and static code analysis tools can now understand > the type of the variable, without having to resort to `@var` annotations. > > These combine into a third advantage: readability. Today's equivalent of > the above one-liner could be: > > /** @var EmailService $service */ > $service = $diContainer->get('email.service'); > if (! $service instanceof EmailService) { > throw new TypeError('Expected instance of EmailService, ...'); > } > > Which is a lot of boilerplate code that could be easily avoided by > introducing this new syntax. > > Before moving forward and working on a formal RFC, I'd like to hear your > thoughts: what's your early feeling about this? Did I miss other > discussions around this subject? Are there any technical issues that come > to mind? Could this feature help the upcoming JIT compiler produce more > efficient machine code by knowing the type of the variable at compile time? > etc. > > Note: "casting" might not be the perfect name here as what we're really > doing is a type check, but this reuses the type casting syntax and > resembles Java's object casting. > > Thank you, > Ben > Without commenting on the rest of the proposal: It's not possible to use (ClassName) as a cast syntax, because it is ambiguous. For example (Foo) [$x] is already valid syntax (fetch constant Foo and take index $x), or (Foo) +$bar, etc. The only reason why (int) etc are okay is because we treat the whole (int) as a single token, something we can't do in general (because it would break foo(Foo)). Nikita --000000000000cf860205872f0c4d--