Recent

Author Topic: Interesting compiler feature  (Read 4336 times)

soerensen3

  • Full Member
  • ***
  • Posts: 213
Interesting compiler feature
« on: November 04, 2017, 12:37:11 am »
Today I discovered an interesting compiler feature. In other languages it is quite common that you can do the following with an object:
Code: Pascal  [Select][+][-]
  1. if ( obj ) then ...
  2.  
which is equivalent to:
Code: Pascal  [Select][+][-]
  1. if ( Assigned( obj )) then ...
  2.  
While not possible with standard pascal libraries you can get the feature in pascal if you overload the assignment operator:
Code: Pascal  [Select][+][-]
  1. operator := ( obj: TObject ): Boolean;
  2. begin
  3.   Result:= Assigned( obj );
  4. end;
  5.  
I think it the code is still understandable but shorter. This could also be applied to other types as pointers or ints and so on. But I think there it is less understandable.
What do you think, is this bad programming practice if used locally inside a library?
Lazarus 1.9 with FPC 3.0.4
Target: Manjaro Linux 64 Bit (4.9.68-1-MANJARO)

ccrause

  • Hero Member
  • *****
  • Posts: 845
Re: Interesting compiler feature
« Reply #1 on: November 04, 2017, 07:29:02 am »
Today I discovered an interesting compiler feature. In other languages it is quite common that you can do the following with an object:
Code: Pascal  [Select][+][-]
  1. if ( obj ) then ...
  2.  
which is equivalent to:
Code: Pascal  [Select][+][-]
  1. if ( Assigned( obj )) then ...
  2.  
Your approach hides a boolean expression.  As long as someone trying to understand your code remembers this, it is probably OK.  I am not so keen on the approach, but it is a personal preference.

BeniBela

  • Hero Member
  • *****
  • Posts: 905
    • homepage
Re: Interesting compiler feature
« Reply #2 on: November 04, 2017, 08:02:29 am »
This is brilliant!

Could add an inline modifier

bytebites

  • Hero Member
  • *****
  • Posts: 633
Re: Interesting compiler feature
« Reply #3 on: November 04, 2017, 08:17:34 am »
This kind of changes has previously been rejected with laugh.

balazsszekely

  • Guest
Re: Interesting compiler feature
« Reply #4 on: November 04, 2017, 08:24:47 am »
Feature request for the haters: Perhaps you manage somehow to change begin end to {}.  :D

Leledumbo

  • Hero Member
  • *****
  • Posts: 8747
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Interesting compiler feature
« Reply #5 on: November 04, 2017, 09:14:41 am »
What do you think, is this bad programming practice if used locally inside a library?
From Pascal spirit POV, yes. That actually kills typing discipline because now obj has two interpretations: as TObject or boolean, which can be confusing and is a potential source of hidden bug. Feel free to use it, though. Your code is your code after all.

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1186
    • Burdjia
Re: Interesting compiler feature
« Reply #6 on: November 04, 2017, 11:45:37 am »
This is a good example of why I'm against operator overloading.  >:D
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: Interesting compiler feature
« Reply #7 on: November 04, 2017, 01:11:42 pm »
While not possible with standard pascal libraries you can get the feature in pascal if you overload the assignment operator:
...and that breaks the statement like this
Code: Pascal  [Select][+][-]
  1. var
  2.   a : TObject;
  3.   b : TObject;
  4. ..
  5.   (a)=b
  6.  

Thaddy

  • Hero Member
  • *****
  • Posts: 14210
  • Probably until I exterminate Putin.
Re: Interesting compiler feature
« Reply #8 on: November 04, 2017, 01:27:11 pm »
Also note you overload the assignment operator := not the equality operator =
Specialize a type, not a var.

soerensen3

  • Full Member
  • ***
  • Posts: 213
Re: Interesting compiler feature
« Reply #9 on: November 04, 2017, 08:28:22 pm »
Quote
Feature request for the haters: Perhaps you manage somehow to change begin end to {}.  :D

Or how about this  :D
Code: Pascal  [Select][+][-]
  1.   {$MACRO ON}
  2.   {$DEFINE end:=begin}
  3.   {$DEFINE begin:=end}
  4. // No need to try it's not working
  5.  

I didn't know that it would upset people so much ;)
I see your point though. But if it is about type strictness assignment operator overloading shouldn't be there in the first place. But it is - I think even in Delphi (?). The code is not readable for other if you rely on things like this too much. But I also like the challenge about finding such things are possible and also I don't really like to repeat a lot of code over and over only because of too much type strictness (That's why I like generics). It can really eat up time, makes the code more complicated and less maintainable.
Lazarus 1.9 with FPC 3.0.4
Target: Manjaro Linux 64 Bit (4.9.68-1-MANJARO)

Thaddy

  • Hero Member
  • *****
  • Posts: 14210
  • Probably until I exterminate Putin.
Re: Interesting compiler feature
« Reply #10 on: November 04, 2017, 08:51:29 pm »
There are a couple of spare chars left in the scanner: we could add ~ ? | and | (the latter is already defined)
Specialize a type, not a var.

balazsszekely

  • Guest
Re: Interesting compiler feature
« Reply #11 on: November 04, 2017, 11:41:57 pm »
@soerensen3
Actually you did a cool thing, but don't expect it to be main stream feature.  :D

 

TinyPortal © 2005-2018