PROTOTYPE Improvement

Previous topic - Next topic

MrTAToad

Would be nice if PROTOTYPE could return a type as a return parameter, something like :

PROTOTYPE _ONPROCESS AS tResult:speed

At the moment, it generates a compiler error.

mentalthink

Hi MRTatoad, the fact for make the prototype of a function... it´s like declare a variable?¿... I don´t understand too much reading C++ books, says it´s obligatory (other not), but it´s only like a "visual Help"

MrTAToad

The type would be known though - after all you can pass types as a parameter :)

In addition, it would be nice if you could pass a function directly rather than having to use a variable.

mentalthink


MrTAToad


Slydog

Quote from: MrTAToad on 2012-Jul-03
In addition, it would be nice if you could pass a function directly rather than having to use a variable.

+1  :good:
Otherwise it makes your code less 'clean' and harder to manage.
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

bigsofty

You know, this is one of those commands I have never touched. I had to do a quick manual lookup to check out the details. Although I understand its function, what is the practical use?

The suggestions that Mr. T proposed sound perfectly logical to me too BTW.
Cheers,

Ian.

"It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration."
(E. W. Dijkstra)

Slydog

#7
I use it in my GUI library.
When defining a new button, for example, you can pass a function that will be called automatically when that button is clicked.

It could be used for a timer library that will call a specified function when the timer runs out.

It basically allows your libraries to have custom / user specified events.
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

MrTAToad

Quote from: Slydog on 2012-Jul-04
Quote from: MrTAToad on 2012-Jul-03
In addition, it would be nice if you could pass a function directly rather than having to use a variable.

+1  :good:
Otherwise it makes your code less 'clean' and harder to manage.
Yes - at the moment I have to stick all the function pointers in an array and then pass that to a function.

MrTAToad

Quote from: bigsofty on 2012-Jul-04
You know, this is one of those commands I have never touched. I had to do a quick manual lookup to check out the details. Although I understand its function, what is the practical use?

The suggestions that Mr. T proposed sound perfectly logical to me too BTW.
It is GLBasic's pointer type and thus is only really used for advanced situations...

Slydog

#10
Here's some code to illustrate:

Subset of my MainMenu module:
Code (glbasic) Select
GLOBAL fn_button_options AS PGui_Button_Click
fn_button_options = MenuMain_Options
guiMain.ButtonAdd("Options", button.x, button.y, button.width, fn_button_options)

FUNCTION MenuMain_Options: state%
GameState(STATE_MENU_OPTIONS)
ENDFUNCTION


In my GUI library:
Code (glbasic) Select
PROTOTYPE PGui_Button_Click: id%

FUNCTION ButtonAdd%: caption$, x%, y%, w%, fn_click AS PGui_Button_Click
LOCAL button AS TGui_Button
button.Set(caption$, x, y, w, fn_click)
DIMPUSH self.buttons[], button
ENDFUNCTION


[Edit] This is how it would look in the MainMenu module if you could pass a function directly (no need for the 'fn_button_options' variable):
Code (glbasic) Select
guiMain.ButtonAdd("Options", button.x, button.y, button.width, MenuMain_Options)

FUNCTION MenuMain_Options: state%
GameState(STATE_MENU_OPTIONS)
ENDFUNCTION
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

bigsofty

Ah, that quite clear now. It could be very handy in many situations, I am thinking of possibly a quick'n'dirty way of emulating function overloading esp.

It reminds me of ALIAS for some reason too, even though that command only deals with variables.

Thanks for the info. :)
Cheers,

Ian.

"It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration."
(E. W. Dijkstra)

MrTAToad

It can be rather useful.  To expand on the demo above, currently I have to use :

Code (glbasic) Select
module.OnProgramStart=titleOnProgramStart
module.OnProgramEnd=titleOnProgramEnd
module.OnResolutionChange=titleOnResolutionChange
module.OnDisplay3D=titleOnDisplay3D
module.OnDisplay2D=titleOnDisplay2D
module.OnRoutineStart=titleOnRoutineStart
module.OnRoutineEnd=titleOnRoutineEnd
module.OnProcess=titleOnProcess
module.OnLanguageChange=titleOnLanguageChange
IF _setup.RegisterModule("title",module)=TRUE
module.OnProgramStart=activatePlayersOnProgramStart
module.OnProgramEnd=activatePlayersOnProgramEnd
module.OnResolutionChange=activatePlayersOnResolutionChange
module.OnDisplay3D=activatePlayersOnDisplay3D
module.OnDisplay2D=activatePlayersOnDisplay2D
module.OnProcess=activatePlayersOnProcess
module.OnRoutineStart=activatePlayersOnRoutineStart
module.OnRoutineEnd=activatePlayersOnRoutineEnd
module.OnLanguageChange=activatePlayersOnLanguageChange
IF _setup.RegisterModule("activateplayers",module)=TRUE
module.OnProgramStart=mainProgramOnProgramStart
module.OnProgramEnd=mainProgramOnProgramEnd
module.OnResolutionChange=mainProgramOnResolutionChange
module.OnDisplay3D=mainProgramOnDisplay3D
module.OnDisplay2D=mainProgramOnDisplay2D
module.OnProcess=mainProgramOnProcess
module.OnRoutineStart=mainProgramOnRoutineStart
module.OnRoutineEnd=mainProgramOnRoutineEnd
module.OnLanguageChange=mainProgramOnLanguageChange
IF _setup.RegisterModule("mainProgram",module)=TRUE
_setup.Main("#title")
ENDIF
ENDIF
ENDIF

Falstaff

Yeah I've been using Prototype for emulating 'event driven' code for my GUI stuffs as well. It's ok, although you can get some confusing errors sometimes when doing Prototype/Alias stuff.

MrTAToad

Yes, you have to be careful with it :)