Forum > General

[CLOSED] Absolute

(1/3) > >>

pcurtis:
Is


--- 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";}};} ---var  Grid : TStringGrid absolute Sender;   Grid.... 
the same as


--- 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";}};} ---(Sender as TStringGrid).... 

Martin_fr:
All the below are assumptions

"as" afaik includes type safety checking, and can throw an exception if the instance in Sender is not a related class...

So if anything then

--- 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";}};} ---TStringGrid(Sender)
But I am not 100% sure, if there are any minor details that differ...

One thing: Casting only works for some types. You can't do "TMyRecord(somevar)". But you should be able to use absolute in that case.

And (I guess) depending on endian-ness

--- 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";}};} ---var a: longint;b: byte absolute a;
This may differ to "Byte(a)". I would expect "Byte(a)" to return the low-byte. But absolute to depend on memory order.
On little endian, the lowest-order byte is first => at the same address as the longint
On big endian, the lowest-order byte is last => at "PByte(@a)+3"

In your case, both types have the same size. So that is not an issue.




pcurtis:
Thanks.

So the answer is more or less the same.

howardpc:
No, similar, but not the same.
When casting Sender to a descendant class, it is wise to check that the cast is valid.
"as" checks and raises an exception for an invalid cast, as Martin_fr writes.
An alternative, better, in most circumstances unless you want the user to see the potential exception, is to guard the implicit absolute cast with

--- 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";}};} ---var  grid: TStringGrid absolute Sender;begin    if Sender.InheritsFrom(TStringGrid) then    begin       grid...   end; This simply fails to run the grid's block of code if it is not valid to do so, without raising an exception.

Most exceptions seen by users indicate a shortcoming in the programmer's approach.

Warfley:
Absolute performs zero compiler checks:

--- 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";}};} ---var  a: Byte;  b: TStringList absolute a;begin  b := TStringlist.Create; This is broken code, it compiles without a warning or error but completely smashes your stack.
But if you try the following:

--- 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";}};} ---var  a: Byte;begin  a := Byte(TStringList.Create)end.The compiler will throw an error that what you are trying to do is wrong.

IMHO there is absolutely no case where absolute is a good idea. If Access to the same memory is required, variant records and/or (pointer) casting is always preferential, as it archives the same effect, while 1. being more verbose about what is actually happening here and 2. performs error checking

Navigation

[0] Message Index

[#] Next page

Go to full version