Re: OOP syntax help

2022-01-07 Thread Duke Normandin
On Sat, 8 Jan 2022 11:44:17 +1100
Chris Angelico  wrote:

[snip]

> > Thanks for clearing that up. I also notice that class
> > declarations are called "programs" - not to be confused with
> > the "main" program to be executed. Is that a correct
> > interpretation?  If a class is declared within a program, in
> > Pike its a "program" within a program? :?
>
> (Redirecting back to the list.)

Sorry about that!

> It's a bit of history from LPC, and others would be better at
> explaining than I am, but my understanding is that it derives

No worries! _That_ is a muddle I don't want to get into after the
little bit that you et out of the bag. :-)

--
Duke
** Text only please. Bottom post is best for me **



Re: OOP syntax help

2022-01-07 Thread Chris Angelico
On Sat, Jan 8, 2022 at 11:12 AM Duke Normandin  wrote:
>
> On Sat, 8 Jan 2022 10:58:53 +1100
> Chris Angelico  wrote:
>
> [snip]
>
> > > "animal my_dog = animal(); ?? It seems to me that this should be
> > > the cloning statement to create a particular object of the
> > > animal class?
> >
> > This is the combination of the previous two lines: declaring
> > my_dog and also constructing a dog.
>
> This was what confusing me. Makes sense now that I know the
> statement is a combo of the two preceding ones - like declaring and
> initialising a variable all one statement in some languages.
>
> [snip]
>
> Thanks for clearing that up. I also notice that class declarations
> are called "programs" - not to be confused with the "main" program
> to be executed. Is that a correct interpretation?  If a class is
> declared within a program, in Pike its a "program" within a
> program? :?

(Redirecting back to the list.)

It's a bit of history from LPC, and others would be better at
explaining than I am, but my understanding is that it derives from the
fact that your whole script is itself a class definition. In fact, you
can actually clone a program by its file name - there's a standard way
to cast a string to a program (or directly to an object), which loads
the script from disk and parses it.

object parsevdf = (object)"parsevdf.pike";

The file "parsevdf.pike" contains a program, by any definition of the
word. According to Pike, it contains the definition of a class - which
means that this class is a program.

ChrisA



Re: OOP syntax help

2022-01-07 Thread Chris Angelico
On Sat, Jan 8, 2022 at 10:46 AM Duke Normandin  wrote:
>
> I've /never/ been fond of OOP, but if I'm to continue with Pike, I
> figure I should at least try to understand Pike's very of OOP.
>
> From: https://pike.lysator.liu.se/docs/tut/oop/creation_and_usage.md
>
> [quote]
> Creating and Using Objects
>
> Assuming that we have the class animal, we can define some
> variables that can be used to store animals. Remember that the
> class is also a data type. We can also create some animals to put
> in those variables. To create an animal, we use the syntax
> *classname*(), i e the name of the class followed by a pair of
> parentheses.
>
> animal some_animal;
> some_animal = animal();
> animal my_dog = animal();
> [/quote]
>
> The three statements are confusing to me.
> So a class called "animal" has been created and exists! OK ..
>
> "animal some_animal;" ?? Are we cloning the class here to create an
> object?

At that point, you're just declaring a variable, same as saying "int
some_number;" or "array some_collection;". Since you don't initialize
it, it defaults to 0 (specifically UNDEFINED, the special zero with
that flag).

> "some_animal = animal(); ??  So what's this than?

This is actually cloning the animal program, or instantiating the
animal class, or whatever you want to call it. This is actually
constructing an animal and assigning it to some_animal.

> "animal my_dog = animal(); ?? It seems to me that this should be
> the cloning statement to create a particular object of the animal
> class?

This is the combination of the previous two lines: declaring my_dog
and also constructing a dog.

> It's all a bloody muddle to me. I'm sure that I must be reading it
> wrongly.

The thing to bear in mind is that variable declarations are just a
hint saying "hey, this thing should store this type of thing". It
doesn't actually construct anything. You could put a generic "object"
there instead (or even "mixed") and it would behave the same way:

object some_animal;
some_animal = animal();
object my_dog = animal();

Calling a type constructs an object, just as it does in Python (and in
JavaScript if you use the "new" keyword with the call). You can then
assign that object to any compatible variable; notably, a variable
that wants a Foo can accept any object that inherits Foo (or if you
like: an object counts as all of its supertypes).

ChrisA



OOP syntax help

2022-01-07 Thread Duke Normandin
I've /never/ been fond of OOP, but if I'm to continue with Pike, I
figure I should at least try to understand Pike's very of OOP.

From: https://pike.lysator.liu.se/docs/tut/oop/creation_and_usage.md

[quote]
Creating and Using Objects

Assuming that we have the class animal, we can define some
variables that can be used to store animals. Remember that the
class is also a data type. We can also create some animals to put
in those variables. To create an animal, we use the syntax
*classname*(), i e the name of the class followed by a pair of
parentheses.

animal some_animal;
some_animal = animal();
animal my_dog = animal();
[/quote]

The three statements are confusing to me.
So a class called "animal" has been created and exists! OK ..

"animal some_animal;" ?? Are we cloning the class here to create an
object?

"some_animal = animal(); ??  So what's this than?

"animal my_dog = animal(); ?? It seems to me that this should be
the cloning statement to create a particular object of the animal
class?

It's all a bloody muddle to me. I'm sure that I must be reading it
wrongly.
--
Duke
** Text only please. Bottom post is best for me **