strong/weak typing and pointers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gabriel Zachmann

    #1

    strong/weak typing and pointers


    Is it correct to say that strong/weak typing does not make a difference
    if one does not use any pointers (or adress-taking operator)?

    More concretely, I am thinking particularly of Python vs C++.
    So, are there any examples (without pointers, references, or adress-taking),
    which would have a different result in Python and in C++?

    I would appreciate all insights or pointers to literature.

    TIA,
    gabriel.

    --
    /-------------------------------------------------------------------------\
    | We act as though comfort and luxury |
    | were the chief requirements of life, |
    | when all that we need to make us happy |
    | is something to be enthusiastic about. (Einstein) |
    +-------------------------------------------------------------------------+
    | zach@cs.uni-bonn.de __@/' www.gabrielzachmann.org |
    \-------------------------------------------------------------------------/
  • Diez B. Roggisch

    #2
    Re: strong/weak typing and pointers

    > Is it correct to say that strong/weak typing does not make a difference[color=blue]
    > if one does not use any pointers (or adress-taking operator)?[/color]

    It seems that you mistake strong/weak typing with static/dynamic typing - a
    completely different beast.

    Python is in fact strong typed - in opposition to php or perl or even C,
    this won't work:

    a = "1" + 2

    as "1" is a string and 2 an integer. And even though C is statically typed,
    it won't complain - you just end up with an unexpected result.

    And pointers are not evil in themselves - the are a neccessity to create
    recursive structures. But deliberately casting pointers can be very harmful
    - a reason why its forbidden in languages like java and AFAIK ada.
    [color=blue]
    > More concretely, I am thinking particularly of Python vs C++.
    > So, are there any examples (without pointers, references, or
    > adress-taking), which would have a different result in Python and in C++?[/color]

    I have difficulties to understand what you want here. Please elaborate.

    --
    Regards,

    Diez B. Roggisch

    Comment

    • JCM

      #3
      Re: strong/weak typing and pointers

      Gabriel Zachmann <zach@cs.uni-bonn.de> wrote:
      [color=blue]
      > Is it correct to say that strong/weak typing does not make a difference
      > if one does not use any pointers (or adress-taking operator)?[/color]

      You'll find a lack of consensus here on what's meant by "strong/weak
      typing". In Python there's no way to re-interpret the bits of a value
      as if they were a different type. For example, code like this is
      impossible in Python:

      float x = 2.5;
      printf("%d\n", *(int*)&x);
      [color=blue]
      > More concretely, I am thinking particularly of Python vs C++.
      > So, are there any examples (without pointers, references, or adress-taking),
      > which would have a different result in Python and in C++?[/color]

      If I understand your question, I believe not; because Python doesn't
      provide the low-level operators that would be necessary for it.

      Comment

      • Andrea Griffini

        #4
        Re: strong/weak typing and pointers

        On Thu, 28 Oct 2004 18:34:12 +0200, "Diez B. Roggisch"
        <deetsNOSPAM@we b.de> wrote:
        [color=blue][color=green]
        >> Is it correct to say that strong/weak typing does not make a difference
        >> if one does not use any pointers (or adress-taking operator)?[/color]
        >
        >It seems that you mistake strong/weak typing with static/dynamic typing - a
        >completely different beast.
        >
        >Python is in fact strong typed - in opposition to php or perl or even C,
        >this won't work:
        >
        >a = "1" + 2
        >
        >as "1" is a string and 2 an integer. And even though C is statically typed,
        >it won't complain - you just end up with an unexpected result.[/color]

        You didn't mention C++. Try this ...

        std::string s = "Wow";
        s += 3.141592654; // Perfectly valid
        s = 3.141592654; // Also valid

        Andrea

        Comment

        • Grant Edwards

          #5
          Re: strong/weak typing and pointers

          On 2004-10-28, Diez B. Roggisch <deetsNOSPAM@we b.de> wrote:
          [color=blue]
          > It seems that you mistake strong/weak typing with
          > static/dynamic typing - a completely different beast.
          >
          > Python is in fact strong typed - in opposition to php or perl or even C,
          > this won't work:
          >
          > a = "1" + 2
          >
          > as "1" is a string and 2 an integer.[/color]

          "1" is a pointer to a char.
          [color=blue]
          > And even though C is statically typed, it won't complain[/color]

          That's because <pointer> + <integer> has a well-defined meaning
          in C -- just like <float> + <integer> does in Python (and in C).
          [color=blue]
          > - you just end up with an unexpected result.[/color]

          Only people who don't know how C pointer arithmatic works will
          get unexpected results. [That's probably a shockingly high
          percentage of C programmers.]

          --
          Grant Edwards grante Yow! I brought my BOWLING
          at BALL -- and some DRUGS!!
          visi.com

          Comment

          • Alex Martelli

            #6
            Re: strong/weak typing and pointers

            JCM <joshway_withou t_spam@myway.co m> wrote:
            [color=blue]
            > Gabriel Zachmann <zach@cs.uni-bonn.de> wrote:
            >[color=green]
            > > Is it correct to say that strong/weak typing does not make a difference
            > > if one does not use any pointers (or adress-taking operator)?[/color]
            >
            > You'll find a lack of consensus here on what's meant by "strong/weak
            > typing". In Python there's no way to re-interpret the bits of a value
            > as if they were a different type. For example, code like this is
            > impossible in Python:
            >
            > float x = 2.5;
            > printf("%d\n", *(int*)&x);[/color]

            True, but module struct lets you get the same effect, though the 4 bytes
            get copied, not 'reinterpreted in place'.

            [color=blue][color=green]
            > > More concretely, I am thinking particularly of Python vs C++.
            > > So, are there any examples (without pointers, references, or adress-taking),
            > > which would have a different result in Python and in C++?[/color]
            >
            > If I understand your question, I believe not; because Python doesn't
            > provide the low-level operators that would be necessary for it.[/color]

            Well... what about something like:

            std::list<int> a, b;

            ....

            a[2] = 45;
            b = a;
            b[2] = 23;

            In C++, a[2] is still 45, because 'b = a;' COPIED the whole list over.

            A similar case in Python would make no implicit copies, just give an
            additional name 'b' to the same object which 'a' names, so assigning to
            b[2] would also change a[2]. Nothing to do with dynamic vs static
            typing, of course, because e.g. Java would work like Python here.

            I've found this one tidbit to be the single biggest stumbling block for
            experienced C or C++ programmers learning Java or Python. "without
            references" isn't really true, because (in Python and Java) a and b
            _are_ 'references' (aka names) to the same object -- but then, neither
            in Java nor Python can you say that a name _isn't_ ``a reference''...
            names always reference objects... ((Java makes exceptions to this rule
            for some lowlevel types such as ints, Python doesn't)).

            Templates may be another case in which C++ might act one way, and Java
            and Python the other way, and may be more relevant to type issues.

            E.g.,

            template<typena me T>
            T foo(const T& bar)
            {
            static T baz;
            T temp = baz;
            baz = bar;
            return temp;
            }

            now, if you make a series of calls such as foo(1), foo(1.2), foo(2),
            foo(3.4), you should get as results 0, 0.0, 1, and 1.2 -- there are two
            'foo's, one instantiated for T being int, another one for T being
            double, so the 'delay register' baz also exists in two incarnations.

            In the Python rough equivalent:

            def foo(bar, _baz=[None]):
            temp = _baz.pop()
            _baz.append(bar )
            return temp

            (and the Java equivalent, too, with everything declared as Object to be
            "generic"), the same calls would give None, 1, 1.2, 2 -- there is a
            single 'incarnation' of foo, a single 'delay register' _baz. (Not sure
            which way Java 1.5's generics go wrt statics; I'd expect the C++ way).


            Not sure I've gotten the gist of what the OP was asking about, though.

            Alex

            Comment

            • Jorgen Grahn

              #7
              Re: strong/weak typing and pointers

              On Thu, 28 Oct 2004 18:34:12 +0200, Diez B. Roggisch <deetsNOSPAM@we b.de> wrote:
              ....[color=blue]
              > Python is in fact strong typed - in opposition to php or perl or even C,
              > this won't work:
              >
              > a = "1" + 2
              >
              > as "1" is a string and 2 an integer. And even though C is statically typed,
              > it won't complain - you just end up with an unexpected result.[/color]

              [slightly offtopic defense of C and C++]

              That's true only if a is a 'char *' of course (and if you didn't expect
              this unexpected result ;-).

              In C++ 'char *' would have been invalid, but not 'const char *' or
              (and this is worse) 'std::string'.
              [color=blue]
              > And pointers are not evil in themselves - the are a neccessity to create
              > recursive structures.[/color]

              I'd say they are neccessary, period. But note that I count what Java and
              Python call "references " as pointers ...
              [color=blue]
              > But deliberately casting pointers can be very harmful
              > - a reason why its forbidden in languages like java and AFAIK ada.[/color]

              Yes; lots of casts in C code (or worse, in C++ code) is a very, very bad
              sign. Note though, that in the absense of casts, C and in particular C++ are
              pretty strongly typed for pointers. Strongly enough to keep me happy, at
              least.
              [color=blue][color=green]
              >> More concretely, I am thinking particularly of Python vs C++.
              >> So, are there any examples (without pointers, references, or
              >> adress-taking), which would have a different result in Python and in C++?[/color]
              >
              > I have difficulties to understand what you want here. Please elaborate.[/color]

              I think he means the static/dynamic typing, and if it makes a difference in
              a simple C and a simple Python program, if we pretend that all names are
              just "variables" . Hard to come up with a meaningful answer, but how about:

              a = 2 const int a = 2;
              if something_rare_ happens: if(something_ra re_happens) {
              return b return b;
              a = 'hugo' }
              std::string a("hugo");
              bar(a) bar(a);

              In the Python program, we might clobber 'a' by accidentaly reusing its name
              for something of a different type. C++ is stricter about this (doesn't
              allow the construct above, in fact) and you can look at the code
              (statically) to see which names are in scope and which are not.

              In Python, you can forget to give 'b' a value, and not notice until that
              code executes. You can in C++ too, and the runtime effects will be more
              subtle but worse. The compiler is more likely to catch pure typos, though.

              In Python, it is often hard to look at a function such as 'bar' and say you
              know it is always called with an integer argument, or a string, or a 'Foo'
              object. It's not even enough to look at all places where 'bar' is called,
              because the type of b may depend on the phase of the moon or other dynamic
              things. In C++ they compiler makes the guarantees, unless someone has
              willfully bypassed the type system.

              Is all this caused by the static/dynamic typing difference? No, but it
              certainly has to do with it. Both languages have made a decision here, and
              that of course works together with the rest of the language design. Python
              doesn't /have/ to declare variables and parameters to give them a type, so
              Guido said you don't have to, and let functions take all kinds of flexible
              arguments. C++ had to have declarations/definitions, so Bjarne used them to
              add the 'const' keyword, to give values a scope and making it well-defined
              when objects are destroyed. And so on.

              For what it's worth, I think both kinds of typing are interesting and useful
              tools. Neither of them are obsolete or inferior; neither of them will
              disappear in the next ten years.

              /Jorgen

              --
              // Jorgen Grahn <jgrahn@ Ph'nglui mglw'nafh Cthulhu
              \X/ algonet.se> R'lyeh wgah'nagl fhtagn!

              Comment

              • Mel Wilson

                #8
                Re: strong/weak typing and pointers

                In article <slrnco26s8.311 .zach@fuji.info rmatik.uni-bonn.de>,
                Gabriel Zachmann <zach@cs.uni-bonn.de> wrote:[color=blue]
                >
                >Is it correct to say that strong/weak typing does not make a difference
                >if one does not use any pointers (or adress-taking operator)?[/color]

                One effect of weak typing is to put more reliance on
                operators. In Perl, for instance the string operator `lt`
                does a string compare to find that "10" is less than 2
                (lexically) and the numeric operator `<` finds that "10" is
                not less than 2 (numerically). Nothing to do with pointers
                at all.

                Regards. Mel.

                Comment

                • Duncan Booth

                  #9
                  Re: strong/weak typing and pointers

                  Gabriel Zachmann wrote:
                  [color=blue]
                  > Is it correct to say that strong/weak typing does not make a
                  > difference if one does not use any pointers (or adress-taking
                  > operator)?
                  >
                  > More concretely, I am thinking particularly of Python vs C++.
                  > So, are there any examples (without pointers, references, or
                  > adress-taking), which would have a different result in Python and in
                  > C++?[/color]

                  Here's a trivial example that is almost identical in Python and C/C++ but
                  gives totally different results. In a weakly typed language such as C or
                  C++:

                  #include <stdio.h>

                  int main(int argc, char**argv)
                  {
                  float f = 3;
                  printf("value is %d", f);
                  }

                  I get the output (you may get different results):

                  value is 0

                  In a fairly strongly typed language such as Python:
                  [color=blue][color=green][color=darkred]
                  >>> f = 3.0
                  >>> print "value is %d" % f[/color][/color][/color]
                  value is 3

                  In a really strongly typed language I would expect an exception to
                  be thrown.

                  Comment

                  • Oliver Fromme

                    #10
                    Re: strong/weak typing and pointers

                    Diez B. Roggisch <deetsNOSPAM@we b.de> wrote:[color=blue]
                    > And pointers are not evil in themselves - the are a neccessity to create
                    > recursive structures. But deliberately casting pointers can be very harmful
                    > - a reason why its forbidden in languages like java and AFAIK ada.[/color]

                    I agree. A language worth mentioning in this context might
                    be Cyclone. It's derived from C (and still has very much in
                    common with it, so it's easy to port C programs to Cyclone).
                    The difference is that "safe" features have been added to the
                    language. For example, you can't do arbitrary type casts on
                    pointers anymore, and you can't access strings (or other
                    allocated memory) beyond their end. The ultimate goal of
                    Cyclone is to make it impossible for programs to crash or
                    have security holes caused by buffer overflows or similar.

                    Furthermore, Cyclone provides interesting features, such as
                    tagged unions, parametric polymorphism, pattern matching,
                    exceptions, even a somewhat limited implementation of type
                    inference.

                    http://www.research.att.com/projects/cyclone/

                    Best regards
                    Oliver

                    --
                    Oliver Fromme, Konrad-Celtis-Str. 72, 81369 Munich, Germany

                    ``All that we see or seem is just a dream within a dream.''
                    (E. A. Poe)

                    Comment

                    • Gabriel Zachmann

                      #11
                      Re: strong/weak typing and pointers

                      > It seems that you mistake strong/weak typing with static/dynamic typing - a

                      sorry, i don't think so.
                      [color=blue]
                      > completely different beast.
                      >
                      > Python is in fact strong typed - in opposition to php or perl or even C,
                      > this won't work:
                      >
                      > a = "1" + 2[/color]

                      haven't tried that yet, but i guess it would at least evoke a warning in
                      ANSI C++.
                      [color=blue]
                      > as "1" is a string and 2 an integer. And even though C is statically typed,[/color]

                      In C++, "1" is a 'char const * const'.
                      [color=blue]
                      > it won't complain - you just end up with an unexpected result.
                      >
                      > And pointers are not evil in themselves - the are a neccessity to create[/color]

                      i didn't say that.
                      In fact, they are everywhere, even in Python and Java, except that you
                      don't get to see them.
                      [color=blue]
                      > recursive structures. But deliberately casting pointers can be very harmful[/color]

                      i agree.
                      [color=blue]
                      > - a reason why its forbidden in languages like java and AFAIK ada.
                      >[color=green]
                      > > More concretely, I am thinking particularly of Python vs C++.
                      > > So, are there any examples (without pointers, references, or
                      > > adress-taking), which would have a different result in Python and in C++?[/color]
                      >
                      > I have difficulties to understand what you want here. Please elaborate.[/color]

                      i am just trying to come up with the best possible definition of "weak and
                      strong typing" ( "best" in the sense of completeness and objectiveness).
                      I've read up quite a bit about strong/weak typing, and static and dynamic
                      typing, and it seems to me that, while static/dynamic typing is a pretty
                      well-defined concept, the definition of strong/weak typing is not so
                      clear-cut.

                      Cheers,
                      Gab.

                      --
                      /-------------------------------------------------------------------------\
                      | There are works which wait, |
                      | and which one does not understand for a long time; [...] |
                      | for the question often arrives a terribly long time after the answer. |
                      | (Oscar Wilde) |
                      +-------------------------------------------------------------------------+
                      | zach@cs.uni-bonn.de __@/' www.gabrielzachmann.org |
                      \-------------------------------------------------------------------------/

                      Comment

                      • Gabriel Zachmann

                        #12
                        Re: strong/weak typing and pointers

                        > You didn't mention C++. Try this ...[color=blue]
                        >
                        > std::string s = "Wow";
                        > s += 3.141592654; // Perfectly valid
                        > s = 3.141592654; // Also valid[/color]

                        ah, good example.

                        So, would it be valid to say:
                        the more coercion (or automatic conversion) rules a language has, the
                        weaker the typing?

                        Best regards,
                        Gabriel.

                        --
                        /-------------------------------------------------------------------------\
                        | There are works which wait, |
                        | and which one does not understand for a long time; [...] |
                        | for the question often arrives a terribly long time after the answer. |
                        | (Oscar Wilde) |
                        +-------------------------------------------------------------------------+
                        | zach@cs.uni-bonn.de __@/' www.gabrielzachmann.org |
                        \-------------------------------------------------------------------------/

                        Comment

                        • Gabriel Zachmann

                          #13
                          Re: strong/weak typing and pointers

                          > printf("value is %d", f);

                          This seems a very good example to me.

                          Note that this is also an example showing that C++ does contain a little
                          bit of dynamic typing, too, isn't it?

                          Cheers,
                          gab.

                          --
                          /-------------------------------------------------------------------------\
                          | There are works which wait, |
                          | and which one does not understand for a long time; [...] |
                          | for the question often arrives a terribly long time after the answer. |
                          | (Oscar Wilde) |
                          +-------------------------------------------------------------------------+
                          | zach@cs.uni-bonn.de __@/' www.gabrielzachmann.org |
                          \-------------------------------------------------------------------------/

                          Comment

                          • Diez B. Roggisch

                            #14
                            Re: strong/weak typing and pointers

                            Gabriel Zachmann wrote:
                            [color=blue][color=green]
                            >> printf("value is %d", f);[/color]
                            >
                            > This seems a very good example to me.
                            >
                            > Note that this is also an example showing that C++ does contain a little
                            > bit of dynamic typing, too, isn't it?[/color]

                            Where do you get that idea from? Modern compilers are aware of printf, and
                            have special type-checking rules built into them that verify that you pass
                            the right number and types of arguments for the format string. And thats
                            totally static, as it is done at compiletime!

                            --
                            Regards,

                            Diez B. Roggisch

                            Comment

                            • Diez B. Roggisch

                              #15
                              Re: strong/weak typing and pointers

                              > Note that this is also an example showing that C++ does contain a little[color=blue]
                              > bit of dynamic typing, too, isn't it?[/color]

                              On a related note: c++ _can_ have some dynamic type information - when not
                              disabled with -fno-rtti (gcc) you get "real time type identification" . That
                              allows for (guess why there called that way...) dynamic casts.

                              --
                              Regards,

                              Diez B. Roggisch

                              Comment

                              Working...