Autore Topic: "Public" e "Private" delle Sub nei Moduli  (Letto 628 volte)

Offline vuott

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 11.266
  • Ne mors quidem nos iunget
    • Mostra profilo
"Public" e "Private" delle Sub nei Moduli
« il: 20 Dicembre 2012, 16:04:25 »
Vi riporto la discussione sorta nella M.L.I. circa l'argomento in oggetto:

« Hi,
just a question to understand this phenomenon better:

Why do event-SUBs have to be Public? It seems to me as if they don't run
if I make them Private.

For example, I have a module which handles an event-driven thing (like
printing). I want to have all SUBs and Functions within this module to
be invisible to the outside, and only those which directly communicate
to the outer world should be Public. Thus when I am in another part of
the program, the completion lists will only list those which actually
play a role to outside the module.

When I change myPrinter_Draw from Public to Private, it will not run
anymore, however (just tried this). So I wonder why this is so, or in
other words: why is it required to be Public to be found from within its
own module?

Thanks for your insights!

Rolf
»


« "Public" means "can be accessed from the outside". So, as an event
handler needs to be accessed from the outside by definition (the event
comes from another object), it has to be public.

--
Benoît Minisini
»


« It can be masked by adding an underscore at the beginning of the event name.

Fabien Bodard
»


« Errr - nope, doesn't run here:

Tried to change myPrinter_Draw() into _myPrinter_Draw(), and this is
dysfunctional :-)

Never mind - I can live with it...

Rolf
»


« The event comes from another object, I understand that, so that's why.
Thank you!

Rolf
»


« To give you more details:

In most languages, public / private / friend... are a mix of telling
which symbols are exported at runtime and some syntactic sugar.

In Gambas, I kept only the runtime definition, (which I could call the
real effective one).
"Public" means "accessible from another class", and
"Private" means the contrary.

And it's effective. If something is private, you won't see any symbol
for it in the object file (unless debugging symbols are enabled).

The only syntactic sugar is in the IDE : if a public symbol has an
underscore in it, it will be usually hidden in automatic completion or
debugging windows. But that's all. For the compiler and the interpreter,
it is as public as any other public symbol.

I did that for the sake of simplicity and clarity. Because people often
don't understand the difference between what is syntactic sugar
(implemented at the compiler level) and what is real (implemented at the
interpreter level).

Regards,

--
Benoît Minisini
»
« Ultima modifica: 09 Dicembre 2014, 13:40:23 da vuott »
« Chiunque, non ricorrendo lo stato di necessità, nel proprio progetto Gambas fa uso delle istruzioni Shell o Exec, è punito con la sanzione pecuniaria da euro 20,00 a euro 60,00. »

Offline vuott

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 11.266
  • Ne mors quidem nos iunget
    • Mostra profilo
Re: "Public" e "Private" delle Sub nei Moduli
« Risposta #1 il: 21 Dicembre 2012, 12:16:15 »
« Very interesting, yes that makes sense :-)

On the other hand, I don't understand why the interpreter doesn't find


  
Codice: gambas [Seleziona]
Public Sub _myPrinter_Draw()


anymore. Did I get that wrong?

Regards

Rolf
»


« Did you rename the actual object "myPrinter" to "_myPrinter", too? I assume
that this must be done...

Regards,
Tobi
»


« Aaaah yes - that was it:

Codice: gambas [Seleziona]
Private myPrinter As Printer



in a SUB somewhat later:

Codice: gambas [Seleziona]
myPrinter = New Printer As "_myPrinter"

myPrinter.Print()



BUT!

Codice: gambas [Seleziona]
Public Sub _myPrinter_Begin()


This way it works here, but the SUBs are still visible when the
completion list pops up in another module... Is that correct?

Regards

Rolf
»
« Ultima modifica: 09 Dicembre 2014, 13:37:29 da vuott »
« Chiunque, non ricorrendo lo stato di necessità, nel proprio progetto Gambas fa uso delle istruzioni Shell o Exec, è punito con la sanzione pecuniaria da euro 20,00 a euro 60,00. »