1

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
  • 4
    What do you want to do with this pointer once you've added it to the language, and how is that different from what references currently do? Jul 13, 2023 at 12:15
  • 1
    And are you certain that weakly typed languages are not managing pointers already? The fact it is a reference or a pointer might be determined at runtime. Jul 13, 2023 at 12:16
  • 1
    @PhilipKendall: or a different language. Jul 13, 2023 at 13:06
  • 1
    In terms of whether pointers are available, it is more about the underlying runtime environment rather than the language itself. Pointers are generally available on almost any unmanaged platform. The fact that javascript and Python do not have pointers is because their runtime platforms deliberately insulate programs away from low level memory management. Any language is generally limited by the capabilities of the target platform/runtime environment Jul 14, 2023 at 11:17
  • 1
    The question is a bit unclear. "something that points to something" is exactly what references are. The difference between references and pointers is usually defined as pointers can be changed after creation - e.g. iterating through a string or array by incrementing a pointer. This introduces the risk of writing to an illegal memory address which is dangerous, and why languages like Java or JavaScript do not allow pointer arithmetic. But this is unrelated to the distinction between week/stong/static/dynamic typing.
    – JacquesB
    Jul 15, 2023 at 11:15

7 Answers 7

3

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).

6

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.

4

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.

0

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.

0

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.

0

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];
-1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.