Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:98755 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 33906 invoked from network); 10 Apr 2017 08:18:13 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 10 Apr 2017 08:18:13 -0000 Authentication-Results: pb1.pair.com header.from=rasmus@mindplay.dk; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=rasmus@mindplay.dk; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain mindplay.dk from 209.85.192.178 cause and error) X-PHP-List-Original-Sender: rasmus@mindplay.dk X-Host-Fingerprint: 209.85.192.178 mail-pf0-f178.google.com Received: from [209.85.192.178] ([209.85.192.178:35283] helo=mail-pf0-f178.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 75/51-22499-3CF3BE85 for ; Mon, 10 Apr 2017 04:18:12 -0400 Received: by mail-pf0-f178.google.com with SMTP id i5so29886158pfc.2 for ; Mon, 10 Apr 2017 01:18:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mindplay-dk.20150623.gappssmtp.com; s=20150623; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=TV2SIgF8dNAP6Ek9OnuFU5YIoFszfViZoHyyVrQGU7M=; b=e0THuKlaa3YcnezpJ+m5bmhoHoX/OGwEBb1S+9+u6LrLtaLPq3plwWtxlV9WitJzCO PAYMZuey4aCqf1onMdxp7fxrv5LrsgS8sCAQLpO/joR5A7gJhGrvRwv0+QU3Z6zUtH1A 6KHu2Nib/4u6OiCM16kjfVWgxDlF2+wTGSy8iD+/vLEv53Ihc0BK5sO29SmzZsAKfxBr Zq/PNEUAphhTUQ5GCHBUZXUfJ4jW9IspZ9vJn+kkzCuiJxKqU640LAyATpHxHPpMkso2 eHW5Qs2sJltmcZ8Yxra1Yi13kKa0RyZUCa1hBKnQJpjPx7MiKqBv7QT6SsaskiuCU+LC bYpQ== 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:cc; bh=TV2SIgF8dNAP6Ek9OnuFU5YIoFszfViZoHyyVrQGU7M=; b=O+peeCIBPU+8JMp1z9FlR+mG81Ur6uNsimefY54IWm8BxbKdRS8qFzDX6FHFk3IflP EX4epKBVQgqaSyRbRna8m0EdokKIKpWdHV8gXBS8u242Fb4XratJKlVYgGAfEulNyvex J0ezfgnx0PMM0qIPvmkGFxuE8IfFTuMH3YF0fEZ4wmrsrWCgoAl1VjNF35kglP/2gy8y i0Bg0f5fWrAQKArKgEU1aVNIzC+XrTjOBWqKoY95jtpQ5pFQDB9K8YgI6D5tSx+YVNqs qmVgfyXXrU55tBSPAgtjPUjS789jhs+mTSmssYeB1q6CC7YpRBdVBUUmKriXQXbjJ0Yg TpeA== X-Gm-Message-State: AN3rC/46lKyjG4SYewP6doanPE16GD1Rqalw/Pgf61PkmFPg+/5X7vMVQfPZnrtPVIZw6cLMJ3ihRU2/9rGGsA== X-Received: by 10.99.111.10 with SMTP id k10mr17666438pgc.213.1491812289144; Mon, 10 Apr 2017 01:18:09 -0700 (PDT) MIME-Version: 1.0 Received: by 10.100.145.150 with HTTP; Mon, 10 Apr 2017 01:18:08 -0700 (PDT) In-Reply-To: References: Date: Mon, 10 Apr 2017 10:18:08 +0200 Message-ID: To: Yasuo Ohgaki Cc: PHP internals Content-Type: multipart/alternative; boundary=94eb2c19d5ce910a15054ccb9dd3 Subject: Re: [PHP-DEV] scalar type-casting From: rasmus@mindplay.dk (Rasmus Schultz) --94eb2c19d5ce910a15054ccb9dd3 Content-Type: text/plain; charset=UTF-8 My concern is actually neither performance nor brevity - my concern is, can you read the code and actually understand what it does, can you write code without running into surprising side-effects, and so on. DbC might have merit in terms of performance, but perhaps not so much in a scripting language - if performance was critical to a given project, I would not be using a scripting language. The addition of DbC and marginally better performance for certain specific use-cases wouldn't change that for me. On Mon, Apr 10, 2017 at 4:04 AM, Yasuo Ohgaki wrote: > Hi Rasmus, > > Although DbC is not what you need, but DbC could solve your issue > more efficiently. i.e. Faster execution, not shorter code. > > https://wiki.php.net/rfc/dbc2 > > With DbC, caller has responsibility to pass correct parameters. > > On Sun, Apr 9, 2017 at 6:30 PM, Rasmus Schultz wrote: > >> >> $one = "1"; >> $one_int = (int) $one; >> add_one($one_int); >> > > > add_one(&$value) > require (is_int($value)) > { > $value += 1; > } > > // Caller has responsibility to pass correct parameters. > $one = filter_validate($_GET['var'], FILTER_VALIDATE_INT); > add_one($one); > > > > >> class Foo { public function __toString() { return "foo"; } } >> function append_to(string &$str) { $str .= "_bar"; } >> $foo = new Foo(); >> append_to($foo); >> var_dump($foo); // string(7) "foo_bar" > > > > class Foo { public function __toString() { return "foo"; } } > > function append_to(&$str) > require (is_string($str)) > { > $str .= "_bar"; > } > > $foo = new Foo(); > > // Caller has responsibility to pass correct parameters, but it's not > append_to($foo); // Error at DbC precondition check in append_foo() > var_dump($foo); // Cannot reach here in dev mode > > > > I really like parameter type check. > Problem is type check makes execution slower. > Another problem is type check is not enough for many codes. > > With DbC support, we can specify any expressions. Therefore, we can > check much more complex requirements for functions/methods at > development time. > > > > e.g. > function save_age($user_age) > require (is_int($user_age)) > require ($user_age >= 0) > require ($user_age < 150) > { > save_to_somewehre($user_age); > } > //Note: All input parameters must be validated to be correct value for the > app. e.g. use filter_validate()/etc > > > What you really need might be DbC. > > Regards, > > -- > Yasuo Ohgaki > yohgaki@ohgaki.net > > --94eb2c19d5ce910a15054ccb9dd3--