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;








3















What is the difference between



Rectangle origin: 5@5 extent: 40@30


and



Rectangle new origin: 5@5 extent: 40@30









share|improve this question






















  • Friendly reminder to consider accepting one of the answers (or asking further clarification in comments).

    – fede s.
    Apr 10 at 13:56

















3















What is the difference between



Rectangle origin: 5@5 extent: 40@30


and



Rectangle new origin: 5@5 extent: 40@30









share|improve this question






















  • Friendly reminder to consider accepting one of the answers (or asking further clarification in comments).

    – fede s.
    Apr 10 at 13:56













3












3








3








What is the difference between



Rectangle origin: 5@5 extent: 40@30


and



Rectangle new origin: 5@5 extent: 40@30









share|improve this question














What is the difference between



Rectangle origin: 5@5 extent: 40@30


and



Rectangle new origin: 5@5 extent: 40@30






smalltalk gnu-smalltalk






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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

















  • 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












3 Answers
3






active

oldest

votes


















4














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.






share|improve this answer






























    3














    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






    share|improve this answer























    • 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







    • 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












    • @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


















    1














    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





    share|improve this answer

























      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
      );



      );













      draft saved

      draft discarded


















      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









      4














      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.






      share|improve this answer



























        4














        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.






        share|improve this answer

























          4












          4








          4







          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.






          share|improve this answer













          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 24 at 18:07









          JayKJayK

          1,56611119




          1,56611119























              3














              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






              share|improve this answer























              • 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







              • 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












              • @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















              3














              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






              share|improve this answer























              • 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







              • 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












              • @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













              3












              3








              3







              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






              share|improve this answer













              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







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Mar 24 at 16:47









              melkyadesmelkyades

              670513




              670513












              • 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







              • 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












              • @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

















              • 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







              • 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












              • @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
















              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











              1














              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





              share|improve this answer





























                1














                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





                share|improve this answer



























                  1












                  1








                  1







                  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





                  share|improve this answer















                  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






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Mar 26 at 15:35

























                  answered Mar 26 at 15:29









                  tukantukan

                  7,70011130




                  7,70011130



























                      draft saved

                      draft discarded
















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                      Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                      Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript