Recent

Author Topic: procedure doesn't match class declaration but still works ?  (Read 2282 times)

Joanna

  • Hero Member
  • *****
  • Posts: 1376
I have a unit that has a class that is a descendant of button and in the class i declare a procedure with a default parameter like this
Code: Pascal  [Select][+][-]
  1.  PUBLIC { public declarations }
  2.     PROCEDURE INIT (CONST ATABLENAME,ACOLNAME:ShortString;CONST WANT_TO_TOGGLE:BOOLEAN;
  3.                     CONST UPDATE_TYPE:BOOLEAN = FALSE);
however it seems to compile and work ok if i have this
Code: Pascal  [Select][+][-]
  1.   PROCEDURE TUPDATE_BOOL_COL_BTN.INIT( CONST ATABLENAME, ACOLNAME: SHORTSTRING;CONST WANT_TO_TOGGLE: BOOLEAN; CONST UPDATE_TYPE:BOOLEAN{ = FALSE}) ;
the value seems to be correct default value of false. i am wondering why it tolerates this inconsistent procedure declaration though. is this intentional? wouldn't it be better to have compiler catch this error?

Code: Pascal  [Select][+][-]
  1.  PROCEDURE TUPDATE_BOOL_COL_BTN.INIT( CONST ATABLENAME, ACOLNAME: SHORTSTRING;
  2.                                      CONST WANT_TO_TOGGLE: BOOLEAN{; CONST UPDATE_TYPE:BOOLEAN = FALSE}) ;  
the above code gives the errors

BUTTONS_2023.PAS(179,32) Error: function header doesn't match any method of this class "INIT(const ShortString;const ShortString;const Boolean);"
BUTTONS_2023.PAS(55,15) Error: Found declaration: INIT(const ShortString;const ShortString;const Boolean;const Boolean=`FALSE`);
BUTTONS_2023.PAS(185,16) Error: Identifier not found "UPDATE_TYPE"

so it will complain if the parameter is missing but not if it is a mismatch with class declaration

Thaddy

  • Hero Member
  • *****
  • Posts: 17413
  • Ceterum censeo Trumpum esse delendum (Tnx Charlie)
Re: procedure doesn't match class declaration but still works ?
« Reply #1 on: July 05, 2023, 02:15:20 pm »
It is insane to have a procedure called init, because that is for an old school (object) constructor. (although technically it is allowed in many modes)
I suggest to simply rename it and check your code. I also suggest to change the SHOUTING formatting to proper case. What you showed is really old Pascal style (probably 35 years +) and we need to see more code to properly help you. Don't worry, usually can be fixed.
Also look at the mode you are trying to compile: that can make a huge difference....
« Last Edit: July 05, 2023, 02:20:15 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

egsuh

  • Hero Member
  • *****
  • Posts: 1623
Re: procedure doesn't match class declaration but still works ?
« Reply #2 on: July 05, 2023, 02:19:59 pm »
In the implementation section, you may drop the initial values, if you defined initial values in the interface section. 

You can call the function in your codes without the variable that have initial value, but not as implementation.

Thaddy

  • Hero Member
  • *****
  • Posts: 17413
  • Ceterum censeo Trumpum esse delendum (Tnx Charlie)
Re: procedure doesn't match class declaration but still works ?
« Reply #3 on: July 05, 2023, 02:21:53 pm »
@egsuh
Better read my reply. You are suggesting nonsense.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

WooBean

  • Sr. Member
  • ****
  • Posts: 292
Platforms: Win7/64, Linux Mint 22.1 Xia

egsuh

  • Hero Member
  • *****
  • Posts: 1623
Re: procedure doesn't match class declaration but still works ?
« Reply #5 on: July 05, 2023, 02:31:32 pm »
Quote
Better read my reply. You are suggesting nonsense.

I'm answering OP's question. His point seems that in the declaration he added

       procedure Init (....   = false)

while in implementation,

       procedure TUPDATE_BOOL_COL_BTN.Init (.... ; {  = false})  // without default value

And he asserts that this shouldn't allowed.

Thaddy

  • Hero Member
  • *****
  • Posts: 17413
  • Ceterum censeo Trumpum esse delendum (Tnx Charlie)
Re: procedure doesn't match class declaration but still works ?
« Reply #6 on: July 05, 2023, 02:34:46 pm »
No it is about procedure init and it is very, very old code. All other assumptions are wrong.
Please stop bogus answers: OpenAI, chatGPT may be confused.
It is probably just a mode issue.
( In TP mode a constructor must be called init and this is definitely TP style code )
« Last Edit: July 05, 2023, 02:38:32 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

