Question

I’ve just watched this talk by Daniel Spiewak where he talks about the advantages of structural typing as compared to Scala’s ans Java’s nominal typing. One example for this difference would be the following Java code

public interface Foo {
  public int length();
}
public interface Bar {
  public int length();
}

Foo f = ...;
Bar b = f;

which of course would not compile because type compatibility between Foo and Bar is determined by name.

A structural type system on the other hand could declare both types being equal or compatible and thus, amongst other things, allow for checked duck typing.

Now I think I do understand most of the advantages of a structural type system but I wonder if it would not invalidate type safety from examples like the following

class Foo {
  class Bar { /* ... */ }
  def takeBar(b: Bar) = { /* ... */ }
  def getBar: Bar = new Bar
}

val foo1 = new Foo
val foo2 = new Foo
foo1.takeBar(foo1.getBar) // should compile
foo1.takeBar(foo2.getBar) // should not compile

Is my understanding correct that in a structural type system the last line would compile as well and if so, wouldn’t this be a disadvantage with respect to type safety?

No correct solution

Licensed under: CC-BY-SA with attribution
scroll top