Discreet Cosine Transform

A few thoughts on ruby, video and other things

Python, Ruby and Dart Part 2.2: Does an Object Implement a Given Interface

A bit more useful then just determining the class of an object is to see if an object responds to a specific method. This is a form of polymorphism for dynamically typed languages, where for example a method could take in any object so long as that object responds to a call on read.

Implements a given interface could probably be interpreted a few ways, for example you might wonder in Ruby if a given class includes a specific mixin. Or in Python, which supports multiple inheritance, all the parent classes a given class descends from. There are ways to do just that in both, if that is what you need.

What I want to look at today though is specifically if a given name is callable on a given object. This is a bit of a lowest common denominator approach - it wouldn’t matter if an object acquired that method via inheritance, mixin, or direct implementation.

Ruby

The way to do this in Ruby is well known:

1
2
3
object1 = "test"

puts object1.respond_to?(:length) #prints "true"

Python

Looks like Python has a very approximate way to do this, though its split into two steps:

1
2
3
object1 = "test"
print hasattr(object1, 'capitalize') #prints "True"
print callable(object1.capitalize) #prints "#True"

So, hasattr only tells you that object1 has an attribute named capitalize, it doesn’t yet tell you if you can call that attribute like a method. I believe as an alternative you can jump right to callable and just be ready to trap an AttributeError.

Dart

Dart is not quite so cut and dry as the two examples above. This is because Dart, while having the option of being dynamically typed, has a bit more of a classical type system then Python or Ruby. By this I mean you have the option in Dart to be more explicit that a specific class implements a specific interface. It supports abstract classes, inheritance and mixins. It also supports strong typing on method invocation, so the need to query an object to insure it implements a specific method probably is not needed. You can even make the type you check against in the method declaration be an interface or parent class, again, as with a more traditional, strong type system.

But, for sake of 1:1, let us see if we can figure out if an object supports calling a specific method on it:

1
2
3
4
5
6
7
import 'dart:mirrors';

void main() {
  String object1 = 'test';
  InstanceMirror object_mirror = reflect(object1);
  print(object_mirror.type.instanceMembers.containsKey(#length)); //prints "true"
}

That said, Dart’s is keyword seems pretty useful, as it checks if an object implements a specified interface:

1
2
3
4
5
6
7
void main() {
  String object1 = 'test';

  print(object1 is String); //prints "true"
  print(object1 is Comparable); //also prints "true" because String implements Dart's Comparable interface

}

Again, because Dart has a bit more classical idea of “interface”, this seems more useful to me then for example detecting if a Ruby class includes a specified module.

Comments