guy
guy

Reputation: 21

autocasting and type conversion in specman e

Consider the following example in e:

var a : int  = -3
var b : int  =  3
var c : uint = min(a,b); print c
    c = 3
var d : int  = min(a,b); print d
    d = -3

The arguments inside min() are autocasted to the type of the result expression.

My questions are:

  1. Are there other programming languages that use type autocasting, how do they treat functions like min() and max()?
  2. Is this behavior logical? I mean this definition is not consistent with the following possible definition of min:
    a < b ? a : b

thanks

Upvotes: 1

Views: 785

Answers (1)

mmachenry
mmachenry

Reputation: 1962

There are many languages that do type autocasting and many that will not. I should say upfront that I don't think it's a very good idea, and I prefer a more principled behavior in my language but some people prefer the convenience and lack of verbosity of autocasting.

Perl

Perl is an example of a language that does type autocasting and here's a really fun example that shows when this can be quite confusing.

print "bob" eq "bob";
print "nancy" eq "nancy";
print "bob" == "bob";
print "nancy" == "nancy";

The above program prints 1 (for true) three times, not four. Why not the forth? Well, "nancy" is not == "nancy". Why not? Because == is the numerical equality operator. eq is for string equality. The strings are being compared as you might thing in eq but in == they are being converted to number automatically. What number is "bob" equally to? Zero of course. Any string that doesn't have a number as its prefix is interpreted as zero. For this reason "bob" == "chris" is true. Why isn't "nancy" == "nancy"? Why isn't "nancy" zero? Because that is read as "nan" for "not a number". NaN is not equal to another NaN. I, personally, find this confusing.

Javascript

Javascript is another example of a language that will do type autocasting.

'5' - 3

What's the result of that expression? 2! Very good.

'5' + 3

What's the result of that one? 8? Wrong! It's '53'.

Confused? Good. Wow Javascript, Such Good Conventions, Much Sense. It's a series of really funny Javascript evaluations based on automatic casting of types.

Why would anyone do this!?

After seeing some carefully selected horror stories you might be asking yourself why people who are intelligent enough to invent two of the most popular programming languages in the entire world would do something so silly. I don't like type autocasting but to be fair, there is an argument for it. It's not purely a bug. Consider the following Haskell expression:

length [1,2,3,4] / 2

What does this equal? 2? 2.0? Nope! It doesn't compile due to a type error. length returns an Int and you can't divide that. You must explicitly cast the length to a fraction by calling fromIntegral in order for this to work.

fromIntegral (length [1,2,3,4]) / 2

That can definitely get quite annoying if you're doing a lot of math with integers and floats moving about in your program. But I would greatly prefer that to having to understand why nancy isn't equal to nancy but chris is, in fact, equal to bob.

Happy medium?

Scheme only have one number type. It automatically converts floats and fractions and ints happy and treats them just as you'd expect. It will allow you to divide the length of a list without explicit casting because it knows what you mean. But no, it will never secretly convert a string to a number. Strings aren't numbers. That's just silly. ;)

Your example

As for your example, it's hard to say it is or is not logical. Confusing, yes. I mean you're here on stack overflow aren't you? The reason you're getting 3 I think is either -3 is being interpreted, like Ross said, as a unit in 2's compliment and is a much higher number or because the result of the min is -3 and then it's getting turned into an unsigned int by dropping the negative. The problem is that you asked it for asked it to put the result into an unsigned int but the result is negative. So I would say that what it did is logical in the context of type autocasting, but that type autocasting is confusing. Presumably, you're being saved from having to do explicit type casting all over the place and paying for it with weird behavior like this on occasion.

Upvotes: 1

Related Questions