Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:82710 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 82400 invoked from network); 14 Feb 2015 23:49:13 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 14 Feb 2015 23:49:13 -0000 Authentication-Results: pb1.pair.com smtp.mail=yohgaki@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=yohgaki@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.216.182 as permitted sender) X-PHP-List-Original-Sender: yohgaki@gmail.com X-Host-Fingerprint: 209.85.216.182 mail-qc0-f182.google.com Received: from [209.85.216.182] ([209.85.216.182:45223] helo=mail-qc0-f182.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 2E/C2-00421-7FEDFD45 for ; Sat, 14 Feb 2015 18:49:11 -0500 Received: by mail-qc0-f182.google.com with SMTP id r5so5776785qcx.13 for ; Sat, 14 Feb 2015 15:49:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc:content-type; bh=H4jbPPlrvtgfXSWiH97OJgcNGqbmkC+mSV1s/LcWs9U=; b=hpVzNX75IphHgAbN2P4949YyXQMTQafnczKYYkgCivX+Mz86NckXB6AZ96FAUFVYXm SY0tgYRyV/Ps9QCniToboSTWSLqqJQDPRkP2kU16uEOaCK47o5hTIcr/MSrUgFlgDcGR 6WKpY1KzPoBTPK+0l7wECv4RwCcZbV2Vd03b+mmVOdoUz4CkENM3SoJZgwNOsTXj4QSK oQ/bOwyEr87v4YWHUVmsaV7qYQs00tRVPxQlfXm5PFdA02SdcEqJK6OkNLz63MISYiDe DNWsT/bEIKxlgHXtQVV0Af9p8sV6aoDrNzmdkp6jasH9FHefAE3HCDLXidhXRSTkm30U jklw== X-Received: by 10.229.204.135 with SMTP id fm7mr42913528qcb.2.1423957748293; Sat, 14 Feb 2015 15:49:08 -0800 (PST) MIME-Version: 1.0 Sender: yohgaki@gmail.com Received: by 10.229.198.8 with HTTP; Sat, 14 Feb 2015 15:48:28 -0800 (PST) In-Reply-To: References: <54DAFD32.3000005@gmail.com> <54DB0BC0.20304@gmail.com> <54DBA801.8060403@gmail.com> <011e01d04802$cbd78ce0$6386a6a0$@php.net> <013801d0481d$d34c5170$79e4f450$@php.net> <002701d04846$50c0d630$f2428290$@tutteli.ch> Date: Sun, 15 Feb 2015 08:48:28 +0900 X-Google-Sender-Auth: yKbOkakm2UJ82JzNerKSqlo1wlM Message-ID: To: Robert Stoll Cc: francois , Dmitry Stogov , Joe Watkins , Stanislav Malyshev , PHP Internals Content-Type: multipart/alternative; boundary=001a11c1fd4cc327e0050f14fff2 Subject: Re: [PHP-DEV] Design by Contract From: yohgaki@ohgaki.net (Yasuo Ohgaki) --001a11c1fd4cc327e0050f14fff2 Content-Type: text/plain; charset=UTF-8 On Sun, Feb 15, 2015 at 8:17 AM, Yasuo Ohgaki wrote: > I wrote simple D program "sample.d" to play with. Install D, then > > dmd sample.d && ./sample > > ====sample.d==== > import std.stdio; > > class A { > int x = 1; > int y = 2; > > public: > void set (int x, int y) > in { > assert(x > 0); > assert(y > 0); > } > body { > this.x = x; > this.y = y; > } > int mul() > out (ret) { > assert(ret > 0); > } > body { > return x * y; > } > > invariant { > assert(this.x > 0); > assert(this.y > 0); > } > } > > class B : A { > int a; > int b; > override void set (int a, int b) > in { > assert(a > 4); > assert(b > 2); > } > body { > this.a = a; > this.b = b; > } > override int mul() > out (ret) { > assert(ret > 4); > } > body { > return a * b; > } > > invariant { > assert(this.x > 0); > assert(this.y > 0); > } > } > > class C : A { > override void set (int x, int y) > in { > // Cannot work with override > assert(x > -4); > assert(y > -2); > } > body { > this.x = x; > this.y = y; > } > override int mul() > out (ret) { > assert(ret > -4); > } > body { > return x * y; > } > > // This cannot work. Less restrictive than parent > invariant { > assert(this.x > -4); > assert(this.y > -2); > } > } > > > void main() > { > A a = new A; > B b = new B; > C c = new C; > a.set(4,2); > writeln("Ans: ", a.mul()); > // -1 is invalid > //a.set(-1, 0); core.exception.AssertError@sample.d(10): Assertion failure > > b.set(5,3); // Fine with B. B is different type. > writeln("Ans: ", b.mul()); > } > ================= > > > As you can see, D has type safety by contracts and does non-intuitive > checks. > Question is "Does PHP need this kind of type safety?" > > I totally agree strict type safety is good for writing correct programs, > BTW. > D has type safety feature since it has strong type. If PHP is going to be > strongly typed language as scalar type hint RFC, and might be stronger > type than now. We may consider stricter types later. It's not just time > for it. IMHO. > PHP may be extended to check contracts like D when > declare('strict_types'=1). > I used latest D rpm package for CentOS/Fedore x86_64 from http://dlang.org/download.html It seems invariant() is removed from latest D, but the package can use invariant(). You may get errors if you use real latest D. Regards, -- Yasuo Ohgaki yohgaki@ohgaki.net --001a11c1fd4cc327e0050f14fff2--