Forum > Beginners

Procedure declaration before implementation section

<< < (2/4) > >>

MarkMLl:

--- Quote from: Zvoni on June 28, 2022, 02:17:26 pm ---.... or if you need a forward-declaration (for whatever reason) within the same unit?

--- End quote ---

I think I'd argue that you shouldn't put something in the interface section unless you really need to export it, in the same way that you shouldn't put something in the public section of a class unless you are prepared to take the consequences of its being called under conditions you didn't anticipate.

For OP's benefit: if you find yourself reading about forward declarations in anything other than the FPC documentation, different variants of Pascal (i.e. different compilers and potentially different modes) have two different conventions:

a) Some, notably older, define the parameter list at the point of the forward declaration and omit it where the procedure/function is actually defined.

b) Others require that both have an identical parameter list.

MarkMLl

Warfley:

--- Quote from: MarkMLl on June 28, 2022, 06:13:10 pm ---I think I'd argue that you shouldn't put something in the interface section unless you really need to export it, in the same way that you shouldn't put something in the public section of a class unless you are prepared to take the consequences of its being called under conditions you didn't anticipate.

--- End quote ---
While generally good advice, this only works until you start using generics:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---interface generic procedure Bar<T>; implementation procedure Foo;begin end; generic procedure Bar<T>;begin  Foo; //  unit1.pas(19,4) Error: Generic template in interface section references symbol in implementation sectionend;Here you have to export Foo in the interface because otherwise you can't use it in the generic function

pascal111:
It's good mentioning for "generic paradigm", I would ask if free pascal supporting this style of programming.

I think beginners tend to declare everything as public, in classes, functions, procedures, variables ... etc, this makes me remember the first days of BASIC programming language that everything there is public or global, we had no local data.

MarkMLl:

--- Quote from: pascal111 on June 29, 2022, 10:52:39 am ---It's good mentioning for "generic paradigm", I would ask if free pascal supporting this style of programming.

--- End quote ---

Why would he have given you an example if it didn't?

MarkMLl

pascal111:

--- Quote from: MarkMLl on June 29, 2022, 11:23:31 am ---
--- Quote from: pascal111 on June 29, 2022, 10:52:39 am ---It's good mentioning for "generic paradigm", I would ask if free pascal supporting this style of programming.

--- End quote ---

Why would he have given you an example if it didn't?

MarkMLl

--- End quote ---


"Why would he have given you an example if it didn't?!"  8)

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version