Mantis Bugtracker
  

Viewing Issue Simple Details Jump to Notes ] View Advanced ] Issue History ] Print ]
ID Category Severity Reproducibility Date Submitted Last Update
0005319 [Squeak] Kernel feature always 10-29-06 03:36 01-22-08 05:31
Reporter Keith_Hodges View Status public  
Assigned To
Priority normal Resolution open  
Status new   Product Version 3.9
Summary 0005319: Please add #askFor: to Object
Description Very useful function

#askFor: selector

^(self respondsTo: selector) ifTrue: [ self selector ] ifFalse: [ false ]

Additional Information
Attached Files  Object-askFor.st [^] (257 bytes) 10-29-06 03:37
 Object-askFor.2.st [^] (260 bytes) 10-31-06 04:12
 Object-askFor.3.st [^] (243 bytes) 10-31-06 13:32
 ObjectTest-testAskFor.st [^] (352 bytes) 10-31-06 14:17
 Object-askForifAbsent.st [^] (283 bytes) 12-18-06 12:24
 ObjectTest-testAskForIfAbsent.st [^] (406 bytes) 12-18-06 12:24
 Object-askForTests.1.cs [^] (1,446 bytes) 01-06-07 06:02
 Object-askForifAbsent.1.st [^] (443 bytes) 01-06-07 06:02
 Object-askFor.4.st [^] (343 bytes) 01-06-07 06:03
 Object-askFor.5.st [^] (343 bytes) 01-09-07 09:22
 Object-askFor.6.st [^] (337 bytes) 01-09-07 09:26
 Object-askForTests.2.cs [^] (848 bytes) 01-09-07 09:29
 AskFor.1.cs [^] (1,497 bytes) 01-21-08 22:31

- Relationships

- Notes
(0007941 - 829 - 973 - 973 - 1089 - 1089 - 1089)
wiz
10-30-06 06:53

As written I don't think this will work as intended.

There is name confusion here.

as uploaded it reads:


'From Squeak3.9gamma of ''23 July 2006'' [latest update: 0007066] on 29 October 2006 at 1:24:44 am'!

!Object methodsFor: 'testing' stamp: 'kph 10/28/2006 15:28'!
askFor: selector

