Is it possible to introduce Pointers to a weakly typed programming language?

  softwareengineering

Most weakly typed languages do not have a concept of a pointer. Instead, most of them use references. However, is it possible to introduce pointers to such a language.

From a naive point of view, to have pointers, you almost certainly need to have a concept of a type system that can be used to declare things (which means that Perl’s packages, Ruby’s classes don’t count, as you don’t do Integer i = 1 in Ruby), thus allowing you to define pointers like

var a *SomeType

However, if you don’t have such a concept (think early Python, JavaScript, etc.), it seems impossible to actually allow the language user to convey the meaning that I want something that points to something. I’m wondering whether this naive understanding is correct.

8

A key issue with pointers has nothing really to do with the existence or otherwise of a strong type system.

What you need is a language that clearly exposes the existence of numerically-addressable memory, a dereference operator, and all the associated baggage of understanding that.

Another key thing about pointers is that the memory refers back to itself – the value of a field stored at a certain address, is itself a memory address.

People can be accustomed to the concept of numerical cross-references from everyday life, but they’ll rarely have used a list in an everyday situation in which both sides of a reference are the same indexed list (this is practically characteristic of data processing and storage as a science), and they definitely won’t have encountered a situation where a (sane) person has chosen to store all varieties of information in the same indexed list (this is solely characteristic of how computer memory works).

There are weakly-typed programming languages that have pointers.

C, for example, has pointers and arguably has one of the weakest type systems among current mainstream programming languages.

I think you are maybe meaning dynamically typed rather than weakly typed, so I’ll answer for both.

In a weakly typed language, variables are more often coerced into other types rather than throwing a type mismatch error. Weakly typed languages can either be statically or dynamically typed. In C, for example, a weakly typed static language, if you try to use an int as a pointer it will let you do that cast without throwing a type error, but you may end up trying to access memory that doesn’t exist.

In a dynamically typed language, the type of variables is determined at the last minute at runtime instead of ahead of time at compile time. That doesn’t mean there isn’t a type, or that the type isn’t known, or that you won’t get a type error if you try to use it incorrectly.

References are essentially pointers with fewer allowed operations. In a dynamically typed language, you would simply need to define those extra operations. A reference and dereference operator (& and *), and math operations. If you try to use them on something that isn’t a pointer, you get a runtime error, same as if you try to call a method that doesn’t exist on an object.

The language B, a predecessor to C, was weakly typed (or typeless, depending on definition) and supported pointers. You could dereference an arbitrary integer value, and it would be treated as a memory address.

So it is possible – but whether it is a good idea is a different question. There is a reason C added pointer types.

As you mention, many weakly typed language use reference semantics, i.e. they manipulate handles that know where the object is. In this context you do not need pointers, because the reference replaces it, in a safer way.

If this is not sufficient for your purpose, for example because you want to iterate through an array, like pointers conveniently allow, you need to use the iterator design pattern. This is what is done in modern C++, where native C like arrays are rarely used, and iterators routinely replace them, also in a safer way, to iterate through vectors and other nice containers.

I just hope that you do When you write

to convey the meaning that I want something that points to something.

you do not have in mind “something of a given kind”, because this would mean to look for stronger typing semantics in a language that has banned it, and this is then a different and more compex story.

Reference semantics in Python and Javascript is achieved via a mutable collection – instead of passing around a value, put it in a list of size 1 and pass that around.

function readValue(output) {
    output[0] = "new value";
}

let datum = [null];
readValue(datum);
let result = datum[0];

Most modern languages have C bindings that necessitate use of pointers. Sometimes pointers are represented by:

  • large integers
  • dedicated dynamic type

A trivial example is Python’s ctypes library

LEAVE A COMMENT