The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 4,262 other subscribers

Delphi – when compiling consts doesn’t work…

Posted by jpluimers on 2018/05/23

Delphi has a lot of loose ends. With the extension of the language, it gets more and more. Or as Stefan Glienke formulated it:

It should all compile but it does not because currently what is a real const and what is a variable treated as const by the compiler is a f…ing mess and thus some combinations don’t work. If you have watched that C++ session recently posted here you know how const should be handled by a compiler in 2017.

[WayBack] The recent next gen compiler debate reminded me of this nice talk.This is about c++ but it shows off nicely what a high quality compiler can achieve in… – Christoph Hillefeld – Google+

Source [WayBackI don’t need a solution for this but wonder if Elements4 should compiles or not… – Paul TOTH – G+

–jeroen


type
TElement = record
Idx : Integer;
Valeurs: TArray<Integer>;
end;
TElements = TArray<TElement>;
const
El1 : TElement = (Idx: 1; Valeurs: [1]); // ok
Elements1 : array[0..0] of TElement = ((Idx: 1; Valeurs: [1])); // ok
Elements2 : TElements = []; // ok
Elements3 : TElements = [El1]; // E2026 Constant expression expected
Elements4 : TElements = [(Idx: 1; Valeurs: [1])]; // E2003: Undeclared identifier: 'Idx'

3 Responses to “Delphi – when compiling consts doesn’t work…”

  1. rvelthuis said

    Disagree. The concept of pure and typed constants is pretty clear and can and should not be compard with constness in C++. The concepts are quite different. If you think about it a little longer, especially from the perspective of a compiler, you will also see what the difference is and that you could hardly ever use typed constants where pure constants are required.

    • jpluimers said

      That is indeed the source of the problem: One kind of consts is more const than others.

      • rvelthuis said

        Both are const alright, but one is more typed (and certified to have an address) than the other. True constants do not even have to be stored (or have a specific type), they can be immediate values like 17, 3.9 or ‘A’. Typed constants are in fact immutable “variables” and are certain to have an address. Both are equally const, just different in other aspects.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.