[英]error: no match for ‘operator==’ in ‘boiler::uniqueInstance == 0l’
[英]uniqueInstance on Seaside Smalltalk
我正在嘗試在Seaside上實現Singleton模式。 這是我的代碼:
uniqueInstance
uniqueInstance ifNil: [ uniqueInstance := self createInstance].
^ uniqueInstance
createInstance
^ self basicNew
initialize
users:= OrderedCollection new.
在實例方面,我有作為實例變量的用戶 。 當我調用應用程序時,uniqueInstance初始化工作。 當我檢查用戶變量時,它說的是“ Identity Set”,而不是OrderedCollection。
有人可以給我解決我的錯誤的方法嗎?
您的#createInstance
方法發送#basicNew
消息,但此消息不發送#initialize
。 因此,沒有人初始化您的對象,而users
仍然nil
。 必須有一些將users
設置為IndentitySet
惰性方法,這就是為什么IndentitySet
。 但是沒有人將其OrderedCollection
。
要initialize
實例,請使用#new
而不是#basicNew
; 至少在Pharo和其他一些方言中, #new
會為您發送#initialize
。
基於(Pharo)關於單例的郵件列表討論,我前段時間https://www.peteruhnak.com/blog/2015/12/06/singleton-variations/寫下了一些變化。
但是單例的要點是:
MyThing class>>new
super new
或self basicNew initialize
new
; 如果您的方言沒有自動調用initialize
使用后一種形式 MyThing class>>reset
至nil
的情況下
<script: 'self reset'>
編譯指示可以做得更好) 因此,在您的情況下,可能是這樣的:
Users class>>uniqueInstance
^ uniqueInstance ifNil: [ uniqueInstance := super new ]
Users class>>new
self error: 'Users is a singleton -- send uniqueInstance instead'
Users>>initialize
users:= OrderedCollection new
如果您在Pharo,請隨時探索現有實現
MessageBrowser browse: (#uniqueInstance implementors select: #isMetaSide).
MessageBrowser browse: (#default implementors select: #isMetaSide).
MessageBrowser browse: (#current implementors select: #isMetaSide).
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.