Lazarus
Free Pascal => General => Topic started by: Bart on June 22, 2021, 11:20:04 pm
-
Hi,
Given:
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
-
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 ?
-
Just curiosity.
Bart
-
Given:
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 (https://wiki.freepascal.org/Templates) or Generics (https://wiki.freepascal.org/Generics) for that purpose.
-
If you really need to detect the parameter type, use Templates (https://wiki.freepascal.org/Templates) or Generics (https://wiki.freepascal.org/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
-
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
-
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
-
[…] 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”.
-
I don't think e.g. C can safely do it either. e.g.
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.
-
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
-
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.
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.
-
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.
-
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.
-
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