Recent

Author Topic: [NO SOLUTION] Case with string in Delphi mode  (Read 6689 times)

Okoba

  • Hero Member
  • *****
  • Posts: 528
Re: Case with string in Delphi mode
« Reply #15 on: March 17, 2022, 01:23:11 pm »
Thanks.

bytebites

  • Hero Member
  • *****
  • Posts: 624
Re: [NO SOLUTION] Case with string in Delphi mode
« Reply #16 on: March 17, 2022, 01:45:09 pm »
There is solution: Modify your compiler.

Okoba

  • Hero Member
  • *****
  • Posts: 528
Re: [NO SOLUTION] Case with string in Delphi mode
« Reply #17 on: March 17, 2022, 01:49:57 pm »
Not a good way to maintain your own version of the compiler for just this simple change.

Although these minor issues are piling up, string case, generics, and operators ObjFPC and Delphi mode each have their best features, and I wish we could have both. For now, the best solution is to have different units for different needs, not clean though.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6646
Re: Case with string in Delphi mode
« Reply #18 on: March 17, 2022, 01:53:38 pm »
P.S. I did not know Jonas is a compiler dev.

Of many years standing, and with significant academic etc. credentials.

Noting Thaddy's comment that something like this should have no place in the language: I've got reservations, but it's undeniably useful when writing command parsers etc. By comparison, I've seen a language family that required this sort of thing to be done using predefined constant tables, and the separation of data and control resulted in a grievous loss of clarity.

However, I'd be very interested if somebody (Sven?) had a suggestion of how the current case could be replaced by something more genuine: an inline table of paired literals and lambdas or similar.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: Case with string in Delphi mode
« Reply #19 on: March 18, 2022, 03:54:11 pm »
Noting Thaddy's comment that something like this should have no place in the language: I've got reservations, but it's undeniably useful when writing command parsers etc. By comparison, I've seen a language family that required this sort of thing to be done using predefined constant tables, and the separation of data and control resulted in a grievous loss of clarity.

To be fair, case of with a string expression is absolutely a syntatic sugar for a sequence of if-statements in contrast to the one for ordinals which offers a real performance difference.

However, I'd be very interested if somebody (Sven?) had a suggestion of how the current case could be replaced by something more genuine: an inline table of paired literals and lambdas or similar.

What kind of improvement do you hope to get from something like this?

MarkMLl

  • Hero Member
  • *****
  • Posts: 6646
Re: Case with string in Delphi mode
« Reply #20 on: March 18, 2022, 04:04:29 pm »
What kind of improvement do you hope to get from something like this?

I don't, frankly. But the more syntactic sugar that can be moved out of the language into support libraries the better IMO.

I suppose though that a case statement- irrespective of the type of the controlling expression- is fairly closely related to the inline if-then-else that we've discussed before.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: Case with string in Delphi mode
« Reply #21 on: March 18, 2022, 04:09:07 pm »
What kind of improvement do you hope to get from something like this?

I don't, frankly. But the more syntactic sugar that can be moved out of the language into support libraries the better IMO.

You simply can't move things out of the language that are there already. The point is to be conservative to what is moved into the language instead.

But with the upcoming anonymous functions or even with nested functions one could already create some generic case-like functionality.

I suppose though that a case statement- irrespective of the type of the controlling expression- is fairly closely related to the inline if-then-else that we've discussed before.

Except for types where the compiler can at compile time determine an order (ordinals for example), cause then jump tables can be used (and the compiler does so for ordinals).

Okoba

  • Hero Member
  • *****
  • Posts: 528
Re: Case with string in Delphi mode
« Reply #22 on: March 18, 2022, 09:06:45 pm »
But with the upcoming anonymous functions or even with nested functions one could already create some generic case-like functionality.
I'd like to see how one can do it with anonymous functions or nested functions as cleanly or almost as cleanly as case.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6646
Re: Case with string in Delphi mode
« Reply #23 on: March 18, 2022, 09:45:54 pm »
You simply can't move things out of the language that are there already. The point is to be conservative to what is moved into the language instead.

But with an adequately-flexible base language one could some sort of general-purpose variadic syntax extension, and use that to reimplement case in a library. That's really no more outrageous than the idea of overloaded operators, which by my standards have come along fairly recently.

Quote
But with the upcoming anonymous functions or even with nested functions one could already create some generic case-like functionality.

Yes, much what was anticipating. Can you put exit/continue/etc. in an anonymous function?

