Aditya Kappagantula
Aditya Kappagantula

Reputation: 562

Smalltalk - Is there a way to determine the data type?

Is there a way to determine the datatype of an already initialized variable in smalltalk?

Eg:

|abc|
abc := #(1 2 3 4 5 6)  'This is the array declared'
(abc isKindOf: Array) ifTrue: [ 'Check the data type of abc against array datatype'
    ^'Success!'
]

Reason for the request: I need to implement the a method only if it is called by an specific datatype.

Upvotes: 0

Views: 798

Answers (3)

Alexandre Bergel
Alexandre Bergel

Reputation: 108

There is also a frequent idiom in Smalltalk. Define the method isArray on Object that returns false; define isArray on the class Array that returns true. Like that, you are able to send #isArray to any object. But again, as said above, this idiom highlights a suboptimal design.

Upvotes: 2

Aditya Kappagantula
Aditya Kappagantula

Reputation: 562

Simply add the method to the datatype Array. i.e to the class Array.

Then only an instance of an Array will be able to call it.

Upvotes: 1

Uko
Uko

Reputation: 13396

If I understand your question correctly, you can use

abc isMemberOf: Array

or

abc class == Array

This checks if abc is an instance of the Array class ( a thing that you call datatype).

Also maybe

abc respondsTo: #message

can be useful for you as it checks whether method called message is defined for abc.

Upvotes: 3

Related Questions