Recent

Author Topic: Modeswitch for no "specialize" keyword like in Delphi mode  (Read 12193 times)

Okoba

  • Hero Member
  • *****
  • Posts: 611
Re: Modeswitch for no "specialize" keyword like in Delphi mode
« Reply #15 on: December 31, 2024, 03:22:17 pm »
Yes, I was pointing out that not writing `specialize` looks clean.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 10790
  • Debugger - SynEdit - and more
    • wiki
Re: Modeswitch for no "specialize" keyword like in Delphi mode
« Reply #16 on: December 31, 2024, 03:50:29 pm »
Code: Pascal  [Select][+][-]
  1.   {$modeswitch implicitfunctionspecialization}

Different issue though. "implicitfunctionspecialization" is just a way of reverting how Pascals type safe system works... Albeit in that case, probably not an issue.

I was referring to the difference between
Code: Pascal  [Select][+][-]
  1. Test<Integer>(V1);
Code: Pascal  [Select][+][-]
  1. specialize Test<Integer>(V1);

What about
Code: Pascal  [Select][+][-]
  1. if Test < Foo > (V1);


Which can be
Code: Pascal  [Select][+][-]
  1. program project1;
  2. {$mode delphi}
  3.   function Test<T>(var V: T): boolean;
  4.   begin
  5.   end;
  6.  
  7. type Foo = integer;
  8. var
  9.   V1: Integer;
  10. begin
  11.   if Test<Foo>(V1) then
  12.     write(1);
  13. end.

or
Code: Pascal  [Select][+][-]
  1. program project1;
  2. {$mode delphi}
  3.   function Test: Integer;
  4.   begin
  5.   end;
  6.  
  7. var
  8.   Foo: Integer;
  9. const V1 = False;
  10. begin
  11.   if Test<Foo>(V1) then
  12.     write(1);
  13. end.

The exact same line of code, just in the 2nd case its 2 comparisons...

So I ask: How is that helping readability?


---
And before we get into the () around V1 => imagine it is an expression that needs to be in brackets. The result of such an expression can be passed as argument or used for comparison.
« Last Edit: December 31, 2024, 03:53:54 pm by Martin_fr »

Okoba

  • Hero Member
  • *****
  • Posts: 611
Re: Modeswitch for no "specialize" keyword like in Delphi mode
« Reply #17 on: January 02, 2025, 08:02:51 am »
Code: Pascal  [Select][+][-]
  1.   if Test<Foo>(V1) then
  2.  

Until now I did not know you can write a line like that!
What does it mean? Foo is between Test and V1?

I get your point about readability.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 10790
  • Debugger - SynEdit - and more
    • wiki
Re: Modeswitch for no "specialize" keyword like in Delphi mode
« Reply #18 on: January 02, 2025, 09:17:39 am »
Code: Pascal  [Select][+][-]
  1.   if Test<Foo>(V1) then
  2.  

Until now I did not know you can write a line like that!
What does it mean? Foo is between Test and V1?

I get your point about readability.

Well, it first checks "Test < Foo", which returns boolean.

Since boolean is an enum, it can be compared based on the ordinal values of False and True.
So it then compares the result of the above with "V1" => e.g "True > False".


But of course, you may have overloaded operators.

Test may return some type of your own, maybe a record. Then the "<" operator for that record vs a number returns another record, which then has an overloaded > against boolean....

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 10790
  • Debugger - SynEdit - and more
    • wiki
Re: Modeswitch for no "specialize" keyword like in Delphi mode
« Reply #19 on: January 02, 2025, 09:21:37 am »
Currently the difference (other than what is in scope) is if "Foo" is a value or type. With a type, there is no < comparison, so it must be the generic.

But once generics can be specialized with value too, .... Well if that happens, but it was discussed at least.

Okoba

  • Hero Member
  • *****
  • Posts: 611
Re: Modeswitch for no "specialize" keyword like in Delphi mode
« Reply #20 on: January 02, 2025, 09:25:57 am »
Thank you.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 10790
  • Debugger - SynEdit - and more
    • wiki
Re: Modeswitch for no "specialize" keyword like in Delphi mode
« Reply #21 on: January 02, 2025, 09:31:32 am »
This thread just gave me an idea. If the "specialize" is present, the highlighter should be able to see the <> as brackets and match them. But only if it is present. And if I find time...

Okoba

  • Hero Member
  • *****
  • Posts: 611
Re: Modeswitch for no "specialize" keyword like in Delphi mode
« Reply #22 on: January 02, 2025, 09:39:02 am »
Great.
Or on declaration of the types, even in Delphi mode it can be detectable.
Do you know the maintainer of Codetools? Generic support of Codetools is unfortunately very weak.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 10790
  • Debugger - SynEdit - and more
    • wiki
Re: Modeswitch for no "specialize" keyword like in Delphi mode
« Reply #23 on: January 02, 2025, 05:07:07 pm »
Highlighting is not done by codetools. (except for inactive ifdef).

Mattias is the expert on Codetools.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5851
  • Compiler Developer
Re: Modeswitch for no "specialize" keyword like in Delphi mode
« Reply #24 on: January 03, 2025, 03:21:49 pm »
@PascalDragon is there a possibility for having this as a mode switch so I can keep using objfpc and eliminate using  generic/specialize keywords?

It is on my infinite todo-list, but with very low priority.

So I ask: How is that helping readability?

And that precisely is one of the reasons why FPC in mode Delphi can't handle such code yet...

But once generics can be specialized with value too, .... Well if that happens, but it was discussed at least.

So, you missed this? 😅

Thaddy

  • Hero Member
  • *****
  • Posts: 16520
  • Kallstadt seems a good place to evict Trump to.
Re: Modeswitch for no "specialize" keyword like in Delphi mode
« Reply #25 on: January 03, 2025, 10:59:16 pm »
Another thing to note is that if you specialize to a type instead of a var, there is not much difference in code verbosity between Delphi mode and ObjFPC mode. In fact: none.

The only nuisance occurs if you don't do that: then it gets verbose to infinity....
But that is basically a choice of the programmer ( and not a very good one in most cases)
E.g:
Code: Pascal  [Select][+][-]
  1. program project1;
  2. {$mode delphi}
  3.   function Test<T>(var V: T): boolean;
  4.   begin
  5.     result := true;
  6.   end;
  7.  
  8. type
  9.   FooTest = Test<integer>;// after this the code is the same in both modes
  10.   Foo = integer;
  11. var
  12.   V1: Integer;
  13. begin
  14.   if Footest(V1) then
  15.     write(1);
  16. end.
Code: Pascal  [Select][+][-]
  1. program project1;
  2. {$mode objfpc}
  3.   generic function Test<T>(var V: T): boolean;
  4.   begin
  5.     result := true;
  6.   end;
  7.  
  8. type
  9.     FooTest = specialize Test<integer>;// after this all code is the same in both modes
  10.     Foo = integer;
  11. var
  12.   V1: Integer;
  13. begin
  14.   if fooTest(V1) then
  15.     write(1);
  16. end.

Your code becomes a lot cleaner.
« Last Edit: January 04, 2025, 10:36:09 am by Thaddy »
But I am sure they don't want the Trumps back...

 

TinyPortal © 2005-2018