^(self respondsTo: #selector) ifTrue: [ self selector ] ifFalse: [ false ]! !

which has a different meaning than what was written above.


And I take the meaning as if self is an instance of a class that responds to the selector #selector then [ and I'd be very curious to know how the compiler handles the if true clause ] else return false.

So this won't work no how.

To see something that will look at the implementors containing #perform .
And read more smalltalk literature.

Yours in service, -- Jerome Peace
 
(0008010 - 1320 - 1557 - 1557 - 1676 - 1676 - 1676)
wiz
10-31-06 08:00

The second attempt reads:


'From Squeak3.9gamma of ''23 July 2006'' [latest update: 0007065] on 31 October 2006 at 12:54:39 am'!

!Object methodsFor: 'testing' stamp: 'kph 10/31/2006 00:53'!
askFor: selector
    ^ (self respondsTo: selector)
        ifTrue: [ self selector] ifFalse: [ false ]! !


You need to prove to someone that this works.

Try 2 things
first change arguement name to something that is not also a method name. (the name #selector has several implementors in 7066)

and then see if it will actually compile.

Then see what it does when compiled.

I would never use the true clause you have selected. It probably does not work as you intend. And even if it does the fact that a code reader would be confused by the possible ambiguity of the clause makes it something to steer away from.

The other important thing to do here is to write some test code or do-its to prove that it works as you intend. Always prove to yourself that your code works before you try proposing it to others. Save yourself some embarassment.

Coding is like pinball. Only if you do it well do you get a chance to do it again.

To me this code points out a lot of weaknesses in the coder. You are going to have to work (and study) really hard to live this one down.

Yours in service, --Jerome Peace
 
(0008760 - 795 - 919 - 919 - 919 - 919 - 919)
black
12-27-06 08:54

This seems to have some utility, but I'm not convinced that it is worth its weight.

It can work only with a unary selector, and is useful only if the answer could not possibibly be false.

    try: aSelector ifNotUnderstood: aBlock

would be a bit more useful.

    try: aSelector withArguments: aCollection ifNotUnderstood: aBlock

would be more useful still, but it would be much less readable than the code that just askes the respondsTo: question and then sends the message.

In addition, respondsTo: only checks if the selector is in the method dictionary. The method might be abstract, in which case it would still not be understood. Or there might be clever code in the doesNotUnderstand: method ...

the overall outcome might be that this method adds more confusion.
 
(0008834 - 62 - 74 - 74 - 74 - 74 - 74)
Keith_Hodges
01-05-07 20:31

Example of use

(myObject askFor: #isMyModel) ifTrue:[ ... ]
 
(0008836 - 722 - 824 - 824 - 824 - 824 - 824)
wiz
01-06-07 01:27

Hi Keith,

I still don't see this method's worth.

I would either write:

(myObject respondsTo: #isMyModel) and: [ myObject isMyModel] .

or I would define a backstop for isMyModel in Object.

The main negative for me is that askFor: returns a boolean but does not guarentee the return of a boolean. That increases its surface area and increases the care with which it needs to be employed.

As one who finds bugs by reading code, I value clarity above all else.
Methods that are easy to follow and easy to understand. And maintainence is hard enough without introducing tricky code.

I still think you should give this one up to the learning experience and leave it go.

Yours in service, --Jerome Peace
 
(0008841 - 1612 - 1726 - 1726 - 1726 - 1726 - 1726)
Keith_Hodges
01-06-07 04:02
edited on: 01-06-07 06:06

Simply because.

1. you dont want to always have to add #is... methods to Object
1b as a user of a sub-system I am not always the provider of the sub-system. As the user it is not my responsibility to provide #is... methods on Object for everything that I might use from someone else. (see 3 for the providers options)

2. In the past it was part of another smalltalk dialect and it was genuinely useful
3. In a system where parts are liberally going to be ripped out it is a useful mechanism that is tolerant of bits that may or may not be present. At present there is only one tolerant mechanism in place i.e. #respondsTo: this is too generic and less readable for this use-case. #askFor: enables a more specific idiom. As a provider of a sub-system, I can if I wish provide #is... methods on object. But what if my sub-system, or another related sub-system that I am intending to use is not loaded by the user at run time.

If I am going to contribute to 3.10 I want this to be present, because I think it may be key to a number of ideas I have in mind. If it turns out to be worthless then by all means remove it after testing has shown that it is not needed. If I have anything to do with 3.10 then its in, at least as an experiment.

If you want #askFor: can be constrained to return a boolean. ( Object-askFor.4.st implements this)

The usefulness of #askFor:ifAbsent: would really be curtailed if similarly constrained.

surname := myFriend askFor: #surname ifAbsent: [ 'Smith' ]

would be
surname := (myFriend respondsTo: #surname) ifTrue: [ myFriend surname ] ifFalse: [ 'Smith' ].

 
(0008846 - 975 - 1095 - 1095 - 1095 - 1095 - 1095)
wiz
01-07-07 02:07

Ok.

I'll buy that :

surname := myFriend askFor: #surname ifAbsent: [ 'Smith' ] .

would be useful.

And I have no need for it to be constrained. Reading the if absent part clues you in to the answers type.

I stand by my reluctance for #askFor: It returns false and not nil. That makes it a hybrid and will temp the building of foggy code.
I've come across enough foggy code bugs to last a lifetime.

Constraining it to a boolean would curtail its usefulness.
Have it return nil and I won't have much objection left.

[OT] Its been easy to notice your energy and enthusiam for contributing to squeak. That's why I bothered responding early in the first place. That energy is most useful if you accompany it with clarity in your code and in your communications. The more fully you've explained things the more I've understood. Please take time to do more of that explaining as you continue to contribute.

Cheers.

Yours in service, --Jerome Peace
 
(0008857 - 579 - 698 - 698 - 698 - 698 - 698)
Keith_Hodges
01-09-07 07:46

> Please take time to do more of that explaining as you continue to contribute.

sure, as long as I am not making it up as I go along :-)

options: "a) value or nil" "b)true or false"

option a) makes

isItTheRightType = (myObject askFor: #isEric) ifNil: [ false ]

virtually equivalent to askFor:ifAbsent: making that one redundant.

(myObject askFor: #isEric) ifNotNilDo: [ :anEric | ... ]

So we would settle on askFor: returning value or nil, and forget askFor:ifAbsent:

(if I could only remember the original behaviour it would make this all a bit easier)
 
(0008858 - 501 - 573 - 573 - 573 - 573 - 573)
wiz
01-09-07 09:15

Hi Keith,

As a design value or nil is more flexible than true or false.

Also askFor: #method ifAbsent: [ ] should be implemented.

It is not the same as #askFor:

It will distinguish between the case where the #method is present and when it is not. You may want that to have side effects on the way to delivering the answer.

You can define #askFor: in terms of askFor: #method ifAbsent:[ ] but not the other way around. The pattern abounds in squeak.

Yours in service, --Jerome Peace
 
(0011693 - 138 - 150 - 150 - 150 - 150 - 150)
Keith_Hodges
01-21-08 22:20
edited on: 01-21-08 22:32

And as soon as I hit a use case... if find that a) value or nil, just is not the behavior that I am looking for... I want true or false.

 
(0011698 - 793 - 949 - 949 - 949 - 949 - 949)
wiz
01-22-08 05:31
edited on: 01-22-08 05:37

Hi Keith,
>And as soon as I hit a use case... if find that a) value or nil, just is not the behavior that I am looking for... I want true or false.

2) You are being too terse for me to understand. What is your use case?

If your use case entails askfor returning true or false when the selector is understood then:

(self askFor: #isItCodeYet ) == true
  ifTrue: [...]
 ifFalse: [...] .

would seem to me to cover it.

Secondly, ProtoObject does not understand #perform: .

It is probably still bad form to place a method in a class with does not inherit the protocol.

It would be more elegant to define this in Object where #perform: is known.

Thank you for including #askFor:ifAbsent: that to my mind is still the more useful form of this behavior.

Cheers, -Jer

 

- Issue History
Date Modified Username Field Change
10-29-06 03:36 Keith_Hodges New Issue
10-29-06 03:37 Keith_Hodges File Added: Object-askFor.st
10-30-06 06:53 wiz Note Added: 0007941
10-31-06 04:12 Keith_Hodges File Added: Object-askFor.2.st
10-31-06 08:00 wiz Note Added: 0008010
10-31-06 13:32 Keith_Hodges File Added: Object-askFor.3.st
10-31-06 14:17 Keith_Hodges File Added: ObjectTest-testAskFor.st
12-18-06 05:47 Keith_Hodges Note Added: 0008720
12-18-06 05:57 Keith_Hodges Note Edited: 0008720
12-18-06 05:59 Keith_Hodges Note Edited: 0008720
12-18-06 05:59 Keith_Hodges Note Edited: 0008720
12-18-06 08:35 Keith_Hodges Note Edited: 0008720
12-18-06 08:36 Keith_Hodges Note Edited: 0008720
12-18-06 08:36 Keith_Hodges Note Edited: 0008720
12-18-06 10:08 Keith_Hodges Note Edited: 0008720
12-18-06 10:10 Keith_Hodges Note Edited: 0008720
12-18-06 10:44 Keith_Hodges Note Edited: 0008720
12-18-06 12:24 Keith_Hodges File Added: Object-askForifAbsent.st
12-18-06 12:24 Keith_Hodges File Added: ObjectTest-testAskForIfAbsent.st
12-18-06 12:26 Keith_Hodges Note Edited: 0008720
12-18-06 12:26 Keith_Hodges Note Edited: 0008720
12-27-06 08:54 black Note Added: 0008760
01-05-07 20:31 Keith_Hodges Note Added: 0008834
01-06-07 01:27 wiz Note Added: 0008836
01-06-07 04:02 Keith_Hodges Note Added: 0008841
01-06-07 04:08 Keith_Hodges Note Edited: 0008841
01-06-07 04:18 Keith_Hodges Note Edited: 0008841
01-06-07 06:02 Keith_Hodges File Added: Object-askForTests.1.cs
01-06-07 06:02 Keith_Hodges File Added: Object-askForifAbsent.1.st
01-06-07 06:03 Keith_Hodges File Added: Object-askFor.4.st
01-06-07 06:06 Keith_Hodges Note Edited: 0008841
01-06-07 06:07 Keith_Hodges Note Edited: 0008720
01-06-07 06:09 Keith_Hodges Note Edited: 0008720
01-06-07 06:11 Keith_Hodges Note Edited: 0008720
01-07-07 02:07 wiz Note Added: 0008846
01-09-07 07:46 Keith_Hodges Note Added: 0008857
01-09-07 09:15 wiz Note Added: 0008858
01-09-07 09:22 Keith_Hodges File Added: Object-askFor.5.st
01-09-07 09:23 Keith_Hodges Note Edited: 0008720
01-09-07 09:26 Keith_Hodges File Added: Object-askFor.6.st
01-09-07 09:29 Keith_Hodges File Added: Object-askForTests.2.cs
01-09-07 09:30 Keith_Hodges Note Edited: 0008720
02-03-07 02:44 Keith_Hodges Note Edited: 0008720
01-21-08 22:20 Keith_Hodges Note Added: 0011693
01-21-08 22:31 Keith_Hodges File Added: AskFor.1.cs
01-21-08 22:31 Keith_Hodges Note Edited: 0008720
01-21-08 22:32 Keith_Hodges Note Edited: 0011693
01-21-08 23:48 Keith_Hodges Note Edited: 0008720
01-22-08 05:31 wiz Note Added: 0011698
01-22-08 05:37 wiz Note Edited: 0011698
01-22-08 10:38 Keith_Hodges Note Edited: 0008720
01-22-08 10:39 Keith_Hodges Note Edited: 0008720
01-22-08 10:46 Keith_Hodges Note Deleted: 0008720


Mantis 1.0.8[^]
Copyright © 2000 - 2007 Mantis Group
185 total queries executed.
51 unique queries executed.
Powered by Mantis Bugtracker