Liste des Groupes | Revenir à c arch |
On 2024-09-05 10:54, David Brown wrote:That seems like a safe choice. C's implicit promotion of int to long int can be convenient, but convenience is sometimes at odds with safety.On 05/09/2024 02:56, MitchAlsup1 wrote:The Ada language can work in both ways. If you just have:On Thu, 5 Sep 2024 0:41:36 +0000, BGB wrote:>
>On 9/4/2024 3:59 PM, Scott Lurndal wrote:>
>
Say:
long z;
int x, y;
...
z=x*y;
Would auto-promote to long before the multiply.
\I may have to use this as an example of C allowing the programmer
to shoot himself in the foot; promotion or no promotion.
You snipped rather unfortunately here - it makes it look like this was code that Scott wrote, and you've removed essential context by BGB.
>
>
While I agree it is an example of the kind of code that people sometimes write when they don't understand C arithmetic, I don't think it is C-specific. I can't think of any language off-hand where expressions are evaluated differently depending on types used further out in the expression. Can you give any examples of languages where the equivalent code would either do the multiplication as "long", or give an error so that the programmer would be informed of their error?
z : Long_Integer; -- Not a standard Ada type, but often provided.
x, y : Integer;
...
z := x * y;
the compiler will inform you that the types in the assignment do not match: using the standard (predefined) operator "*", the product of two Integers gives an Integer, not a Long_Integer.
If you add this definition to the code:You can make types in C++ that have this effect, but you have to make them and use them consistently. You can't overload operators on standard types like that.
function "*" (Left, Right : Integer) return Long_Integer
is (Long_Integer(Left) * Long_Integer(Right));
the compiler sees that there is now /also/ an Integer * Integer => Long_Integer multiplication operator, and uses that. Function overloading in Ada can depend on the type expected of the result.
Perhaps you asked for a language that worked like this "out of the box", without the programmer having to add things like the "*" function above, and then Ada would not qualify on the second alternative (automatic lengthening before multiplication, depending on the result type desired).I asked for either, and you gave me both :-)
(I don't count personal one-person languages here.While Ada has low market penetration, I don't think it quite qualifies as a one-person language -- yet :-)
Les messages affichés proviennent d'usenet.