Static class methods in pony?

136 Views Asked by At

On code that doesn't make sense to have a this context, such as utility functions that relate to a class, is there a definition and calling syntax for "static class" methods in Pony or am I holding it wrong?

2

There are 2 best solutions below

1
On

Looks like you can't include them in an existing class, but stdlib uses this pattern of hacky workaround:

primitive Utils
  fun format(x: USize) => String
    x.string()

Utils.format(1234)
0
On

I would recommend using a primitive as you answered, but another possibility is to use fun tag:

class Foo
  fun tag get_something(): String =>
    "Hello, world!"

This can be done for any type with methods (objects, actors, primitives), so long as you have a tag reference to it.