pharo icon indicating copy to clipboard operation
pharo copied to clipboard

Internal users of asOrderedCollection should be rewritten because asOrderedCollection is deprecated

Open Ducasse opened this issue 1 year ago • 9 comments

Object >> asOrderedCollection

	self
		deprecated:
		'The usage of this method is not recommended. We want to have a smaller Object api. We will remove this method in the next Pharo version.'
"Automatic transform removed. See https://github.com/pharo-project/pharo/issues/14197"
		"transformWith: '`@receiver asOrderedCollection' -> 'OrderedCollection with: `@receiver'".
	^ OrderedCollection with: self

Ducasse avatar Jan 06 '24 20:01 Ducasse

We should only rewrite receivers that are not collection. If you be nice to be able to use deprecated: anExplanationString transformWith: aRule when: conditionBlock but I do not know the scope of conditionBlock: because we want to express

Object >> asOrderedCollection

	self
		deprecated:
		'The usage of this method is not recommended. We want to have a smaller Object api. We will remove this method in the next Pharo version.'
            transformWith: '`@receiver asOrderedCollection' -> 'OrderedCollection with: `@receiver'
            when: [ `@receiver class IsKindOf: Collection ]
	^ OrderedCollection with: self

We can probably do it with thisContext receiver or something like that.

Ducasse avatar Jan 06 '24 20:01 Ducasse

recOrList below is a candidate

wipeImage: otherImage at: topLeft clippingBox: clipBox rectForIndex: rectForIndexBlock

	| i clipRect t rectOrList waitTime |
	i := 0.
	clipRect := topLeft extent: otherImage extent.
	clipBox ifNotNil: [clipRect := clipRect intersect: clipBox ifNone: [ ^ self ]].
	[rectOrList := rectForIndexBlock value: (i := i + 1).
	 rectOrList == nil]
		whileFalse: [
			t := Time millisecondClockValue.
			rectOrList asOrderedCollection do: [:r |
				self copyBits: r from: otherImage at: topLeft + r topLeft
					clippingBox: clipRect rule: Form over fillColor: nil].
			waitTime := 3 - (Time millisecondClockValue - t).
			waitTime > 0 ifTrue:
				["(Delay forMilliseconds: waitTime) wait"]]

Ducasse avatar Jan 06 '24 20:01 Ducasse

I do not get the following code

makeResolved

	super makeResolved.

	slots := self slots asOrderedCollection markAsRingResolved

Because self slots is defined as

slots

	| allSlots |
	allSlots := OrderedCollection new.
	self slotsDo: [ :each | allSlots add: each].
	^ allSlots asArray

Ducasse avatar Jan 06 '24 20:01 Ducasse

@jordanmontt this is already related :)

Ducasse avatar Jan 06 '24 21:01 Ducasse

I do not get the following code

makeResolved

	super makeResolved.

	slots := self slots asOrderedCollection markAsRingResolved

Because self slots is defined as

slots

	| allSlots |
	allSlots := OrderedCollection new.
	self slotsDo: [ :each | allSlots add: each].
	^ allSlots asArray

fun

guillep avatar Jan 08 '24 08:01 guillep

Hello, We can indeed use deprecated: anExplanationString transformWith: aRule when: conditionBlock and write a rule that transforms only when the receiver isn't a collection. For example [ :ctx | (ctx receiver class = Collection) not ]. The problem is, as described in https://github.com/pharo-project/pharo/issues/14197, if we have polymorphism like in

objects := { Object new . OrderedCollection new }.
resultObjects := objects collect:[ :o | o asOrderedCollection ].

Even if we check with the rule that the receiver is not a collection, the first time it will check and as is not a collection, it will transform the code. The second time it will, and that is where the problem is…

jordanmontt avatar Jan 26 '24 11:01 jordanmontt

asOrderedCollection is only deprecated on Object, for collections it still is there like asArray, asSet...

MarcusDenker avatar Jan 29 '24 14:01 MarcusDenker

So what we do?

hernanmd avatar Jan 29 '24 14:01 hernanmd

I don't think the issue is actionable in an evident way. The thing is to realize what are the callsites calling the deprecated methods, and rewrite those (and not the valid senders).

One way to do it is to run all the tests of Pharo. Tests triggering the deprecation will be rewritten. Then they can be committed.

The main issue is when there are polymorphic callsites: sometimes the receiver is a collection, sometimes it is not. In that case the rewriting will be correct only for one case. In that case, probably a case-by-case analysis is needed to see what to do.

guillep avatar Feb 01 '24 09:02 guillep