Recent

Author Topic: Detect type of parameter  (Read 5662 times)

Bart

  • Hero Member
  • *****
  • Posts: 4349
    • Bart en Mariska's Webstek
Detect type of parameter
« on: June 22, 2021, 11:20:04 pm »
Hi,

Given:
Code: Pascal  [Select][+][-]
  1. procedure foo(var x);

Is it at all possible to detect the type of the paramater passed?
More specific: can you detect wether or not x is in fact of type string?

Context: a (silly) programming challenge.

Bart

440bx

  • Hero Member
  • *****
  • Posts: 2430
Re: Detect type of parameter
« Reply #1 on: June 22, 2021, 11:42:38 pm »
generally speaking, the answer is no.  because an untyped parameter is just a pointer to "something"/"anything".

That said, applying some heuristics and, depending on the type of parameter passed. it might be possible to reach a conclusion with a fairly high degree of confidence but no certainty.

Is your question driven by curiosity or are you dealing with a situation where you actually need to know the type of data that was passed as an untyped parameter ?
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

Bart

  • Hero Member
  • *****
  • Posts: 4349
    • Bart en Mariska's Webstek
Re: Detect type of parameter
« Reply #2 on: June 23, 2021, 12:03:00 am »
Just curiosity.

Bart

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 960
    • Lebeau Software
Re: Detect type of parameter
« Reply #3 on: June 23, 2021, 07:08:15 pm »
Given:
Code: Pascal  [Select][+][-]
  1. procedure foo(var x);

Is it at all possible to detect the type of the paramater passed?

More specific: can you detect wether or not x is in fact of type string?

No, 'x' receives only a memory address, it has no concept of what type it is pointing at.  That is why it is an "untyped" parameter.

If you really need to detect the parameter type, use Templates or Generics for that purpose.
« Last Edit: June 23, 2021, 11:37:49 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Bart

  • Hero Member
  • *****
  • Posts: 4349
    • Bart en Mariska's Webstek
Re: Detect type of parameter
« Reply #4 on: June 23, 2021, 07:24:31 pm »
If you really need to detect the parameter type, use Templates or Generics for that purpose.

The programming challenge unfortunately does not allow that.
The aim is to have a procedure with an untyped parameter, and then determine if that paramter is a string or not.
(I did not make up the rules).
So, likely not possible in Pascal, unlike you implement some heuristics to determine a string at the memory pointed to by the parameter.

Bart

winni

  • Hero Member
  • *****
  • Posts: 2496
Re: Detect type of parameter
« Reply #5 on: June 23, 2021, 08:22:57 pm »
Hi!

Some kind of adventure:

If x is an AnsiString then the length of it is to find at  x-4 as a DoubleWord.
If this results in qn reasonable value, then this might be an AnsiString.

Now you can test from x to x+length if these are valid UTF-8 chars.

Winni
 

Bart

  • Hero Member
  • *****
  • Posts: 4349
    • Bart en Mariska's Webstek
Re: Detect type of parameter
« Reply #6 on: June 23, 2021, 09:35:41 pm »
Well, the string of course might not be UTF8 (the challenge does not say anything about that).
A first check might be to check that the byte at length+1 = 0?

(If Windows codepage encoded strings are allowed, then one might argue that any content is valid, but the check for a terminating #0 must be fulfilled.)

Bart

Kays

  • Sr. Member
  • ****
  • Posts: 329
  • Whasup!?
    • KaiBurghardt.de
Re: Detect type of parameter
« Reply #7 on: June 23, 2021, 10:00:57 pm »
[…] The aim is to have a procedure with an untyped parameter, and then determine if that paramter is a string or not. […]
Yeah, theoretically, I guess, you could inspect debugging information, you know, but this will also require a program to be compiled with them, which I suppose your challenge does not allow. On the other hand this will actually allow you to write a function “is string”, and not just “is probably a string”.
Yours Sincerely
Kai Burghardt

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 9445
  • FPC developer.
Re: Detect type of parameter
« Reply #8 on: June 23, 2021, 10:28:59 pm »
I don't think e.g. C can safely do it either. e.g.

Code: [Select]
void something (void* p)
{
 // trying to detect if p is a string, will typically scan for \0 and crash after only acccessing a few bytes.
}

// play with blocksize to get a block that might have unallocated memory behind it. I bet here a 64-bit page, 8192 bytes for relatively straight forward (embedded) mallocs.
#define blocksize 8192

{
byte* myp=malloc(blocksize);

myp[blocksize-2]='A';
myp[blocksize-1]='B';
something(&myp[blocksize-2]);
}

If you try to see if this is a string, eventually you will access beyond the allocation into unallocated memory, which only takes two bytes worth of iteration, and get a nice SIGSEGV/Access violation for your trouble.

And while this is artificial, such patterns exist in code e.g. when the routine e.g. handling of  circular I/O buffers.

Debug info, checking if a pointer is in stack space or data space, accessing valid data memory is all implementation specific and not guaranteed by nearly any language.

« Last Edit: June 23, 2021, 10:31:50 pm by marcov »

Bart

  • Hero Member
  • *****
  • Posts: 4349
    • Bart en Mariska's Webstek
Re: Detect type of parameter
« Reply #9 on: June 23, 2021, 10:36:56 pm »
At least Fortran and Pyhton seem to be able to detect the type of an untyped parameter...

Ah well, we use Pascal for a reason  O:-)

Bart

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 9445
  • FPC developer.
Re: Detect type of parameter
« Reply #10 on: June 23, 2021, 11:05:50 pm »
At least Fortran and Pyhton seem to be able to detect the type of an untyped parameter...

It probably requires as much a wizard in those languages to judge them as to craft the examples to make sure no undefined or implementation defined information is used (iow is it an ugly hack that doesn't alway hold? )

I don't know either well enough.

My C example (that has a direct equivalent in Pascal) is a trap for such ugly hacks that usually hold, but are not 100% safe.

Of course Python as a _generally_ interpreted languages holds better cards. Languages that don't allow to pass partial allocations are probably also safer.

Quote
Ah well, we use Pascal for a reason  O:-)

I never liked this aspect of pascal. The untyped parameter is type compatible with everything but doesn't carry size. Array of byte and open arrays (Delphi style) in general also pass an associated size, but are not generally type compatible.

The missing combination is the Modula-2 untyped (const) ARRAY OF BYTE parameter that is a pointer (ADDRESS) and a size under the hood and is type compatible with everything.

PascalDragon

  • Hero Member
  • *****
  • Posts: 3191
  • Compiler Developer
Re: Detect type of parameter
« Reply #11 on: June 24, 2021, 08:56:16 am »
At least Fortran and Pyhton seem to be able to detect the type of an untyped parameter...

I don't know about Fortran, but Python uses dynamic typing, so by definition you're able to determine the type of an "untyped" parameter, because it will still have a type attached to it. In languages like Pascal and C that is simply not the case.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 960
    • Lebeau Software
Re: Detect type of parameter
« Reply #12 on: June 24, 2021, 06:04:48 pm »
I don't know about Fortran, but Python uses dynamic typing, so by definition you're able to determine the type of an "untyped" parameter, because it will still have a type attached to it. In languages like Pascal and C that is simply not the case.

Same with other scripting languages.  Such dynamic typing would be akin to using Variant in Delphi/FreePascal, or std::variant/std::any in C++, though with a bit more flexibility since those languages are interpreted rather than compiled.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Bart

  • Hero Member
  • *****
  • Posts: 4349
    • Bart en Mariska's Webstek
Re: Detect type of parameter
« Reply #13 on: June 24, 2021, 06:51:52 pm »
I don't know about Fortran, but Python uses dynamic typing, so by definition you're able to determine the type of an "untyped" parameter, because it will still have a type attached to it. In languages like Pascal and C that is simply not the case.

Yes, so in those languages the concept of an "untyped" parameter in essence does not exist.
So, the programming challenge is a little bit bogus ;-)

At least in Pascal untyped really means untyped.

Bart

 

TinyPortal © 2005-2018