First time here? Check out the FAQ!

Adding a method to a class

+1 vote
Hello hello.

In Pharo Smalltalk it is possible to add a method to an existing class from the playground by using this syntax:

  compile: 'tenTimes
    "Multiplicate a number for 10"
    ^ self * 10.'
  classified: 'actions'.

I wonder if it is possible to do something like this evaluating a little script in a text or from a Tool in Kyma. I can imagine that it should be done at every Kyma restart, but I don't mind too much :).



asked Jul 21, 2020 in Capytalk & Smalltalk by domenico-cipriani (Adept) (2,830 points)
reshown Jul 21, 2020 by ssc

1 Answer

0 votes
Best answer
No, but you can use blocks and save them in variables to define functions. Your example would be something like:

| f |

f := [ :x | x * 10].

f value: 2
answered Jul 21, 2020 by ssc (Savant) (115,030 points)
selected Jul 21, 2020 by domenico-cipriani
thanks I will try to work it out with this.
Are block methods in Smalltalk similar to anonymous functions like Lambdas in C++ ?
Yes, blocks are lambda expressions or anonymous functions (functions without names).