Search code examples
smalltalkgnu-smalltalk

Using extended classes in gst (GNU smalltalk)?


This is a bit of a follow-up question to this one.

Say I've managed to extend the Integer class with a new method 'square'. Now I want to use it.

Calling the new method from within the file is easy:

Integer extend [
    square [
        | r |
        r := self * self.
        ^r
    ]
]

x := 5 square.
x printNl.

Here, I can just call $ gst myprogram.st in bash and it'll print 25. But what if I want to use the method from inside the GNU smalltalk application? Like this:

$ gst
st> 5 square
25
st>

This may have to do with images, I'm not sure. This tutorial says I can edit the ~/.st/kernel/Builtins.st file to edit what files are loaded into the kernel, but I have no such file.


Solution

  • I would not edit what's loaded into the kernel. To elaborate on my comment, one way of loading previously created files into the environment for GNU Smalltalk, outside of using image files, is to use packages.

    A sample package.xml file, which defines the package per the documentation, would look like:

    <package>
      <name>MyPackage</name>
    
      <!-- Include any prerequisite packages here, if you need them -->
      <prereq>PrequisitePackageName</prereq>
    
      <filein>Foo.st</filein>
      <filein>Bar.st</filein>
    </package>
    

    A sample Makefile for building the package might look like:

    # MyPackage makefile
    #
    PACKAGE_DIR = ~/.st
    PACKAGE_SPEC = package.xml
    PACKAGE_FILE = $(PACKAGE_DIR)/MyPackage.star
    PACKAGE_SRC = \
            Foo.st \
            Bar.st
    
    $(PACKAGE_FILE):        $(PACKAGE_SRC) $(PACKAGE_SPEC)
            gst-package -t ~/.st $(PACKAGE_SPEC)
    

    With the above files in your working directory containing Foo.st and Bar.st, you can do a make and it will build the .star package file and put it in ~/.st (where gst will go looking for packages as the first place to look). When you run your environment, you can then use PackageLoader to load it in:

    $ gst
    GNU Smalltalk ready
    
    st> PackageLoader fileInPackage: 'MyPackage'
    Loading package PrerequisitePackage
    Loading package MyPackage
    PackageLoader
    st>
    

    Then you're ready to rock and roll... :)