Forum > Windows

The declaration of WriteFile, ReadFile win32 api may be incorrect.

<< < (6/7) > >>

tetrastes:

--- Quote from: PascalDragon on May 21, 2022, 04:52:00 pm ---What do you mean “by default”? There is no default, because it's overloaded inside the same unit and thus both are available to the user.

--- End quote ---
Yes, you are right. By "default" I meant that you see the declaration from redef.inc by placing cursor over WriteFileEx in Lazarus Source Editor, and it is not obvious how to find the second one.

440bx:

--- Quote from: PascalDragon on May 21, 2022, 04:52:00 pm ---If one knows how, it's perfectly fine to pass a Nil to a reference parameter:

--- End quote ---
I wouldn't go as far as saying it's "perfectly fine", I'd say it's definitely possible but, IMO, it should be strongly discouraged because it is completely reasonable for a procedure/function that uses "var" parameters to presume the parameters are _not_ nil.  Using any of the var parameters that got passed a nil value will cause an access violation.


MarkMLl:

--- Quote from: 440bx on May 21, 2022, 07:23:44 pm ---
--- Quote from: PascalDragon on May 21, 2022, 04:52:00 pm ---If one knows how, it's perfectly fine to pass a Nil to a reference parameter:

--- End quote ---
I wouldn't go as far as saying it's "perfectly fine", I'd say it's definitely possible but, IMO, it should be strongly discouraged because it is completely reasonable for a procedure/function that uses "var" parameters to presume the parameters are _not_ nil.  Using any of the var parameters that got passed a nil value will cause an access violation.

--- End quote ---

Sven saved me the trouble of digging out the message of a couple of weeks ago where he showed me how to do this.

I'd suggest that the bottom line is that (a) Pascal was designed when programs were basically still the expression of mathematical or logical formulae where (of course) there is no such thing as an optional parameter, and (b) the scope of software is now much wider so we have to admit that additional syntax and semantics are needed.

I have no problem with a var parameter being nullable (is that the right term?) provided it can be checked and/or an error caused by a reference can be caught.

Arguably, the same applies to both const and out parameters.

Let's face it, at least some of the above could be handled by procedure/function overloads which quite simply omit the relevant parameter, but Wirth didn't think of those either.

Finally, pragmatism dictates that modern Pascal's parameter conventions need to be compatible with C. Broadly speaking, var parameters are superior to pointers since they don't have to be explicitly dereferenced and aren't at risk from pointer arithmetic, and not allowing a var parameter to be nulled is a major omission.

MarkMLl

440bx:

--- Quote from: MarkMLl on May 21, 2022, 10:54:20 pm ---Finally, pragmatism dictates that modern Pascal's parameter conventions need to be compatible with C. Broadly speaking, var parameters are superior to pointers since they don't have to be explicitly dereferenced and aren't at risk from pointer arithmetic, and not allowing a var parameter to be nulled is a major omission.

MarkMLl

--- End quote ---
I completely agree that a computer language has to be compatible/accommodate C's parameter passing semantics but, I strongly believe that a "var" parameter should _not_ be nullable because, if that parameter is nullable then it should _not_ be a "var" parameter.

IOW, it is implicit that "var" is the address of a _variable_ and _not_ the address of "nothing".  if the parameter can be nil then it should be a "pointer" not a "var".

That's one of the many logical advantages of Pascal over C.  When a parameter is "var", the fact that it is "var" means that it cannot be null/nil.  That makes the usage of "var" informative.  IOW, "var" states that there exists a pre-condition that the parameter cannot be a null/nil pointer.

PascalDragon:

--- Quote from: 440bx on May 21, 2022, 07:23:44 pm ---
--- Quote from: PascalDragon on May 21, 2022, 04:52:00 pm ---If one knows how, it's perfectly fine to pass a Nil to a reference parameter:

--- End quote ---
I wouldn't go as far as saying it's "perfectly fine", I'd say it's definitely possible but, IMO, it should be strongly discouraged because it is completely reasonable for a procedure/function that uses "var" parameters to presume the parameters are _not_ nil.  Using any of the var parameters that got passed a nil value will cause an access violation.

--- End quote ---

You're right that the function in question should handle that correctly (which essentially all Pascal code doesn't because as you rightfully stated in your other post, var means not Nil), however for dealing with imported C functions that use var instead of a pointer and for which the parameter may indeed be NULL this is a valid trick to be used as from the PoV of the compiler no special magic is involved here.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version