Difference between instances created with and without `new` in GNU SmalltalkDifferences between Smalltalk and python?Images or files in GNU Smalltalk?Difference between new and initialize in Smalltalk?How to subclass OrderedCollection in GNU Smalltalk?Smalltalk: Assigning to a Variable Without Declaring a Variable in GNU SmalltalkCan not call “function” in GNU SmalltalkUsing extended classes in gst (GNU smalltalk)?Importing a GNU Smalltalk project into Pharo?What is the difference between = and == in Pharo Smalltalk?Understanding GNU Smalltalk Closure
How can drunken, homicidal elves successfully conduct a wild hunt?
If you had a giant cutting disc 60 miles diameter and rotated it 1000 rps, would the edge be traveling faster than light?
Smooth switching between 12 V batteries, with a toggle switch
Why doesn’t a normal window produce an apparent rainbow?
Do any instruments not produce overtones?
Compiling c files on ubuntu and using the executable on Windows
Arriving at the same result with the opposite hypotheses
Is it possible to 'live off the sea'
What's up with this leaf?
Using "subway" as name for London Underground?
How to build suspense or so to establish and justify xenophobia of characters in the eyes of the reader?
What is the actual quality of machine translations?
How did they achieve the Gunslinger's shining eye effect in Westworld?
Can an Aarakocra use a shield while flying?
What are the peak hours for public transportation in Paris?
Soft question: Examples where lack of mathematical rigour cause security breaches?
Find the Factorial From the Given Prime Relationship
Russian equivalents of "no love lost"
Confusion about off peak timings of London trains
How Can I Tell The Difference Between Unmarked Sugar and Stevia?
"You've got another thing coming" - translation into French
How to project 3d image in the planes xy, xz, yz?
Can a user sell my software (MIT license) without modification?
Payment instructions allegedly from HomeAway look fishy to me
Difference between instances created with and without `new` in GNU Smalltalk
Differences between Smalltalk and python?Images or files in GNU Smalltalk?Difference between new and initialize in Smalltalk?How to subclass OrderedCollection in GNU Smalltalk?Smalltalk: Assigning to a Variable Without Declaring a Variable in GNU SmalltalkCan not call “function” in GNU SmalltalkUsing extended classes in gst (GNU smalltalk)?Importing a GNU Smalltalk project into Pharo?What is the difference between = and == in Pharo Smalltalk?Understanding GNU Smalltalk Closure
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
What is the difference between
Rectangle origin: 5@5 extent: 40@30
and
Rectangle new origin: 5@5 extent: 40@30
smalltalk gnu-smalltalk
add a comment |
What is the difference between
Rectangle origin: 5@5 extent: 40@30
and
Rectangle new origin: 5@5 extent: 40@30
smalltalk gnu-smalltalk
Friendly reminder to consider accepting one of the answers (or asking further clarification in comments).
– fede s.
Apr 10 at 13:56
add a comment |
What is the difference between
Rectangle origin: 5@5 extent: 40@30
and
Rectangle new origin: 5@5 extent: 40@30
smalltalk gnu-smalltalk
What is the difference between
Rectangle origin: 5@5 extent: 40@30
and
Rectangle new origin: 5@5 extent: 40@30
smalltalk gnu-smalltalk
smalltalk gnu-smalltalk
asked Mar 24 at 16:22
adiusadius
6,39542638
6,39542638
Friendly reminder to consider accepting one of the answers (or asking further clarification in comments).
– fede s.
Apr 10 at 13:56
add a comment |
Friendly reminder to consider accepting one of the answers (or asking further clarification in comments).
– fede s.
Apr 10 at 13:56
Friendly reminder to consider accepting one of the answers (or asking further clarification in comments).
– fede s.
Apr 10 at 13:56
Friendly reminder to consider accepting one of the answers (or asking further clarification in comments).
– fede s.
Apr 10 at 13:56
add a comment |
3 Answers
3
active
oldest
votes
Rectangle new origin: 5@5 extent: 40@30
creates a fully-initialized instance of a Rectangle (to be precise with all coordinates set to 0) and then sets its coordinates and extent with the origin:extend: accessor method.
Rectangle origin: 5@5 extent: 40@30
has the class Rectangle construct a Rectangle instance with the given attributes however it sees fit. In the case of GNU Smalltalk, it uses the basicNew message to allocate the Rectangle instance instead of new (see the source of Rectangle). That forgoes the "fully-initialized instance" state of the variant above: it skips any initialization and just allocates the memory (well, the GNU Smalltalk docs don't say so explicitly, but that is traditionally the purpose of basicNew). Then it uses the origin:extend: accessor to initialize the coordinates and extent of the new instance.
add a comment |
It's a matter of style. The Rectangle class provides a facility method for creating an instance, so that you can communicate directly with the class and write less code. It's also a good practice as you create the rectangle object with all it needs to work correctly (this practice is called RAII, resource acquisition is initialization). If you have a look at the source of Rectangle class>>#origin:extent:
you'll find something very much like
origin: aPoint extent: anotherPoint
^self new origin: aPoint extent: anotherPoint
So actually sending the message directly to the class o creating it manually and then setting it is in practice the same
I have a subclassRectangle subclass: SvgRect
and then it suddenly makes a difference. Withnew
it works, without it I getObject: SvgRect error: did not understand
errors.
– adius
Mar 24 at 17:00
@adius This is probably because of another distinction: in addition to #new, there is also a #basicNew and the latter is used by the #origin:extend: class method. github.com/bonzini/smalltalk/blob/… basicNew does not invoke initializer methods, so your "did not understand" errors are likely because the additional state you defined in your SvgRect is not initialized when you used the class method to construct your SvgRect instance.
– JayK
Mar 24 at 17:57
1
As @JayK says, the errors may occur because Rectangle sendsbasicNew
instead ofnew
, but also there has to be some other part of it. If I had to guess, I'd say that your SvgRect has defined aninitialize
method, so the problem is that using class sideorigin:extent:
won't work because, unlikenew
,basicNew
doesn't sendinitialize
to the object. If that is the case, you may want to redefineorigin:extent:
in the SvgRect to the same I wrote in the original answer to avoid errors.
– melkyades
Mar 24 at 21:33
@JayK @melkyades So then can you tell me what I need to change to get rid of thenew
in github.com/ad-si/svgtalk/blob/master/tests/SvgRectTest.st#L15 ?
– adius
Mar 30 at 9:36
@adius: You'd have to write(SvgRect origin: ... extent: ...) style: ...; yourself
, so the message #style: is not sent to the class but to the new object. Otherwise I see no pitfalls.
– JayK
Mar 31 at 9:18
|
show 1 more comment
I think it is important to note the difference between Smalltalk and other OO languages.
In other OO languages you have a construct called a constructor. Which enables you to automatically run certain code when you call method new
.
For example, in ruby you would do
# Class name
class ConstructorExample
# A constructor
def initialize
puts "I'm now running constructor"
end
end
# Creating Object
ConstructorExample.new # upon which calling you will get initialized run automatically.
The output would be in your shell:
> I'm now running constructor
In Smalltalk you have to differenciate between new
and basicNew
. (Sometimes even new
is only alias for basicNew
so you have to run initialize
manually
or create class method. The basicNew
does not execute the initialize automatically, the new
usually does (not all dialects!).
The above example could be then written as:
Object subclass:#ConstructorExample
instanceVariableNames:''
classVariableNames:''
poolDictionaries:''
category: ''
ConstructorExample>>initialize
Transcript showCR: 'I'm now running a constructor'
"You would then instantiate"
ConstructorExample new
or
| example |
example := ConstructorExample basicNew.
example initialize "sending method initialize like any other method"
In both cases the output would be (in your Transcript):
I'm now running a constructor
The main reason for that, in my eyes, is that you can run constructor after some of your custom code if you have class method
ConstructorExample class >> run
^ self basicNew; Transcript showCR: 'Running before constructor'; self initialize; yourself
Then you would simply do:
ConstructorExample run
The output would be:
Running before constructor
I'm now running a constructor
Now to your example
As JayK, melkyades explained the main differences I'll give more suttle differences (details) on those:
First:
Rectangle new origin: 5@5 extent: 40@30
What it actually does it this (without the Transcript showCR:
):
| myRactangle |
myRactangle := Ractangle new. "Creates an empty instance with origin: 0 @ 0 corner: 0 @ 0 and would execute initialize if there would be one."
Transcript showCR: 'Current origin: ', origin asString, 'with corner: ', corner asString. "You should see the zeros"
myRactangle origin: 5@5 extent: 40@30
Transcript showCR: 'Current origin: ', origin asString, 'with corner: ', corner asString. "You should your custom point(s)"
What happens when you do Ractangle new
?
Rectangle class >> new [
"Answer the (0 @ 0 corner: 0 @ 0) rectangle"
<category: 'instance creation'>
^self origin: 0 @ 0 corner: 0 @ 0
]
When you check the source code it sets origin: 0 @ 0 corner: 0 @ 0
(notice the difference setting it via ...corner:
and not extent:
).
Second:
Rectangle origin: 5@5 extent: 40@30
The source code:
Rectangle class >> origin: originPoint extent: extentPoint
"Answer a rectangle with the given origin and size"
<category: 'instance creation'>
^self basicNew origin: originPoint corner: originPoint + extentPoint
As pointed-out already there is a basicNew
which prevents the any initialize
constuctor to be run manually or via class method like I showed above.
What you could do it rewrite it if you needed. You would create your own rectangle class which would inherit it from Rectangle and rewrite it there.
For example:
Rectangle subclass:#ApplicationRectangle
instanceVariableNames:''
classVariableNames:''
poolDictionaries:''
category: ''
where you would define:
ApplicationRectangle class >> origin: originPoint extent: extentPoint
"Answer a rectangle with the given origin and size"
<category: 'instance creation'>
^self new origin: originPoint corner: originPoint + extentPoint
You would then call it:
ApplicationRectangle origin: 5@5 extent: 40@30
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55325909%2fdifference-between-instances-created-with-and-without-new-in-gnu-smalltalk%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Rectangle new origin: 5@5 extent: 40@30
creates a fully-initialized instance of a Rectangle (to be precise with all coordinates set to 0) and then sets its coordinates and extent with the origin:extend: accessor method.
Rectangle origin: 5@5 extent: 40@30
has the class Rectangle construct a Rectangle instance with the given attributes however it sees fit. In the case of GNU Smalltalk, it uses the basicNew message to allocate the Rectangle instance instead of new (see the source of Rectangle). That forgoes the "fully-initialized instance" state of the variant above: it skips any initialization and just allocates the memory (well, the GNU Smalltalk docs don't say so explicitly, but that is traditionally the purpose of basicNew). Then it uses the origin:extend: accessor to initialize the coordinates and extent of the new instance.
add a comment |
Rectangle new origin: 5@5 extent: 40@30
creates a fully-initialized instance of a Rectangle (to be precise with all coordinates set to 0) and then sets its coordinates and extent with the origin:extend: accessor method.
Rectangle origin: 5@5 extent: 40@30
has the class Rectangle construct a Rectangle instance with the given attributes however it sees fit. In the case of GNU Smalltalk, it uses the basicNew message to allocate the Rectangle instance instead of new (see the source of Rectangle). That forgoes the "fully-initialized instance" state of the variant above: it skips any initialization and just allocates the memory (well, the GNU Smalltalk docs don't say so explicitly, but that is traditionally the purpose of basicNew). Then it uses the origin:extend: accessor to initialize the coordinates and extent of the new instance.
add a comment |
Rectangle new origin: 5@5 extent: 40@30
creates a fully-initialized instance of a Rectangle (to be precise with all coordinates set to 0) and then sets its coordinates and extent with the origin:extend: accessor method.
Rectangle origin: 5@5 extent: 40@30
has the class Rectangle construct a Rectangle instance with the given attributes however it sees fit. In the case of GNU Smalltalk, it uses the basicNew message to allocate the Rectangle instance instead of new (see the source of Rectangle). That forgoes the "fully-initialized instance" state of the variant above: it skips any initialization and just allocates the memory (well, the GNU Smalltalk docs don't say so explicitly, but that is traditionally the purpose of basicNew). Then it uses the origin:extend: accessor to initialize the coordinates and extent of the new instance.
Rectangle new origin: 5@5 extent: 40@30
creates a fully-initialized instance of a Rectangle (to be precise with all coordinates set to 0) and then sets its coordinates and extent with the origin:extend: accessor method.
Rectangle origin: 5@5 extent: 40@30
has the class Rectangle construct a Rectangle instance with the given attributes however it sees fit. In the case of GNU Smalltalk, it uses the basicNew message to allocate the Rectangle instance instead of new (see the source of Rectangle). That forgoes the "fully-initialized instance" state of the variant above: it skips any initialization and just allocates the memory (well, the GNU Smalltalk docs don't say so explicitly, but that is traditionally the purpose of basicNew). Then it uses the origin:extend: accessor to initialize the coordinates and extent of the new instance.
answered Mar 24 at 18:07
JayKJayK
1,56611119
1,56611119
add a comment |
add a comment |
It's a matter of style. The Rectangle class provides a facility method for creating an instance, so that you can communicate directly with the class and write less code. It's also a good practice as you create the rectangle object with all it needs to work correctly (this practice is called RAII, resource acquisition is initialization). If you have a look at the source of Rectangle class>>#origin:extent:
you'll find something very much like
origin: aPoint extent: anotherPoint
^self new origin: aPoint extent: anotherPoint
So actually sending the message directly to the class o creating it manually and then setting it is in practice the same
I have a subclassRectangle subclass: SvgRect
and then it suddenly makes a difference. Withnew
it works, without it I getObject: SvgRect error: did not understand
errors.
– adius
Mar 24 at 17:00
@adius This is probably because of another distinction: in addition to #new, there is also a #basicNew and the latter is used by the #origin:extend: class method. github.com/bonzini/smalltalk/blob/… basicNew does not invoke initializer methods, so your "did not understand" errors are likely because the additional state you defined in your SvgRect is not initialized when you used the class method to construct your SvgRect instance.
– JayK
Mar 24 at 17:57
1
As @JayK says, the errors may occur because Rectangle sendsbasicNew
instead ofnew
, but also there has to be some other part of it. If I had to guess, I'd say that your SvgRect has defined aninitialize
method, so the problem is that using class sideorigin:extent:
won't work because, unlikenew
,basicNew
doesn't sendinitialize
to the object. If that is the case, you may want to redefineorigin:extent:
in the SvgRect to the same I wrote in the original answer to avoid errors.
– melkyades
Mar 24 at 21:33
@JayK @melkyades So then can you tell me what I need to change to get rid of thenew
in github.com/ad-si/svgtalk/blob/master/tests/SvgRectTest.st#L15 ?
– adius
Mar 30 at 9:36
@adius: You'd have to write(SvgRect origin: ... extent: ...) style: ...; yourself
, so the message #style: is not sent to the class but to the new object. Otherwise I see no pitfalls.
– JayK
Mar 31 at 9:18
|
show 1 more comment
It's a matter of style. The Rectangle class provides a facility method for creating an instance, so that you can communicate directly with the class and write less code. It's also a good practice as you create the rectangle object with all it needs to work correctly (this practice is called RAII, resource acquisition is initialization). If you have a look at the source of Rectangle class>>#origin:extent:
you'll find something very much like
origin: aPoint extent: anotherPoint
^self new origin: aPoint extent: anotherPoint
So actually sending the message directly to the class o creating it manually and then setting it is in practice the same
I have a subclassRectangle subclass: SvgRect
and then it suddenly makes a difference. Withnew
it works, without it I getObject: SvgRect error: did not understand
errors.
– adius
Mar 24 at 17:00
@adius This is probably because of another distinction: in addition to #new, there is also a #basicNew and the latter is used by the #origin:extend: class method. github.com/bonzini/smalltalk/blob/… basicNew does not invoke initializer methods, so your "did not understand" errors are likely because the additional state you defined in your SvgRect is not initialized when you used the class method to construct your SvgRect instance.
– JayK
Mar 24 at 17:57
1
As @JayK says, the errors may occur because Rectangle sendsbasicNew
instead ofnew
, but also there has to be some other part of it. If I had to guess, I'd say that your SvgRect has defined aninitialize
method, so the problem is that using class sideorigin:extent:
won't work because, unlikenew
,basicNew
doesn't sendinitialize
to the object. If that is the case, you may want to redefineorigin:extent:
in the SvgRect to the same I wrote in the original answer to avoid errors.
– melkyades
Mar 24 at 21:33
@JayK @melkyades So then can you tell me what I need to change to get rid of thenew
in github.com/ad-si/svgtalk/blob/master/tests/SvgRectTest.st#L15 ?
– adius
Mar 30 at 9:36
@adius: You'd have to write(SvgRect origin: ... extent: ...) style: ...; yourself
, so the message #style: is not sent to the class but to the new object. Otherwise I see no pitfalls.
– JayK
Mar 31 at 9:18
|
show 1 more comment
It's a matter of style. The Rectangle class provides a facility method for creating an instance, so that you can communicate directly with the class and write less code. It's also a good practice as you create the rectangle object with all it needs to work correctly (this practice is called RAII, resource acquisition is initialization). If you have a look at the source of Rectangle class>>#origin:extent:
you'll find something very much like
origin: aPoint extent: anotherPoint
^self new origin: aPoint extent: anotherPoint
So actually sending the message directly to the class o creating it manually and then setting it is in practice the same
It's a matter of style. The Rectangle class provides a facility method for creating an instance, so that you can communicate directly with the class and write less code. It's also a good practice as you create the rectangle object with all it needs to work correctly (this practice is called RAII, resource acquisition is initialization). If you have a look at the source of Rectangle class>>#origin:extent:
you'll find something very much like
origin: aPoint extent: anotherPoint
^self new origin: aPoint extent: anotherPoint
So actually sending the message directly to the class o creating it manually and then setting it is in practice the same
answered Mar 24 at 16:47
melkyadesmelkyades
670513
670513
I have a subclassRectangle subclass: SvgRect
and then it suddenly makes a difference. Withnew
it works, without it I getObject: SvgRect error: did not understand
errors.
– adius
Mar 24 at 17:00
@adius This is probably because of another distinction: in addition to #new, there is also a #basicNew and the latter is used by the #origin:extend: class method. github.com/bonzini/smalltalk/blob/… basicNew does not invoke initializer methods, so your "did not understand" errors are likely because the additional state you defined in your SvgRect is not initialized when you used the class method to construct your SvgRect instance.
– JayK
Mar 24 at 17:57
1
As @JayK says, the errors may occur because Rectangle sendsbasicNew
instead ofnew
, but also there has to be some other part of it. If I had to guess, I'd say that your SvgRect has defined aninitialize
method, so the problem is that using class sideorigin:extent:
won't work because, unlikenew
,basicNew
doesn't sendinitialize
to the object. If that is the case, you may want to redefineorigin:extent:
in the SvgRect to the same I wrote in the original answer to avoid errors.
– melkyades
Mar 24 at 21:33
@JayK @melkyades So then can you tell me what I need to change to get rid of thenew
in github.com/ad-si/svgtalk/blob/master/tests/SvgRectTest.st#L15 ?
– adius
Mar 30 at 9:36
@adius: You'd have to write(SvgRect origin: ... extent: ...) style: ...; yourself
, so the message #style: is not sent to the class but to the new object. Otherwise I see no pitfalls.
– JayK
Mar 31 at 9:18
|
show 1 more comment
I have a subclassRectangle subclass: SvgRect
and then it suddenly makes a difference. Withnew
it works, without it I getObject: SvgRect error: did not understand
errors.
– adius
Mar 24 at 17:00
@adius This is probably because of another distinction: in addition to #new, there is also a #basicNew and the latter is used by the #origin:extend: class method. github.com/bonzini/smalltalk/blob/… basicNew does not invoke initializer methods, so your "did not understand" errors are likely because the additional state you defined in your SvgRect is not initialized when you used the class method to construct your SvgRect instance.
– JayK
Mar 24 at 17:57
1
As @JayK says, the errors may occur because Rectangle sendsbasicNew
instead ofnew
, but also there has to be some other part of it. If I had to guess, I'd say that your SvgRect has defined aninitialize
method, so the problem is that using class sideorigin:extent:
won't work because, unlikenew
,basicNew
doesn't sendinitialize
to the object. If that is the case, you may want to redefineorigin:extent:
in the SvgRect to the same I wrote in the original answer to avoid errors.
– melkyades
Mar 24 at 21:33
@JayK @melkyades So then can you tell me what I need to change to get rid of thenew
in github.com/ad-si/svgtalk/blob/master/tests/SvgRectTest.st#L15 ?
– adius
Mar 30 at 9:36
@adius: You'd have to write(SvgRect origin: ... extent: ...) style: ...; yourself
, so the message #style: is not sent to the class but to the new object. Otherwise I see no pitfalls.
– JayK
Mar 31 at 9:18
I have a subclass
Rectangle subclass: SvgRect
and then it suddenly makes a difference. With new
it works, without it I get Object: SvgRect error: did not understand
errors.– adius
Mar 24 at 17:00
I have a subclass
Rectangle subclass: SvgRect
and then it suddenly makes a difference. With new
it works, without it I get Object: SvgRect error: did not understand
errors.– adius
Mar 24 at 17:00
@adius This is probably because of another distinction: in addition to #new, there is also a #basicNew and the latter is used by the #origin:extend: class method. github.com/bonzini/smalltalk/blob/… basicNew does not invoke initializer methods, so your "did not understand" errors are likely because the additional state you defined in your SvgRect is not initialized when you used the class method to construct your SvgRect instance.
– JayK
Mar 24 at 17:57
@adius This is probably because of another distinction: in addition to #new, there is also a #basicNew and the latter is used by the #origin:extend: class method. github.com/bonzini/smalltalk/blob/… basicNew does not invoke initializer methods, so your "did not understand" errors are likely because the additional state you defined in your SvgRect is not initialized when you used the class method to construct your SvgRect instance.
– JayK
Mar 24 at 17:57
1
1
As @JayK says, the errors may occur because Rectangle sends
basicNew
instead of new
, but also there has to be some other part of it. If I had to guess, I'd say that your SvgRect has defined an initialize
method, so the problem is that using class side origin:extent:
won't work because, unlike new
, basicNew
doesn't send initialize
to the object. If that is the case, you may want to redefine origin:extent:
in the SvgRect to the same I wrote in the original answer to avoid errors.– melkyades
Mar 24 at 21:33
As @JayK says, the errors may occur because Rectangle sends
basicNew
instead of new
, but also there has to be some other part of it. If I had to guess, I'd say that your SvgRect has defined an initialize
method, so the problem is that using class side origin:extent:
won't work because, unlike new
, basicNew
doesn't send initialize
to the object. If that is the case, you may want to redefine origin:extent:
in the SvgRect to the same I wrote in the original answer to avoid errors.– melkyades
Mar 24 at 21:33
@JayK @melkyades So then can you tell me what I need to change to get rid of the
new
in github.com/ad-si/svgtalk/blob/master/tests/SvgRectTest.st#L15 ?– adius
Mar 30 at 9:36
@JayK @melkyades So then can you tell me what I need to change to get rid of the
new
in github.com/ad-si/svgtalk/blob/master/tests/SvgRectTest.st#L15 ?– adius
Mar 30 at 9:36
@adius: You'd have to write
(SvgRect origin: ... extent: ...) style: ...; yourself
, so the message #style: is not sent to the class but to the new object. Otherwise I see no pitfalls.– JayK
Mar 31 at 9:18
@adius: You'd have to write
(SvgRect origin: ... extent: ...) style: ...; yourself
, so the message #style: is not sent to the class but to the new object. Otherwise I see no pitfalls.– JayK
Mar 31 at 9:18
|
show 1 more comment
I think it is important to note the difference between Smalltalk and other OO languages.
In other OO languages you have a construct called a constructor. Which enables you to automatically run certain code when you call method new
.
For example, in ruby you would do
# Class name
class ConstructorExample
# A constructor
def initialize
puts "I'm now running constructor"
end
end
# Creating Object
ConstructorExample.new # upon which calling you will get initialized run automatically.
The output would be in your shell:
> I'm now running constructor
In Smalltalk you have to differenciate between new
and basicNew
. (Sometimes even new
is only alias for basicNew
so you have to run initialize
manually
or create class method. The basicNew
does not execute the initialize automatically, the new
usually does (not all dialects!).
The above example could be then written as:
Object subclass:#ConstructorExample
instanceVariableNames:''
classVariableNames:''
poolDictionaries:''
category: ''
ConstructorExample>>initialize
Transcript showCR: 'I'm now running a constructor'
"You would then instantiate"
ConstructorExample new
or
| example |
example := ConstructorExample basicNew.
example initialize "sending method initialize like any other method"
In both cases the output would be (in your Transcript):
I'm now running a constructor
The main reason for that, in my eyes, is that you can run constructor after some of your custom code if you have class method
ConstructorExample class >> run
^ self basicNew; Transcript showCR: 'Running before constructor'; self initialize; yourself
Then you would simply do:
ConstructorExample run
The output would be:
Running before constructor
I'm now running a constructor
Now to your example
As JayK, melkyades explained the main differences I'll give more suttle differences (details) on those:
First:
Rectangle new origin: 5@5 extent: 40@30
What it actually does it this (without the Transcript showCR:
):
| myRactangle |
myRactangle := Ractangle new. "Creates an empty instance with origin: 0 @ 0 corner: 0 @ 0 and would execute initialize if there would be one."
Transcript showCR: 'Current origin: ', origin asString, 'with corner: ', corner asString. "You should see the zeros"
myRactangle origin: 5@5 extent: 40@30
Transcript showCR: 'Current origin: ', origin asString, 'with corner: ', corner asString. "You should your custom point(s)"
What happens when you do Ractangle new
?
Rectangle class >> new [
"Answer the (0 @ 0 corner: 0 @ 0) rectangle"
<category: 'instance creation'>
^self origin: 0 @ 0 corner: 0 @ 0
]
When you check the source code it sets origin: 0 @ 0 corner: 0 @ 0
(notice the difference setting it via ...corner:
and not extent:
).
Second:
Rectangle origin: 5@5 extent: 40@30
The source code:
Rectangle class >> origin: originPoint extent: extentPoint
"Answer a rectangle with the given origin and size"
<category: 'instance creation'>
^self basicNew origin: originPoint corner: originPoint + extentPoint
As pointed-out already there is a basicNew
which prevents the any initialize
constuctor to be run manually or via class method like I showed above.
What you could do it rewrite it if you needed. You would create your own rectangle class which would inherit it from Rectangle and rewrite it there.
For example:
Rectangle subclass:#ApplicationRectangle
instanceVariableNames:''
classVariableNames:''
poolDictionaries:''
category: ''
where you would define:
ApplicationRectangle class >> origin: originPoint extent: extentPoint
"Answer a rectangle with the given origin and size"
<category: 'instance creation'>
^self new origin: originPoint corner: originPoint + extentPoint
You would then call it:
ApplicationRectangle origin: 5@5 extent: 40@30
add a comment |
I think it is important to note the difference between Smalltalk and other OO languages.
In other OO languages you have a construct called a constructor. Which enables you to automatically run certain code when you call method new
.
For example, in ruby you would do
# Class name
class ConstructorExample
# A constructor
def initialize
puts "I'm now running constructor"
end
end
# Creating Object
ConstructorExample.new # upon which calling you will get initialized run automatically.
The output would be in your shell:
> I'm now running constructor
In Smalltalk you have to differenciate between new
and basicNew
. (Sometimes even new
is only alias for basicNew
so you have to run initialize
manually
or create class method. The basicNew
does not execute the initialize automatically, the new
usually does (not all dialects!).
The above example could be then written as:
Object subclass:#ConstructorExample
instanceVariableNames:''
classVariableNames:''
poolDictionaries:''
category: ''
ConstructorExample>>initialize
Transcript showCR: 'I'm now running a constructor'
"You would then instantiate"
ConstructorExample new
or
| example |
example := ConstructorExample basicNew.
example initialize "sending method initialize like any other method"
In both cases the output would be (in your Transcript):
I'm now running a constructor
The main reason for that, in my eyes, is that you can run constructor after some of your custom code if you have class method
ConstructorExample class >> run
^ self basicNew; Transcript showCR: 'Running before constructor'; self initialize; yourself
Then you would simply do:
ConstructorExample run
The output would be:
Running before constructor
I'm now running a constructor
Now to your example
As JayK, melkyades explained the main differences I'll give more suttle differences (details) on those:
First:
Rectangle new origin: 5@5 extent: 40@30
What it actually does it this (without the Transcript showCR:
):
| myRactangle |
myRactangle := Ractangle new. "Creates an empty instance with origin: 0 @ 0 corner: 0 @ 0 and would execute initialize if there would be one."
Transcript showCR: 'Current origin: ', origin asString, 'with corner: ', corner asString. "You should see the zeros"
myRactangle origin: 5@5 extent: 40@30
Transcript showCR: 'Current origin: ', origin asString, 'with corner: ', corner asString. "You should your custom point(s)"
What happens when you do Ractangle new
?
Rectangle class >> new [
"Answer the (0 @ 0 corner: 0 @ 0) rectangle"
<category: 'instance creation'>
^self origin: 0 @ 0 corner: 0 @ 0
]
When you check the source code it sets origin: 0 @ 0 corner: 0 @ 0
(notice the difference setting it via ...corner:
and not extent:
).
Second:
Rectangle origin: 5@5 extent: 40@30
The source code:
Rectangle class >> origin: originPoint extent: extentPoint
"Answer a rectangle with the given origin and size"
<category: 'instance creation'>
^self basicNew origin: originPoint corner: originPoint + extentPoint
As pointed-out already there is a basicNew
which prevents the any initialize
constuctor to be run manually or via class method like I showed above.
What you could do it rewrite it if you needed. You would create your own rectangle class which would inherit it from Rectangle and rewrite it there.
For example:
Rectangle subclass:#ApplicationRectangle
instanceVariableNames:''
classVariableNames:''
poolDictionaries:''
category: ''
where you would define:
ApplicationRectangle class >> origin: originPoint extent: extentPoint
"Answer a rectangle with the given origin and size"
<category: 'instance creation'>
^self new origin: originPoint corner: originPoint + extentPoint
You would then call it:
ApplicationRectangle origin: 5@5 extent: 40@30
add a comment |
I think it is important to note the difference between Smalltalk and other OO languages.
In other OO languages you have a construct called a constructor. Which enables you to automatically run certain code when you call method new
.
For example, in ruby you would do
# Class name
class ConstructorExample
# A constructor
def initialize
puts "I'm now running constructor"
end
end
# Creating Object
ConstructorExample.new # upon which calling you will get initialized run automatically.
The output would be in your shell:
> I'm now running constructor
In Smalltalk you have to differenciate between new
and basicNew
. (Sometimes even new
is only alias for basicNew
so you have to run initialize
manually
or create class method. The basicNew
does not execute the initialize automatically, the new
usually does (not all dialects!).
The above example could be then written as:
Object subclass:#ConstructorExample
instanceVariableNames:''
classVariableNames:''
poolDictionaries:''
category: ''
ConstructorExample>>initialize
Transcript showCR: 'I'm now running a constructor'
"You would then instantiate"
ConstructorExample new
or
| example |
example := ConstructorExample basicNew.
example initialize "sending method initialize like any other method"
In both cases the output would be (in your Transcript):
I'm now running a constructor
The main reason for that, in my eyes, is that you can run constructor after some of your custom code if you have class method
ConstructorExample class >> run
^ self basicNew; Transcript showCR: 'Running before constructor'; self initialize; yourself
Then you would simply do:
ConstructorExample run
The output would be:
Running before constructor
I'm now running a constructor
Now to your example
As JayK, melkyades explained the main differences I'll give more suttle differences (details) on those:
First:
Rectangle new origin: 5@5 extent: 40@30
What it actually does it this (without the Transcript showCR:
):
| myRactangle |
myRactangle := Ractangle new. "Creates an empty instance with origin: 0 @ 0 corner: 0 @ 0 and would execute initialize if there would be one."
Transcript showCR: 'Current origin: ', origin asString, 'with corner: ', corner asString. "You should see the zeros"
myRactangle origin: 5@5 extent: 40@30
Transcript showCR: 'Current origin: ', origin asString, 'with corner: ', corner asString. "You should your custom point(s)"
What happens when you do Ractangle new
?
Rectangle class >> new [
"Answer the (0 @ 0 corner: 0 @ 0) rectangle"
<category: 'instance creation'>
^self origin: 0 @ 0 corner: 0 @ 0
]
When you check the source code it sets origin: 0 @ 0 corner: 0 @ 0
(notice the difference setting it via ...corner:
and not extent:
).
Second:
Rectangle origin: 5@5 extent: 40@30
The source code:
Rectangle class >> origin: originPoint extent: extentPoint
"Answer a rectangle with the given origin and size"
<category: 'instance creation'>
^self basicNew origin: originPoint corner: originPoint + extentPoint
As pointed-out already there is a basicNew
which prevents the any initialize
constuctor to be run manually or via class method like I showed above.
What you could do it rewrite it if you needed. You would create your own rectangle class which would inherit it from Rectangle and rewrite it there.
For example:
Rectangle subclass:#ApplicationRectangle
instanceVariableNames:''
classVariableNames:''
poolDictionaries:''
category: ''
where you would define:
ApplicationRectangle class >> origin: originPoint extent: extentPoint
"Answer a rectangle with the given origin and size"
<category: 'instance creation'>
^self new origin: originPoint corner: originPoint + extentPoint
You would then call it:
ApplicationRectangle origin: 5@5 extent: 40@30
I think it is important to note the difference between Smalltalk and other OO languages.
In other OO languages you have a construct called a constructor. Which enables you to automatically run certain code when you call method new
.
For example, in ruby you would do
# Class name
class ConstructorExample
# A constructor
def initialize
puts "I'm now running constructor"
end
end
# Creating Object
ConstructorExample.new # upon which calling you will get initialized run automatically.
The output would be in your shell:
> I'm now running constructor
In Smalltalk you have to differenciate between new
and basicNew
. (Sometimes even new
is only alias for basicNew
so you have to run initialize
manually
or create class method. The basicNew
does not execute the initialize automatically, the new
usually does (not all dialects!).
The above example could be then written as:
Object subclass:#ConstructorExample
instanceVariableNames:''
classVariableNames:''
poolDictionaries:''
category: ''
ConstructorExample>>initialize
Transcript showCR: 'I'm now running a constructor'
"You would then instantiate"
ConstructorExample new
or
| example |
example := ConstructorExample basicNew.
example initialize "sending method initialize like any other method"
In both cases the output would be (in your Transcript):
I'm now running a constructor
The main reason for that, in my eyes, is that you can run constructor after some of your custom code if you have class method
ConstructorExample class >> run
^ self basicNew; Transcript showCR: 'Running before constructor'; self initialize; yourself
Then you would simply do:
ConstructorExample run
The output would be:
Running before constructor
I'm now running a constructor
Now to your example
As JayK, melkyades explained the main differences I'll give more suttle differences (details) on those:
First:
Rectangle new origin: 5@5 extent: 40@30
What it actually does it this (without the Transcript showCR:
):
| myRactangle |
myRactangle := Ractangle new. "Creates an empty instance with origin: 0 @ 0 corner: 0 @ 0 and would execute initialize if there would be one."
Transcript showCR: 'Current origin: ', origin asString, 'with corner: ', corner asString. "You should see the zeros"
myRactangle origin: 5@5 extent: 40@30
Transcript showCR: 'Current origin: ', origin asString, 'with corner: ', corner asString. "You should your custom point(s)"
What happens when you do Ractangle new
?
Rectangle class >> new [
"Answer the (0 @ 0 corner: 0 @ 0) rectangle"
<category: 'instance creation'>
^self origin: 0 @ 0 corner: 0 @ 0
]
When you check the source code it sets origin: 0 @ 0 corner: 0 @ 0
(notice the difference setting it via ...corner:
and not extent:
).
Second:
Rectangle origin: 5@5 extent: 40@30
The source code:
Rectangle class >> origin: originPoint extent: extentPoint
"Answer a rectangle with the given origin and size"
<category: 'instance creation'>
^self basicNew origin: originPoint corner: originPoint + extentPoint
As pointed-out already there is a basicNew
which prevents the any initialize
constuctor to be run manually or via class method like I showed above.
What you could do it rewrite it if you needed. You would create your own rectangle class which would inherit it from Rectangle and rewrite it there.
For example:
Rectangle subclass:#ApplicationRectangle
instanceVariableNames:''
classVariableNames:''
poolDictionaries:''
category: ''
where you would define:
ApplicationRectangle class >> origin: originPoint extent: extentPoint
"Answer a rectangle with the given origin and size"
<category: 'instance creation'>
^self new origin: originPoint corner: originPoint + extentPoint
You would then call it:
ApplicationRectangle origin: 5@5 extent: 40@30
edited Mar 26 at 15:35
answered Mar 26 at 15:29
tukantukan
7,70011130
7,70011130
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55325909%2fdifference-between-instances-created-with-and-without-new-in-gnu-smalltalk%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Friendly reminder to consider accepting one of the answers (or asking further clarification in comments).
– fede s.
Apr 10 at 13:56