简体   繁体   中英

Smalltalk x raisedTo: y error

Good day! I Have my report tomorrow and i'm reviewing about Smalltalk. I tried to use the raisedTo: method but it gives me this error:

 MessageNotUnderstood: Character>>raisedTo:

Here's my code:

|x y z|
x := UIManager default request: 'P1: '.
y := UIManager default request: 'P2: '.
z := x raisedTo: y.
self inform: z.

Try the following:

|x y z|
x := UIManager default request: 'P1: '.
y := UIManager default request: 'P2: '.
z := x asNumber raisedTo: y asNumber.
self inform: z asString.

Note how the selectors #asNumber and #asString convert objects to the proper types.

Smalltalk is dynamically typed, but that does not mean you can pass any type of object to a method.

Your code performs #raisedTo: on x. However, x is a String, not a subclass of Number where #raisedTo: is implemented. So you initial error is caused by String not understanding #raisedTo:. (You can check where #raisedTo: is implemented by using the “Method finder” under the Tools menu.) I correct this by sending #asNumber to x.

Likewise, the argument you send to #raisedTo: must also be a number. Here the correction is the same; send #asNumber to y.

Finally, #inform: expects a String, not a number. The correction here is to send #asString to the number.

Note how #asString and #asNumber will not change the object you send the message to. Instead a new object of the appropriate type is answered.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM