Forum > FPC development
Why are types sometimes interchangeable
ctk:
Hi, I'm the one who made the initial remark.
I'm not certain why Joanna meant to make it a question of sorts, as it's only an observation. And a reason I don't see Pascal as a usable language, due to warts (such as this one) in its featureset.
Pascal will not be fixed anyway, as it is set in stone now, so the best I can hope is its users will move on to better-designed languages, after acquiring a sense for what good design is. (which my posting here might help with)
So here's the problem explained, take two:
In Pascal, I have two options to declare a new type.
I can do an alias, which does nothing but add a name for the same type:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program typeDemo(input, output, stderr); type vector = record x: real; y: real;end;type coords = vector; var v1: vector;var c1: coords; begin // there are no record initializers or what? nevermind, let's do it field by field. v1.x := 10.0; v1.y := 5.0; c1 := v1;end.(This compiles; great! That's what was expected, and what I needed.)
Alternatively, I can declare an entirely new type from an existing one:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program typeDemo(input, output, stderr); type vector = record x: real; y: real;end;type coords = type vector; // note that I added 'type' here var v1: vector;var c1: coords; begin // there are no record initializers or what? nevermind, let's do it field by field. v1.x := 10.0; v1.y := 5.0; c1 := v1;end.(This fails to compile; great! That's what was expected, and what I needed.)
And of course I can do the same thing with any other type, so:
Let's do an alias again:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program typeDemo(input, output, stderr); type meters = type real;type seconds = meters; var l1: meters;var t1: seconds; begin t1 := 10.0; l1 := t1;end.(This compiles; great! That's what was expected, and what I needed.)
And now I want these to be incompatible, so I add 'type':
(I badly need a compiler error if I ever assign seconds to meters, say if I'm a physicist)
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program typeDemo(input, output, stderr); type meters = type real;type seconds = type meters; // note that I added 'type' here var l1: meters;var t1: seconds; begin t1 := 10.0; l1 := t1;end.This compiles; what gives?! That's completely unexpected, adding 'type' didn't have its effect. In fact it seems to have had no useful effect whatsoever.
It worked with some other type earlier, so this is inconsistent and arbitrary.
Features should work uniformly. https://en.wikipedia.org/wiki/Principle_of_least_astonishment
MarkMLl:
@ctk: frankly, I agree with you. I've previously remarked that Pascal was, for various reasons, rushed; it was knocked together not long after Hoare and Wirth had started working on the concept of distinct types and Wirth's subsequent languages were far stricter.
MarkMLl
Thaddy:
From the wiki:"You want to do this, for instance in order to define a whole new set of operators. Otherwise the operator definitions for the type it was cloned from would still apply."
And the manual: https://www.freepascal.org/docs-html/ref/refse19.html#x49-690003.8
The latter explains that type Tx = type y generates different RTTI so creates a means to identify a typed type.
This is also used inside the rtl and possibly the compiler. (And Delphi compatible)
It may seem a bit shallow, but it's not.
Bogen85:
--- Quote from: ctk on January 05, 2023, 03:59:33 pm ---Pascal will not be fixed anyway, as it is set in stone now, so the best I can hope is its users will move on to better-designed languages, after acquiring a sense for what good design is. (which my posting here might help with)
--- End quote ---
I understand where you are coming from. There are many things in Pascal that I don't like. And I'm aware of and know many other languages (some very well, and I do use them where it is applicable).
However, there are many things I prefer to do in Pascal, by choice, despite the many shortcomings it has.
--- Quote from: ctk on January 05, 2023, 03:59:33 pm ---
And now I want these to be incompatible, so I add 'type':
(I badly need a compiler error if I ever assign seconds to meters, say if I'm a physicist)
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program typeDemo(input, output, stderr); type meters = type real;type seconds = type meters; // note that I added 'type' here var l1: meters;var t1: seconds; begin t1 := 10.0; l1 := t1;end.This compiles; what gives?! That's completely unexpected, adding 'type' didn't have its effect. In fact it seems to have had no useful effect whatsoever.
It worked with some other type earlier, so this is inconsistent and arbitrary.
Features should work uniformly. https://en.wikipedia.org/wiki/Principle_of_least_astonishment
--- End quote ---
I agree with you, and this is one of the things I do not like about Pascal.
However, if I needed to have Pascal enforce types in this way, that can still be done. Not at the base type level like you are showing here, but via classes or advanced records with overloaded operators and properties.
That way I segregate the different units that share the same base types but since I've hidden access to them directly, the compiler will do the enforcement you (and I) would be looking for.
MarkMLl:
--- Quote from: Bogen85 on January 05, 2023, 04:15:30 pm ---However, if I needed to have Pascal enforce types in this way, that can still be done. Not at the base type level like you are showing here, but via classes or advanced records with overloaded operators and properties.
--- End quote ---
In Wirth's defence, the idea of having distinct types for numerics etc. that were structurally compatible was very much a novel one. While (even in the ALGOL-60 era) people appreciated that reals were distinct from integers and integers were distinct from strings (and all three were often handled by distinct areas of the CPU), the idea that two reals were not assignment-compatible might even have made it more difficult to sell the idea of block-structured languages than it was already.
MarkMLl
Navigation
[0] Message Index
[#] Next page
[*] Previous page