On Thu, Jan 19, 2017 at 11:37 AM, Sven Van Caekenberghe <s...@stfx.eu> wrote:
> The Traits would be cool to standardise the behaviour and to mark it as > such (implicit documentation) Exactly. > , however, Traits cannot add instance/class variables IIUC, that limits > what they can do (and would make it more than one step). > Ok. So that's the difference between a Ruby mixin and a trait. That's a shame, I think what Ruby does with mixins is really nice because it a) results in less code to test, and b) makes hineritance chains more semantic - you only inherit to model spcialization, not to reuse features. Cyril, many thanks! I know how to implement it and it is true that it is not much, but I had hoped to eliminate the code (and tests!) and use a standard feature if possible ;) -- Siemen > > > On 19 Jan 2017, at 11:33, Cyril Ferlicot D. <cyril.ferli...@gmail.com> > wrote: > > > > On 19/01/2017 11:23, Siemen Baader wrote: > >> Is there a trait to quickly create singletons in Pharo, along the way of > >> the Ruby Singleton mixin? > >> > >> https://ruby-doc.org/stdlib-1.9.3/libdoc/singleton/rdoc/Singleton.html > >> > >> (and ideally also observer/observable, which I am going to need next..) > >> > >> -- Siemen > > > > Hi! > > > > Singleton in Smalltalk are really easy so there is no Trait. > > > > You just need a class variable #UniqueInstance and those methods: > > > > current > > "Can also be named #default or #instance" > > ^ UniqueInstance > > ifNil: [ UniqueInstance := self basicNew; initialize; > yourself ] > > > > new > > ^ self error: 'I am a Singleton. Use current to get my instance or > > reset my instance with a new project. Maybe explain why there is a > > Singleton because we should avoid them if possible.' > > > > > > Now for the observer pattern you have the Announcement framework in > Pharo. > > > > I don't really have the time to explain but I think there is some doc on > > internet and on the help browser of Pharo. I found this: > > http://pharo.gemtalksystems.com/book/LanguageAndLibraries/announcements/ > > (I did not read it so I don't know what it's worth) > > > > -- > > Cyril Ferlicot > > > > http://www.synectique.eu > > > > 2 rue Jacques Prévert 01, > > 59650 Villeneuve d'ascq France > > > > >