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

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật