[Gambas-user] static ? in variables

Tobias Boege taboege at ...626...
Thu Jun 15 19:48:03 CEST 2017


On Thu, 15 Jun 2017, PICCORO McKAY Lenz wrote:
> thanks in advance, but i have now more questions:
> 2017-06-15 8:41 GMT-04:00 Tobias Boege <taboege at ...626...>:
> 
> > No, the word "singleton" here just means you get an automatic instance of
> > your class, i.e. you can use the class name as if it were an object. You
> >
> automatic instance?
> 
> maybe its a translation problem, but i try to understant: wiki said here:
> http://gambaswiki.org/wiki/lang/createstatic
> in a tip:
> "allows you to implement the object-oriented programming singleton pattern
> <http://en.wikipedia.org/wiki/Singleton_pattern>."
> and i understand by singleton pattern that a only one instance du i was a
> J2EE programer:
> "restricts the instantiation
> <https://en.wikipedia.org/wiki/Instantiation_%28computer_science%29> of a
> class <https://en.wikipedia.org/wiki/Class_%28computer_programming%29> to
> one object <https://en.wikipedia.org/wiki/Object_%28computer_science%29>.
> This is useful when exactly one object is needed to coordinate actions
> across the system"
> 
> so here are some things that i cannot understant and i think its something
> with translation or?
> 

It's not a translation error, it is that "singleton" has a different meaning
in the paragraph you cite than it has in Gambas. Actually, the documentation
should probably avoid the word "singleton", because it creates confusion with
a more established notion of "singleton" (the one you cite).

Now, let me tell you again what the documentation means when it says
"singleton". When you put the CREATE STATIC keyword into the top of your
class, then the class will be made "auto-creatable". This means that if
your Gambas program uses the class name like an object, e.g. by accessing
a non-static property or method over the *class* name (which doesn't make
sense), then the interpreter will create an object out of this class
(the so-called automatic instance -- because the interpreter creates it
automatically for you in the background as soon as you need it) and uses
this object in place of the class name.

CREATE STATIC does *not* ensure that you can create *only* one instance
of the class. This is not what "singleton" means in Gambas. You can create
multiple objects out of a CREATE STATIC class.

There are two famous examples of auto-creatable (CREATE STATIC) classes,
which I can think of off the top of my head:

  (1) Settings (gb.settings), if you ever worked with it, is a class of
      which you can create as many objects as you like, right? But you can
      also use the class name "Settings" as if it were an object, e.g.

        Print Settings["Key"]

      Here it looks you access a key "Key" of the class Settings, but this
      doesn't make sense as the array accessors aren't static methods of the
      Settings class. In fact, Settings, in this case, behaves as if you
      created as Settings object behind the scenes which is linked to the
      default configuration file path $XDG_CONFIG_HOME &/ Application.Name & ".conf".
      This is precisely the automatic instance of the Settings class.

  (2) An even more prominent example: every Form class is auto-creatable.
      Have you ever wondered why, if you start your GUI project with startup
      form "FMain", a window pops up? You only have a Form class but you
      never created an object of this Form class, so where does this window
      come from? This also is the automatic instance of your class FMain,
      showing itself on startup. You can also write

        FMain.Center()

      to center your main window on the screen. The class name "FMain"
      does not refer to the class, but to the automatic instance, an
      *object* of class FMain that the interpreter created and that is
      used behind the scenes.

This is the "singleton" behaviour that the documentation describes.

> 
> > Nothing extraordinary happens. You have a dynamic class with a static
> > variable and you can use the class like an object (as well as create new
> > objects of that class).
> >
> for the variable:
> 
> public statis var as string
> 
> vs
> 
> public var as strings
> 
> doc wiki said "will be shared with all the class" seems that there's no
> diference based on your answer
> 
> 

I don't understand your objection at all. Maybe the above explanation clears
things up for you?

Regards,
Tobi

-- 
"There's an old saying: Don't change anything... ever!" -- Mr. Monk




More information about the User mailing list