Is there a distinct optional type semantically representing a value that *needs to be calculated later*?

Semantically, C++ std::optional, Rust Option<T>, and other optional/nullable types represent a value that can be present or absent: you have to handle both cases, or you can opt-in to crash.

Is there utility in a type that represents a value that doesn’t exist yet, but needs to later on in execution? Or does such a thing exist? Example use case: an offset in AST node that needs to be calculated in a later pass.

5

Some concepts come to mind (you would need to do some own research for C++/Rust, I only linked a first result):

  1. Lazy<T>: A value that will be computed at a possible later time, but is guaranteed to be there, unlike Optional.

Examples: Lazy (kotlin), lazy (swift)

Similar would be lateinit variables in those languages, which does not compute on-demand, but “guarantees” the property will be set before it is accessed.

C++: Found this on SO.

  1. Future: Value will be computed asynchronously and is often accessed in a reactive way

Examples: Future (Java),Promise (JS)

C++: std::future

  1. Computed properties

Example: C#

  1. Supplier/Functions as a way to access calculated values.

Example: Supplier (Java)


In general, there is nothing stopping you from providing a function or initializer as a wrapper to some property. See also the other answer.

Yes, sort of. It is part of almost any major programming language: it is called a first-class function.

Instead of passing a specific value v, one passes a function f around which can calculate v on demand, at the point in time when it is required. It is basically the core concept behind functional programming.

There are several other concepts based on this: objects (in OO) carry data plus functions, they can be passed around and used just like forementioned standalone functions. Generator functions (a.k.a. iterators) allow the generation of iterable sets of values on demand, instead of precalculating some array.

Of course, for things like your “offset in an AST” example, often the most simple approach is to use some nullable or optional type as a placeholder for the values which have to be calculated later. One could surely make use of functional placeholders for such offsets, or model it by using a specific object Offset for it, but this bears a certain risk of making things more complicated than necessary.

2

There is a range of possibilities, covering the spectrum from lenient to strict.

You did not explicitly state whether the value can be computed lazily when a client needs it, or is computed in a later pass over the data (your example sounds a bit like that). Depending on this, the approaches could be a bit different.

One is to simply use the optional type, and rely on documentation to ensure that developers use the attribute as intended. This would work in both scenarios.

An alternative would be to use a type that can be computed and set once and cannot be changed later. I’m not sure whether the Once<> type would fit the bill, though.

One possibility that comes to mind with strict typing and functional design would be to have separate types for the AST nodes, one without and one with position information. If it is decidable which parts of your code work with the AST before or after the position-determining pass, you can use these types appropriately, so the type checker would catch any attempts to access the attribute before it is available. The position-determining pass would then not be an operation that fills in position information in an existing AST, but a function transforming a positionless AST into one with positions.

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