Recent

Author Topic: [Solved] Delphi alternative to Pascal code?  (Read 3361 times)

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
[Solved] Delphi alternative to Pascal code?
« on: April 23, 2018, 04:16:40 pm »
This is my pascal code, that fails on Delphi:

Code: Pascal  [Select][+][-]
  1. type
  2.  
  3. TTipoReporteJornada = (rjAcumuladoPLUs, rjAcumuladoFamilias);
  4.  
  5. const
  6.  
  7. TipoReporteJornadaStr: array [TTipoReporteJornada] of string =
  8.     ('AcumuladoPLUs', 'AcumuladoFamilias');
  9.  
  10. implementation
  11.  
  12. function StrToTipoReporteJornada(s: string): TTipoReporteJornada;
  13. var
  14.   i: TTipoReporteJornada;
  15. begin
  16.   for i in TTipoReporteJornada do
  17.     if s = TipoReporteJornadaStr[i] then
  18.       exit(i);
  19. end

StrToTipoReporteJornada fails on Delphi 10:

Quote
[dcc32 Error] impresorafiscal.pas(250): E2029 '(' expected but 'DO' found (in the 'do' part)
[dcc32 Error] impresorafiscal.pas(251): E2066 Missing operator or semicolon (in the 'if' part)
[dcc32 Error] impresorafiscal.pas(252): E2430 for-in statement cannot operate on collection type 'procedure, untyped pointer or untyped parameter'

I need to add that the 's' value is always right, is provided as string by the JSON, but always will meet the enum.
« Last Edit: April 23, 2018, 04:41:32 pm by lainz »

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Delphi alternative to Pascal code?
« Reply #1 on: April 23, 2018, 04:22:16 pm »
Which Delphi version? Such code needs a recent Delphi and I am not sure Delphi can work on the type directly (AFAIK not!) for i in TTipoReporteJornada do but needs an instance variable.
FPC has more advanced syntax, also in Delphi modes. Best to write it in Delphi and compile the compatible code with FPC. And Exit(<value>) syntax is also not supported by Delphi afaik.
FPC is compatible with Delphi code (except at the moment anonymous methods and attributes) but Delphi is not necessarily compatible with FPC code even if the FPC code is written using {$mode delphi}.
This is documented behavior.
« Last Edit: April 23, 2018, 04:28:58 pm by Thaddy »
Specialize a type, not a var.

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: Delphi alternative to Pascal code?
« Reply #2 on: April 23, 2018, 04:28:28 pm »
It is Delphi 10, well, I can get rid of Exit.

So I need to use a const set of TTipoReporteJornada, to get it working?

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Delphi alternative to Pascal code?
« Reply #3 on: April 23, 2018, 04:30:12 pm »
Or a var. It can be that you need to write an enumerator as well since for in do syntax for enums may not be supported out of the box.
Specialize a type, not a var.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Delphi alternative to Pascal code?
« Reply #4 on: April 23, 2018, 04:31:30 pm »
Exit(x) is D2009+

A TDictionary is maybe a solution.

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: Delphi alternative to Pascal code?
« Reply #5 on: April 23, 2018, 04:41:21 pm »
Thanks, I solved it like this:

Code: Pascal  [Select][+][-]
  1. for i := Low(TTipoReporteJornada) to High(TTipoReporteJornada) do
  2.     if s = TipoReporteJornadaStr[i] then
  3.       exit(i);

ASerge

  • Hero Member
  • *****
  • Posts: 2223
Re: Delphi alternative to Pascal code?
« Reply #6 on: April 24, 2018, 06:42:56 pm »
Thanks, I solved it like this:
What about StrToTipoReporteJornada('Some trash text')?

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: Delphi alternative to Pascal code?
« Reply #7 on: April 25, 2018, 01:53:55 pm »
Thanks, I solved it like this:
What about StrToTipoReporteJornada('Some trash text')?

I don't mind, because is returned by the 'server' of the Fiscal Printer, and always is OK.

ASerge

  • Hero Member
  • *****
  • Posts: 2223
Re: Delphi alternative to Pascal code?
« Reply #8 on: April 25, 2018, 02:04:29 pm »
What about StrToTipoReporteJornada('Some trash text')?
I don't mind, because is returned by the 'server' of the Fiscal Printer, and always is OK.
That is, you do not mind the random result of the function. Then why use it at all?

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: Delphi alternative to Pascal code?
« Reply #9 on: April 25, 2018, 02:33:50 pm »
What about StrToTipoReporteJornada('Some trash text')?
I don't mind, because is returned by the 'server' of the Fiscal Printer, and always is OK.
That is, you do not mind the random result of the function. Then why use it at all?

Is not random. And it works fine.

ASerge

  • Hero Member
  • *****
  • Posts: 2223
Re: Delphi alternative to Pascal code?
« Reply #10 on: April 25, 2018, 04:51:12 pm »
What about StrToTipoReporteJornada('Some trash text')?
I don't mind, because is returned by the 'server' of the Fiscal Printer, and always is OK.
That is, you do not mind the random result of the function. Then why use it at all?
Is not random. And it works fine.
Really? What about StrToTipoReporteJornada('Some trash text')?

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: [Solved] Delphi alternative to Pascal code?
« Reply #11 on: April 25, 2018, 05:11:45 pm »
Hi ASerge, what you don't understand is:
- The server returns the string that's passed to that function, and always is ok.
- I never pass any string to it made by the user / myself, so passing 'Some trash text' is not the point.

If you say that it needs a default value, it looses it's functionality, to convert from string to enum, to one that is already fine in the response.

You say that I need to add a new enum value like rjError, rjDefault? And if there is none found return that?

Well, I think about that, but I choose to don't do that, since I have a lot of enums, and adding non compatible values with the API of the fiscal printer, it will make more problems, like passing rjDefault to one of the commands, and having error with that.

If the string is always ok, what's the point?

ASerge

  • Hero Member
  • *****
  • Posts: 2223
Re: [Solved] Delphi alternative to Pascal code?
« Reply #12 on: April 25, 2018, 05:19:21 pm »
Hi ASerge, what you don't understand is:
- The server returns the string that's passed to that function, and always is ok.
...
If the string is always ok, what's the point?
Unexpected behavior. I propose to add at the end of the code
Code: Pascal  [Select][+][-]
  1. raise Exception.Create('StrToTipoReporteJornada.Unexpected behavior');
Kind of like the alarm, if in the future the server behavior to change.

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: [Solved] Delphi alternative to Pascal code?
« Reply #13 on: April 25, 2018, 05:35:30 pm »
Ok, yes, that is a good idea.

I already found a single difference between the manual of the "printer commands" and the emulator, a value that's supposed to return that's never returned. So I can now see your point.

I will implement that in all conversion functions.

 

TinyPortal © 2005-2018