Cross Platform with Pharo

What is Cross Platform? Cross platform is something which can be implemented in different environments. The OSPlatform class in Pharo makes it easier to write cross- platform applications. In this blog post, I will go through how to write cross-platform applications of Pharo.

It is very easy to implement applications based on the platform in Pharo. The package ‘System-Platform’ contains all the classes necessary for this. In this package, classes important for cross-platform applications are

  • OSPlatform
  • MacOSPlatform
  • MacOSXPlatform
  • Unix Platform
  • Unix32Platform
  • Unix64Platform
  • WinPlatform
  • Win32Platform
  • Win64Platform

There is a hierarchy amongst these classes and it can be illustrated as follows:

OSPlatform hier

The OSPlatform class follows the ‘singleton design pattern’ i.e in the entire system this class will have only one sub-instance. So, whenever I call, OSPlatform current this will give me the same instance of the platform. This will be the current platform in which you are working. But, if the platform is changed, the above code will change the instance accordingly.

To explain how to implement an application, I would like to use the example from my GSoC project. I had to implement a method ‘getCurrentWorkingDirectory’ which when called will give the right working directory. This has to work irrespective of the platform. The implementation is a bit different in Windows systems and Unix systems. So, how to work with different implementations on different platforms?

As you see the inheritance image, there are different platform classes. The solution here is, to use ‘Polymorphism’. Polymorphism is the art of using objects in an interchangeable way. If polymorphism is used, all the platforms will have individual methods with different implementations and when I send the message to an object, depending on the object, Pharo checks if the object has that method and invokes it. If there is no such method it returns an error.  Polymorphism is very helpful when I have a new class to add. In such case, I just have to add a new method with the required implementation in the new platform class and Pharo takes care of calling the method from the right platform.

Apart from these, the extension methods in Pharo makes it easier to consolidate the classes as required. For example, if I have a method ‘X’ in package ‘A’, and if this method ‘X’ is using a method ‘Y’, instead of setting up a dependency, I can extend the method ‘Y’ to the package ‘A’ just by right clicking on the method and selecting ‘move to package’. This extension of methods helps in creating and committing a new package only with the new or required methods.

And the ‘OSPlatfrom’ class also have methods like ‘family’, ‘isUnix’, ‘isWindows’, etc. The ‘family’ method returns the type of the platform family and ‘isUnix’, ‘isWindows’ returns boolean depending on the platform. These methods help when there is a need to know the platform and do some manipulation depending on the platform.

Do give your feedback below.


  1. Pharo by example book

One thought on “Cross Platform with Pharo

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s