[Pharo-project] Auto create new and initialize methods for new class?
Hi, is there an automated setting which creates default new and initialize methods for a new class? BTW, I'm using Pharo 2.0-one-click. Well, I must go and thanks in advance. -- * * *Think different and code well*, -Conrad
Dirty way: You can modify Class>>subclass:instanceVariableNames:classVariableNames:poolDictionaries:category: like this: subclass: t instanceVariableNames: f classVariableNames: d poolDictionaries: s category: cat "This is the standard initialization message for creating a new class as a subclass of an existing class (the receiver)." | newClass | newClass := (ClassBuilder new) superclass: self subclass: t instanceVariableNames: f classVariableNames: d poolDictionaries: s category: cat. newClass compile: 'initialize'. ^newClass. 2012/6/15 Conrad Taylor <conradwt@gmail.com>
Hi, is there an automated setting which creates default new and initialize methods for a new class? BTW, I'm using Pharo 2.0-one-click. Well, I must go and thanks in advance.
-- * * *Think different and code well*,
-Conrad
Yes, very dirty. 2012/6/15 camille teruel <camille.teruel@gmail.com>
Dirty way: You can modify Class>>subclass:instanceVariableNames:classVariableNames:poolDictionaries:category: like this:
subclass: t instanceVariableNames: f classVariableNames: d poolDictionaries: s category: cat "This is the standard initialization message for creating a new class as a subclass of an existing class (the receiver)." | newClass | newClass := (ClassBuilder new) superclass: self subclass: t instanceVariableNames: f classVariableNames: d poolDictionaries: s category: cat. newClass compile: 'initialize'. ^newClass.
2012/6/15 Conrad Taylor <conradwt@gmail.com>
Hi, is there an automated setting which creates default new and initialize methods for a new class? BTW, I'm using Pharo 2.0-one-click. Well, I must go and thanks in advance.
-- * * *Think different and code well*,
-Conrad
On Fri, Jun 15, 2012 at 4:09 PM, Conrad Taylor <conradwt@gmail.com> wrote:
Hi, is there an automated setting which creates default new and initialize methods for a new class?
why do you need to implement #new so often? It's kind of rare in the code I've read. -- Damien Cassou http://damiencassou.seasidehosting.st "Lambdas are relegated to relative obscurity until Java makes them popular by not having them." James Iry
For initialize, if you use Nautilus: - select your class - CMD+SHIFT+I enjoy :) Ben On Jun 15, 2012, at 11:05 AM, Damien Cassou wrote:
On Fri, Jun 15, 2012 at 4:09 PM, Conrad Taylor <conradwt@gmail.com> wrote:
Hi, is there an automated setting which creates default new and initialize methods for a new class?
why do you need to implement #new so often? It's kind of rare in the code I've read.
-- Damien Cassou http://damiencassou.seasidehosting.st
"Lambdas are relegated to relative obscurity until Java makes them popular by not having them." James Iry
On Fri, Jun 15, 2012 at 5:14 PM, Benjamin < benjamin.vanryseghem.pharo@gmail.com> wrote:
For initialize, if you use Nautilus:
- select your class - CMD+SHIFT+I
Ben, you should move to somewhere OUTSIDE the menu "analyze" or rename analyze ;)
enjoy :)
Ben
On Jun 15, 2012, at 11:05 AM, Damien Cassou wrote:
On Fri, Jun 15, 2012 at 4:09 PM, Conrad Taylor <conradwt@gmail.com> wrote:
Hi, is there an automated setting which creates default new and initialize methods for a new class?
why do you need to implement #new so often? It's kind of rare in the code I've read.
-- Damien Cassou http://damiencassou.seasidehosting.st
"Lambdas are relegated to relative obscurity until Java makes them popular by not having them." James Iry
-- Mariano http://marianopeck.wordpress.com
On Fri, Jun 15, 2012 at 8:14 AM, Benjamin < benjamin.vanryseghem.pharo@gmail.com> wrote:
For initialize, if you use Nautilus:
Ben, would you recommend installing Nautilus? If so, what's the installation steps?
- select your class - CMD+SHIFT+I
enjoy :)
Ben
On Jun 15, 2012, at 11:05 AM, Damien Cassou wrote:
On Fri, Jun 15, 2012 at 4:09 PM, Conrad Taylor <conradwt@gmail.com> wrote:
Hi, is there an automated setting which creates default new and initialize methods for a new class?
why do you need to implement #new so often? It's kind of rare in the code I've read.
-- Damien Cassou http://damiencassou.seasidehosting.st
"Lambdas are relegated to relative obscurity until Java makes them popular by not having them." James Iry
-- * * *Think different and code well*, -Conrad
On Fri, Jun 15, 2012 at 6:08 PM, Conrad Taylor <conradwt@gmail.com> wrote:
On Fri, Jun 15, 2012 at 8:14 AM, Benjamin < benjamin.vanryseghem.pharo@gmail.com> wrote:
For initialize, if you use Nautilus:
Ben, would you recommend installing Nautilus?
Of course he will, he is the author ;) BTW, I also do recommend it.
If so, what's the installation steps?
http://marianopeck.wordpress.com/2012/02/26/nautilus-the-reborn-of-the-syste...
- select your class
- CMD+SHIFT+I
enjoy :)
Ben
On Jun 15, 2012, at 11:05 AM, Damien Cassou wrote:
On Fri, Jun 15, 2012 at 4:09 PM, Conrad Taylor <conradwt@gmail.com> wrote:
Hi, is there an automated setting which creates default new and initialize methods for a new class?
why do you need to implement #new so often? It's kind of rare in the code I've read.
-- Damien Cassou http://damiencassou.seasidehosting.st
"Lambdas are relegated to relative obscurity until Java makes them popular by not having them." James Iry
-- * * *Think different and code well*,
-Conrad
-- Mariano http://marianopeck.wordpress.com
You should do that :) But in Pharo 2.0, it's the default browser. Ben On Jun 15, 2012, at 12:08 PM, Conrad Taylor wrote:
On Fri, Jun 15, 2012 at 8:14 AM, Benjamin <benjamin.vanryseghem.pharo@gmail.com> wrote: For initialize, if you use Nautilus:
Ben, would you recommend installing Nautilus? If so, what's the installation steps?
- select your class - CMD+SHIFT+I
enjoy :)
Ben
On Jun 15, 2012, at 11:05 AM, Damien Cassou wrote:
On Fri, Jun 15, 2012 at 4:09 PM, Conrad Taylor <conradwt@gmail.com> wrote:
Hi, is there an automated setting which creates default new and initialize methods for a new class?
why do you need to implement #new so often? It's kind of rare in the code I've read.
-- Damien Cassou http://damiencassou.seasidehosting.st
"Lambdas are relegated to relative obscurity until Java makes them popular by not having them." James Iry
--
Think different and code well,
-Conrad
On Fri, Jun 15, 2012 at 9:18 AM, Benjamin < benjamin.vanryseghem.pharo@gmail.com> wrote:
You should do that :)
But in Pharo 2.0, it's the default browser.
Ben
OK, I'm using Pharo-2.0-one-click so I'm good to go in regards to Nautilis.
On Jun 15, 2012, at 12:08 PM, Conrad Taylor wrote:
On Fri, Jun 15, 2012 at 8:14 AM, Benjamin < benjamin.vanryseghem.pharo@gmail.com> wrote:
For initialize, if you use Nautilus:
Ben, would you recommend installing Nautilus? If so, what's the installation steps?
- select your class - CMD+SHIFT+I
enjoy :)
Ben
On Jun 15, 2012, at 11:05 AM, Damien Cassou wrote:
On Fri, Jun 15, 2012 at 4:09 PM, Conrad Taylor <conradwt@gmail.com> wrote:
Hi, is there an automated setting which creates default new and initialize methods for a new class?
why do you need to implement #new so often? It's kind of rare in the code I've read.
-- Damien Cassou http://damiencassou.seasidehosting.st
"Lambdas are relegated to relative obscurity until Java makes them popular by not having them." James Iry
-- * * *Think different and code well*,
-Conrad
-- * * *Think different and code well*, -Conrad
On Fri, Jun 15, 2012 at 8:05 AM, Damien Cassou <damien.cassou@gmail.com>wrote:
On Fri, Jun 15, 2012 at 4:09 PM, Conrad Taylor <conradwt@gmail.com> wrote:
Hi, is there an automated setting which creates default new and initialize methods for a new class?
why do you need to implement #new so often? It's kind of rare in the code I've read.
Hi, I would like to initialize the objects I create with some reasonable defaults and I don't want to have setters and getters for every instance variable. --
Damien Cassou http://damiencassou.seasidehosting.st
"Lambdas are relegated to relative obscurity until Java makes them popular by not having them." James Iry
-- * * *Think different and code well*, -Conrad
On Jun 15, 2012, at 5:48 PM, Conrad Taylor wrote:
On Fri, Jun 15, 2012 at 8:05 AM, Damien Cassou <damien.cassou@gmail.com> wrote: On Fri, Jun 15, 2012 at 4:09 PM, Conrad Taylor <conradwt@gmail.com> wrote:
Hi, is there an automated setting which creates default new and initialize methods for a new class?
why do you need to implement #new so often? It's kind of rare in the code I've read.
Hi, I would like to initialize the objects I create with some reasonable defaults and I don't want to have setters and getters for every instance variable.
#new calls #initialize by default. So the only thing you need to implement is the #initialize method on your class. Marcus -- Marcus Denker -- http://marcusdenker.de
On Fri, Jun 15, 2012 at 8:05 AM, Damien Cassou <damien.cassou@gmail.com>wrote:
On Fri, Jun 15, 2012 at 4:09 PM, Conrad Taylor <conradwt@gmail.com> wrote:
Hi, is there an automated setting which creates default new and initialize methods for a new class?
why do you need to implement #new so often? It's kind of rare in the code I've read.
So, how do you create a new instance of a class?
-- Damien Cassou http://damiencassou.seasidehosting.st
"Lambdas are relegated to relative obscurity until Java makes them popular by not having them." James Iry
-- * * *Think different and code well*, -Conrad
On Fri, Jun 15, 2012 at 5:51 PM, Conrad Taylor <conradwt@gmail.com> wrote:
I would like to initialize the objects I create with some reasonable defaults and I don't want to have setters and getters for every instance variable. So, how do you create a new instance of a class?
don't touch #new. Just implement #initialize and set your fields there. To everyone on the mailing list: we should encourage people to ask such kind of questions on stackoverflow.com. -- Damien Cassou http://damiencassou.seasidehosting.st "Lambdas are relegated to relative obscurity until Java makes them popular by not having them." James Iry
Damien Cassou wrote
we should encourage people to ask such kind of questions on stackoverflow.com.
+1 -- View this message in context: http://forum.world.st/Auto-create-new-and-initialize-methods-for-new-class-t... Sent from the Pharo Smalltalk mailing list archive at Nabble.com.
Sent from my iPhone On Jun 15, 2012, at 11:43 AM, Damien Cassou <damien.cassou@gmail.com> wrote:
On Fri, Jun 15, 2012 at 5:51 PM, Conrad Taylor <conradwt@gmail.com> wrote:
I would like to initialize the objects I create with some reasonable defaults and I don't want to have setters and getters for every instance variable. So, how do you create a new instance of a class?
don't touch #new. Just implement #initialize and set your fields there.
To everyone on the mailing list: we should encourage people to ask such kind of questions on stackoverflow.com.
Damien, this is a great point and I'll ask such questions there in the future. Think different and code well, -Conrad
-- Damien Cassou http://damiencassou.seasidehosting.st
"Lambdas are relegated to relative obscurity until Java makes them popular by not having them." James Iry
why do you need an automatic new method? Because by default new invoke initialize and it works well. On Jun 15, 2012, at 4:09 PM, Conrad Taylor wrote:
Hi, is there an automated setting which creates default new and initialize methods for a new class? BTW, I'm using Pharo 2.0-one-click. Well, I must go and thanks in advance.
--
Think different and code well,
-Conrad
Sent from my iPhone On Jun 15, 2012, at 12:06 PM, Stéphane Ducasse <stephane.ducasse@inria.fr> wrote:
why do you need an automatic new method? Because by default new invoke initialize
and it works well.
Stéphane, I have been using VW Smalltalk and it creates new classes ,by default, with a #new method on the class side and a #initialize method on the instance side.
On Jun 15, 2012, at 4:09 PM, Conrad Taylor wrote:
Hi, is there an automated setting which creates default new and initialize methods for a new class? BTW, I'm using Pharo 2.0-one-click. Well, I must go and thanks in advance.
--
Think different and code well,
-Conrad
Conrad Taylor wrote
Stéphane, I have been using VW Smalltalk and it creates new classes ,by default, with a #new method on the class side and a #initialize method on the instance side.
In Pharo, new is built-in and calls initialize. I have a cool little hack that gives you the option to automatically create/add to #initialize as instVars are added. I'll post it in case that helps you... -- View this message in context: http://forum.world.st/Auto-create-new-and-initialize-methods-for-new-class-t... Sent from the Pharo Smalltalk mailing list archive at Nabble.com.
2012/6/16 Conrad Taylor <conradwt@gmail.com>:
Sent from my iPhone
On Jun 15, 2012, at 12:06 PM, Stéphane Ducasse <stephane.ducasse@inria.fr> wrote:
why do you need an automatic new method? Because by default   new invoke initialize
and it works well.
Stéphane, I have been using VW Smalltalk and it creates new classes ,by default, with a #new method on the class side and a #initialize method on the instance side.
Yes, true in VW, but unecessary in squeak/pharo, this is in Behavior>>new ^ self basicNew initialize Also, a generally good rule is to remember to send super initialize (very few exceptions). Nicolas
On Jun 15, 2012, at 4:09 PM, Conrad Taylor wrote:
Hi, is there an automated setting which creates default new and initialize methods for a new class? Â BTW, I'm using Pharo 2.0-one-click. Â Well, I must go and thanks in advance.
--
Think different and code well,
-Conrad
On Jun 15, 2012, at 12:06 PM, Stéphane Ducasse <stephane.ducasse@inria.fr> wrote:
why do you need an automatic new method? Because by default new invoke initialize
and it works well.
Stéphane, I have been using VW Smalltalk and it creates new classes ,by default, with a #new method on the class side and a #initialize method on the instance side.
yes I always hated hated so much to force teacher to kill student on metaclass altar after two weeks of smalltalk
2012/6/16 Stéphane Ducasse <stephane.ducasse@inria.fr>:
On Jun 15, 2012, at 12:06 PM, Stéphane Ducasse <stephane.ducasse@inria.fr> wrote:
why do you need an automatic new method? Because by default  new invoke initialize
and it works well.
Stéphane, I have been using VW Smalltalk and it creates new classes ,by default, with a #new method on the class side and a #initialize method on the instance side.
yes I always hated hated so much to force teacher to kill student on metaclass altar after two weeks of smalltalk
Another point of view: creating an instance is on class side because instances are created from a kind of structural template (how many variables, etc...). You can also create an instance by cloning with copy... But initialize is on instance size because it modifies the private instance variables: one object can't directly modify another, except by kindly asking by sending a message. The instance is an object, the class is another, so the class kindly ask the instance to initialize itself. There's no trick here and it can't be more clear. The only trick is how a class side new inherits from instance side Behavior new, but we don't need this to explain initialize... Nicolas
On Jun 16, 2012, at 11:00 AM, Nicolas Cellier wrote:
2012/6/16 Stéphane Ducasse <stephane.ducasse@inria.fr>:
On Jun 15, 2012, at 12:06 PM, Stéphane Ducasse <stephane.ducasse@inria.fr> wrote:
why do you need an automatic new method? Because by default new invoke initialize
and it works well.
Stéphane, I have been using VW Smalltalk and it creates new classes ,by default, with a #new method on the class side and a #initialize method on the instance side.
yes I always hated hated so much to force teacher to kill student on metaclass altar after two weeks of smalltalk
Another point of view: creating an instance is on class side
what is the class side? :)
because instances are created from a kind of structural template (how many variables, etc...). You can also create an instance by cloning with copyâ¦
I have been there. Teaching a lot during many years and there is no simple explanation: after two hours of lecture you do not want to explain metaclasses. Just because people forgot (CLOS did not do this mistake) to invoke an initialize method when creating an object. At the end I added in all my system the Behavior new>> self basicNew initialize and the students were happy and me too.
But initialize is on instance size because it modifies the private instance variables: one object can't directly modify another, except by kindly asking by sending a message. The instance is an object, the class is another, so the class kindly ask the instance to initialize itself.
There's no trick here and it can't be more clear. The only trick is how a class side new inherits from instance side Behavior new, but we don't need this to explain initialize...
Nicolas
On 16 June 2012 10:46, Stéphane Ducasse <stephane.ducasse@inria.fr> wrote:
On Jun 16, 2012, at 11:00 AM, Nicolas Cellier wrote:
2012/6/16 Stéphane Ducasse <stephane.ducasse@inria.fr>:
On Jun 15, 2012, at 12:06 PM, Stéphane Ducasse <stephane.ducasse@inria.fr> wrote:
why do you need an automatic new method? Because by default  new invoke initialize
and it works well.
Stéphane, I have been using VW Smalltalk and it creates new classes ,by default, with a #new method on the class side and a #initialize method on the instance side.
yes I always hated hated so much to force teacher to kill student on metaclass altar after two weeks of smalltalk
Another point of view: creating an instance is on class side
what is the class side? :)
because instances are created from a kind of structural template (how many variables, etc...). You can also create an instance by cloning with copyâ¦
I have been there. Teaching a lot during many years and there is no simple explanation: after two hours of lecture you do not want to explain metaclasses. Just because people forgot (CLOS did not do this mistake) to invoke an initialize method when creating an object.
Stéphane, would you mind expanding a bit on what mistake CLOS didn't make? frank
At the end I added in all my system the Behavior new>> self basicNew initialize and the students were happy and me too.
But initialize is on instance size because it modifies the private instance variables: one object can't directly modify another, except by kindly asking by sending a message. The instance is an object, the class is another, so the class kindly ask the instance to initialize itself.
There's no trick here and it can't be more clear. The only trick is how a class side new inherits from instance side Behavior new, but we don't need this to explain initialize...
Nicolas
Stéphane, would you mind expanding a bit on what mistake CLOS didn't make?
create-instance invokes initialize-instance in CLOS for every class. In Smalltalk (not squeak or pharo), new does not invoke initialize. Now CLOS arrived 10 years after Smalltalk and we fixed that problem in our lovely environment. Stef
On 16 June 2012 21:07, Stéphane Ducasse <stephane.ducasse@inria.fr> wrote:
Stéphane, would you mind expanding a bit on what mistake CLOS didn't make?
create-instance invokes initialize-instance in CLOS for every class.
In Smalltalk (not squeak or pharo), new does not invoke initialize. Now CLOS arrived 10 years after Smalltalk and we fixed that problem in our lovely environment.
OK, that's what I thought you meant. For instance, Objective-C repeats the mistake: [[MyClass alloc] init]. Thanks! frank
Stef
Anyway, we need to learn if we want to create things with something else than new... But I like basicNew initialize the way it is now. 2012/6/16 Stéphane Ducasse <stephane.ducasse@inria.fr>:
Stéphane, would you mind expanding a bit on what mistake CLOS didn't make?
create-instance invokes initialize-instance in CLOS for every class.
In Smalltalk (not squeak or pharo), new does not invoke initialize. Now CLOS arrived 10 years after Smalltalk and we fixed that problem in our lovely environment.
Stef
-- Philippe Back Dramatic Performance Improvements Mob: +32(0) 478 650 140 | Fax: +32 (0) 70 408 027 Mail: phil@highoctane.be | Web: http://philippeback.eu | Blog: http://philippeback.be High Octane SPRL rue cour Boisacq 101 1301 Bierges Belgium
In fact, a new instance IS initialized by VM, by putting nil values to each slot and zeroes in case of byte array. What happens next, when you sending #initialize? Yes, you rewriting these values by own, which sound like initial nil-out of all slots was unnecessary. So, in some cases it could be less wasteful (in terms of performance) to just make a copy of existing instance: SomeClass>>initialize preinitializedInstance := self basicNew initialize. SomeClass>>new ^ preinitializedInstance shallowCopy. and this actually a bit faster , except from, of course if #initialize also creating new objects, then cannot avoid extra initialization step. Also, often you cannot fully initialize object using just #initialize because it requires some additional arguments to initialize properly. For that many languages having so-called constructors, where you cannot obtain an instance with not properly initialized state because you have to use constructors when creating new instance. -- Best regards, Igor Stasenko.
2012/6/16 Stéphane Ducasse <stephane.ducasse@inria.fr>:
On Jun 16, 2012, at 11:00 AM, Nicolas Cellier wrote:
2012/6/16 Stéphane Ducasse <stephane.ducasse@inria.fr>:
On Jun 15, 2012, at 12:06 PM, Stéphane Ducasse <stephane.ducasse@inria.fr> wrote:
why do you need an automatic new method? Because by default  new invoke initialize
and it works well.
Stéphane, I have been using VW Smalltalk and it creates new classes ,by default, with a #new method on the class side and a #initialize method on the instance side.
yes I always hated hated so much to force teacher to kill student on metaclass altar after two weeks of smalltalk
Another point of view: creating an instance is on class side
what is the class side? :)
I would explain it this way, as viewed from a st-80 browser: - you select instance radio button, and see the messages you can send to instances of the class. - you select class radio button, and see the messages you can send to the class itself. Of course, it's not exactly that simple because of inheritance: the browser doesn't show the messages implemented in superclasses that we can send too. There are hierarchy browsers... Except they don't really help with class side inheritance, that's what make it complex for noobs IMO.
because instances are created from a kind of structural template (how many variables, etc...). You can also create an instance by cloning with copyâ¦
I have been there. Teaching a lot during many years and there is no simple explanation: after two hours of lecture you do not want to explain metaclasses. Just because people forgot (CLOS did not do this mistake) to invoke an initialize method when creating an object.
At the end I added in all my system the Behavior new>> self basicNew initialize and the students were happy and me too.
Sure, I also find this addition valuable beside teaching. Nicolas
But initialize is on instance size because it modifies the private instance variables: one object can't directly modify another, except by kindly asking by sending a message. The instance is an object, the class is another, so the class kindly ask the instance to initialize itself.
There's no trick here and it can't be more clear. The only trick is how a class side new inherits from instance side Behavior new, but we don't need this to explain initialize...
Nicolas
participants (12)
-
Benjamin -
camille teruel -
Conrad Taylor -
Damien Cassou -
Frank Shearar -
Igor Stasenko -
Marcus Denker -
Mariano Martinez Peck -
Nicolas Cellier -
phil@highoctane.be -
Sean P. DeNigris -
Stéphane Ducasse