1

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.

Community
  • 1
  • 1
Mossmyr
  • 909
  • 2
  • 10
  • 26
  • 1
    I edited the title to indicate that the question is about `gst` and removed the [tag:bash] tag as this doesn't have anything to do with `bash`. – Etan Reisner May 05 '16 at 16:16
  • If that file doesn't exist then you can probably just create it. Though given the link you might want/need to copy the default one there and edit it instead of creating an empty one to start from. – Etan Reisner May 05 '16 at 16:16
  • Unfortunately, GNU Smalltalk doesn't have a `FileLoader` method, although I thought I read they were thinking about it. In the meantime, what I do is collect extensions and put them into a [*package*](https://www.gnu.org/software/smalltalk/manual/html_node/Packages.html). Then you can use `PackageLoader` to load in the classes. I use a `Makefile` and `gst-package` to rebuild the package if I change the source. The other alternative, as you say, would be to save it as an image file and reload the image file when needed. Once you get setup using packages, it is very easy. – lurker May 06 '16 at 15:51
  • As an aside, in the link reference it suggest you can shorten your `square` method as, `square [ ^self * self ]`. No need for the extra variable. – lurker May 06 '16 at 15:53

1 Answers1

3

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... :)

lurker
  • 56,987
  • 9
  • 69
  • 103
  • Doesn't seem to work. I've gotten the package to load (I've tested and made sure that this is the case), but once I call `st> 5 square` I get the error message `Object: 5 error: did not understand #square` – Mossmyr May 06 '16 at 17:35
  • Let me elaborate: `$ make` -> `make: '/home/moss/.st/MyPackage.star' is up to date.` `$ gst` -> `GNU SmallTalkready` `st> PackageLoader fileInPackage: 'MyPackage'` -> `[...] Loading package MyPackage [...]` `st> 5 square` -> `Object: 5 error: did not understand #square` – Mossmyr May 06 '16 at 17:38
  • @Mossmyr I don't have much to go on here. I'd need to know what's inside `MyPackage.star`. I use this method all the time and it works for me. I assume your `PACKAGE_SRC` files have at least one file that does the `Integer extend [ square [ ...] ]`? – lurker May 06 '16 at 17:41
  • @Mossmyr I just did a quick test doing exactly as I outlined in my answer and it worked fine. In my test, `MyPackage` was built from just one file, `Ext.st` that contained `Integer extend [ square [ ... ] ]`. Changes to `package.xml` and `Makefile` to reflect that it only consisted of `Ext.st`, to keep it simple. – lurker May 06 '16 at 17:44
  • I'm really stupid. The class I want to apply this on is its own class, and not an extension of Integer. I forgot to change it... I've accepted your answer now, thanks for your effort :) – Mossmyr May 06 '16 at 17:50
  • @Mossmyr haha no worries. – lurker May 06 '16 at 17:57