[squeak-dev] [BUG] Issue with Matrix in instance variable

Marcel Taeumel marcel.taeumel at hpi.de
Wed Feb 19 09:31:34 UTC 2020


> Why don't we copy contents when passed in the constructor?

Because for most cases, you do not need to do that extra effort. I suppose.

Best,
Marcel
Am 19.02.2020 10:29:32 schrieb Thiede, Christoph <christoph.thiede at student.hpi.uni-potsdam.de>:
Wait, this is indeed interesting! Why don't we copy contents when passed in the constructor? This really feels like an implementation detail of array.

block := [Matrix rows: 1 columns: 1 contents: #(1)].
x := block value.
x at: 1 at: 1 put: 2.
y := block value.
self assert: [(y at: 1 at: 1) = 1]. "failed, is 2!"

Whereas it works if you define block as:

"block := [Matrix rows: 1 columns: 1 contents: {1}]."

Best,
Christoph
Von: Squeak-dev <squeak-dev-bounces at lists.squeakfoundation.org> im Auftrag von Taeumel, Marcel
Gesendet: Mittwoch, 19. Februar 2020 10:08:34
An: John Pfersich via Squeak-dev
Betreff: Re: [squeak-dev] [BUG] Issue with Matrix in instance variable
 
Hi Karl.

The issue is that, in your example, you are always using the same instance of Array: 

initialize
super initialize.
grid := Matrix rows: 3 columns: 3 contents: #(
5 3 0 
6 0 0 
0 9 8 )

Those "contents" will always be the same for each new instance of TesterClass. So, even though the source code reads "#( 5 3 0 ..." is has actually changed to "#( 9 9 9 ...". Try inspecting "TesterClass >> #initalize" and take a look at #literal5:



This is not a bug. :-) Just a (maybe surprising) side effect of literal arrays.

Best,
Marcel
Am 19.02.2020 09:09:03 schrieb karl ramberg <karlramberg at gmail.com>:
Hi,
I have this simple class TesterClass new.

Doit  in Workspace: TesterClass inspect.
The grid instance variable is initialized according to initialize method.

I doit in inspector: self fillGrid.

The grid matrix is now filled with all 9.
As expected.


Now doit in Workspace: TesterClass new inspect

The grid is all 9.

I think the issue is in

Matrix class>>rows: columns: contents:

rows: rows columns: columns contents: contents
^super new rows: rows columns: columns contents: contents

Matrix class overrides new so I think there is a conflict


Best,
Karl






'From Squeak5.3alpha of 23 October 2019 [latest update: #19139] on 18 February 2020 at 9:22:56 pm'!
Object subclass: #TesterClass
instanceVariableNames: 'grid'
classVariableNames: ''
poolDictionaries: ''
category: 'MyTestStuff'!

!TesterClass methodsFor: 'as yet unclassified' stamp: 'kfr 2/18/2020 21:21'!
fillGrid
grid atAllPut: 9! !

!TesterClass methodsFor: 'as yet unclassified' stamp: 'kfr 2/18/2020 21:18'!
initialize
super initialize.
grid := Matrix rows: 3 columns: 3 contents: #(
5 3 0
6 0 0
0 9 8 )! !
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20200219/f33b32a8/attachment.html>


More information about the Squeak-dev mailing list