paweld

  • Hero Member
  • *****
  • Posts: 1434
Re: procedure doesn't match class declaration but still works ?
« Reply #7 on: July 05, 2023, 02:42:02 pm »
@Thaddy: @egsuh write well, and the formatting and name of the procedure is not at all relevant to the question.
Best regards / Pozdrawiam
paweld

Thaddy

  • Hero Member
  • *****
  • Posts: 17413
  • Ceterum censeo Trumpum esse delendum (Tnx Charlie)
Re: procedure doesn't match class declaration but still works ?
« Reply #8 on: July 05, 2023, 02:45:04 pm »
IT IS. Bloody read it. And bloody well know TP mode. (Silly bunch  >:D >:D)
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

paweld

  • Hero Member
  • *****
  • Posts: 1434
Re: procedure doesn't match class declaration but still works ?
« Reply #9 on: July 05, 2023, 03:01:47 pm »
TP mode does not allow default values.In addition, in the question there is information about the classes, which in TP mode you also do not experience.
Best regards / Pozdrawiam
paweld

korba812

  • Sr. Member
  • ****
  • Posts: 476
Re: procedure doesn't match class declaration but still works ?
« Reply #10 on: July 05, 2023, 03:13:29 pm »
It is insane to have a procedure called init, because that is for an old school (object) constructor. (although technically it is allowed in many modes)
I suggest to simply rename it and check your code. I also suggest to change the SHOUTING formatting to proper case. What you showed is really old Pascal style (probably 35 years +) and we need to see more code to properly help you. Don't worry, usually can be fixed.
Also look at the mode you are trying to compile: that can make a huge difference....
The object constructor can have any name and the name "Init" has no special meaning for the compiler, even in TP mode or in TP itself.

Joanna

  • Hero Member
  • *****
  • Posts: 1376
Re: procedure doesn't match class declaration but still works ?
« Reply #11 on: July 05, 2023, 03:31:37 pm »
In the implementation section, you may drop the initial values, if you defined initial values in the interface section. 

You can call the function in your codes without the variable that have initial value, but not as implementation.

This seems to be the case however I would appreciate a compiler warning that tells me about the mismatch. When looking at the procedure in implementation section it isn’t obvious that the last parameter is a default value or what the value will be. I find this very misleading.


Joanna

  • Hero Member
  • *****
  • Posts: 1376
Re: procedure doesn't match class declaration but still works ?
« Reply #12 on: July 05, 2023, 03:40:15 pm »
No it is about procedure init and it is very, very old code. All other assumptions are wrong.
Please stop bogus answers: OpenAI, chatGPT may be confused.
It is probably just a mode issue.
( In TP mode a constructor must be called init and this is definitely TP style code )
I assure you this code is brand new I just wrote it today using Lazarus   8)

Constructors are called CREATE as far as I’m concerned..
« Last Edit: July 05, 2023, 03:47:47 pm by Joanna »

Thaddy

  • Hero Member
  • *****
  • Posts: 17413
  • Ceterum censeo Trumpum esse delendum (Tnx Charlie)
Re: procedure doesn't match class declaration but still works ?
« Reply #13 on: July 05, 2023, 04:10:20 pm »
No, Joanna, with due respect,"constructors must be called init", depending on mode.
If that is new code, I really wonder what you are doing....
( don't call a procedure init , unless you know what you are doing and know the modes.)
« Last Edit: July 05, 2023, 04:13:06 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Joanna

  • Hero Member
  • *****
  • Posts: 1376
Re: procedure doesn't match class declaration but still works ?
« Reply #14 on: July 05, 2023, 04:24:15 pm »
No, Joanna, with due respect,"constructors must be called init", depending on mode.
If that is new code, I really wonder what you are doing....
( don't call a procedure init , unless you know what you are doing and know the modes.)

I recall turbo pascal object constructors were called init and destructors were called done.

This isn’t an object it’s a class descendant of tcdbutton. I wasn’t thinking of old style stuff when I named it I was thinking of initialize ... I do not ever plan to return to using turbo pascal nor share this code with anyone who uses turbo pascal. Out of curiosity What is the worst thing that could happen with this being called procedure init?

You will be happy to know that nothing in my project is permanent , I’m constantly redoing..  the lifespan of current code probably won’t be that long...

 

TinyPortal © 2005-2018