Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!.POSTED!not-for-mail From: Henderson Newsgroups: comp.lang.java.programmer Subject: Re: Arithmetic overflow checking Date: Sat, 23 Jul 2011 22:55:00 -0400 Organization: e1 Lines: 45 Message-ID: References: <015aeb15-57db-48ab-9cd4-77f8448b632f@w24g2000yqw.googlegroups.com> <09fe171s46ilvq9qmn254dctunm6noh0ps@4ax.com> <4e262731$0$314$14726298@news.sunsite.dk> <4e26300b$0$309$14726298@news.sunsite.dk> <4e26b4ed$0$2501$db0fefd9@news.zen.co.uk> <4e28097f$0$2533$da0feed9@news.zen.co.uk> <7a23c9d2-508f-4dbd-af91-8cdf2a9764e1@p29g2000pre.googlegroups.com> NNTP-Posting-Host: cJ+uP0NdDAbt8UCZbZVtiQ.user.speranza.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: WinVN 0.99.12z (x86 32bit) X-Notice: Filtered by postfilter v. 0.8.2 Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:6495 On 23/07/2011 5:03 PM, Andreas Leitgeb wrote: > It is my opinion, that operator overloading at least for a certain > limited set of JSL classes (including BigDecimal and BigInteger as > well as some new Complex class in addition to String's +), would > be beneficial. Ditto for a new "strictint" keyword to enable > detection and handling of integer overflows. An obvious suggestion would be to allow it on all java.lang.Number subclasses, but a + b among these is only defined if a is a Number class that has an overload of .plus(x) that is applicable to an argument of a supertype of b, and the most specific such is chosen; and if a is a primitive, is treated as b + a; a * b analogously; a - b with a primitive uses (b - a).negate() and expects the return type of minus to support negate(); and for division we likewise need .reciprocal(). We could go further and rewrite Number.java so that it is Number> and defines: public T plus (? super T) public T plus (int) public T plus (long) public T plus (float) public T plus (double) public T minus (? super T) public T minus (int) ... public T times (? super T) ... public T dividedBy (? super T) ... public T negate () public T reciprocal () or something of the sort. I think short overloads can be left out as they can just auto-promote to int and use the int version and the int version won't be less efficient, unlike (on 32 bit hardware) the long version. The operator expressions then become shorthands for the method calls above, and generate compile time type errors if one is needed and missing, e.g. MyBigInteger.plus(BigInteger) is not defined but MyBigInteger.plus(MyBigInteger) is and one mixes the two types in a + expression. (Integer etc. needn't be explicitly supported as autounboxing can be used to call the primitive plus (int) or whatever if necessary.)