MarkMLl
« Last Edit: March 18, 2022, 10:04:25 pm by MarkMLl »
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Leledumbo

  • Hero Member
  • *****
  • Posts: 8744
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Case with string in Delphi mode
« Reply #24 on: March 19, 2022, 09:02:59 am »
To be fair, case of with a string expression is absolutely a syntatic sugar for a sequence of if-statements in contrast to the one for ordinals which offers a real performance difference.
Wish somebody can create a prefix tree based code generation for this, it will no longer be a simple syntactic sugar and will provide actual performance benefits, at least if the code is frequently executed.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: [NO SOLUTION] Case with string in Delphi mode
« Reply #25 on: March 19, 2022, 11:10:29 am »
Hi!

This was my workaround in my Delphi times:

Code: Pascal  [Select][+][-]
  1. function CaseOfString (s: string; CaseLabels : TStringArray) : Integer;
  2. var  i: Integer;
  3. begin
  4. result := -1;
  5. for i := low (CaseLabels) to high(CaseLabels) do
  6.    begin
  7.    if s = CaseLabels[i] then
  8.       begin
  9.       result := i;
  10.       exit;
  11.       end;
  12.    end;
  13. end;
  14.  

Winni

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: Case with string in Delphi mode
« Reply #26 on: March 19, 2022, 04:46:16 pm »
But with the upcoming anonymous functions or even with nested functions one could already create some generic case-like functionality.
I'd like to see how one can do it with anonymous functions or nested functions as cleanly or almost as cleanly as case.

It will never be “as cleanly” or “almost as cleanly”.

Essentially you need a function that maps your expression to an index and then an array of functions that contain the actions.

You simply can't move things out of the language that are there already. The point is to be conservative to what is moved into the language instead.

But with an adequately-flexible base language one could some sort of general-purpose variadic syntax extension, and use that to reimplement case in a library. That's really no more outrageous than the idea of overloaded operators, which by my standards have come along fairly recently.

FPC will definitely not go in this direction.

Quote
But with the upcoming anonymous functions or even with nested functions one could already create some generic case-like functionality.

Yes, much what was anticipating. Can you put exit/continue/etc. in an anonymous function?

An anonymous function can not influence the code flow of the surrounding code, cause the anonymous function can be passed outside the surrounding code's scope.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6646
Re: Case with string in Delphi mode
« Reply #27 on: March 19, 2022, 05:42:06 pm »
But with an adequately-flexible base language one could some sort of general-purpose variadic syntax extension, and use that to reimplement case in a library. That's really no more outrageous than the idea of overloaded operators, which by my standards have come along fairly recently.

FPC will definitely not go in this direction.

I know. I'm still entitled to hold that opinion.

Quote
Yes, much what was anticipating. Can you put exit/continue/etc. in an anonymous function?

An anonymous function can not influence the code flow of the surrounding code, cause the anonymous function can be passed outside the surrounding code's scope.

OK, so it can't really replace a case. I was hoping it could effectively be inlined, which could be reasonably expected to be able to alter control flow.

After all, a macro expansion can alter control flow and you're the one who repeatedly says that generics etc. are an adequate alternative to macros.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: Case with string in Delphi mode
« Reply #28 on: March 20, 2022, 12:11:01 pm »
Quote
Yes, much what was anticipating. Can you put exit/continue/etc. in an anonymous function?

An anonymous function can not influence the code flow of the surrounding code, cause the anonymous function can be passed outside the surrounding code's scope.

OK, so it can't really replace a case. I was hoping it could effectively be inlined, which could be reasonably expected to be able to alter control flow.

An inlined function can't influence the code flow of the function it's inlined into either (aside from exceptions, but those work the same as if the inlined function was called). And while the compiler currently can't inline function pointers there is nothing in principle that would stop it from inlining functions passed in as parameters as long as the function that is being called is inlined as well.

After all, a macro expansion can alter control flow and you're the one who repeatedly says that generics etc. are an adequate alternative to macros.

I consider generics an adequate alternative to macros, because I consider marcos as an adequate solution in only a very narrow set of usecases.

Thaddy

  • Hero Member
  • *****
  • Posts: 14157
  • Probably until I exterminate Putin.
Re: Case with string in Delphi mode
« Reply #29 on: March 20, 2022, 01:18:07 pm »
I consider generics an adequate alternative to macros, because I consider marcos as an adequate solution in only a very narrow set of usecases.
FPC Macros - with its halfway implementation, no parameters - you mean? Even those are more powerful than many people think. But I agree on your remark regarding generics.
(My C++ work relies on its macro features, bad, but it does)
« Last Edit: March 20, 2022, 01:46:52 pm by Thaddy »
Specialize a type, not a var.

 

TinyPortal © 2005-2018