structural-typing

Duck typing, must it be dynamic? [CW]

Wikipedia currently says about duck-typing: In computer programming with object-oriented programming languages, duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a sp...

Pattern matching structural types in Scala

Why does this print wtf? Does pattern matching not work on structural types? "hello" match { case s: { def doesNotExist(i: Int, x: List[_]): Double } => println("wtf?") case _ => println("okie dokie") } ...

Namespace scoped aliases for generic types in C#

Let's have a following example: public class X { } public class Y { } public class Z { } public delegate IDictionary<Y, IList<Z>> Bar(IList<X> x, int i); public interface IFoo { // ... Bar Bar { get; } } public class Foo : IFoo { // ... public Bar Bar { get { return null; //... ...

Using Scala structural types with abstract types

I'm trying to define a structural type defining any collection that has an "add" method (for instance, a java collection). Using this, I want to define a few higher order functions that operate on a certain collection object GenericTypes { type GenericCollection[T] = { def add(value: T): java.lang.Boolean} } import GenericTypes._ tra...

Does C# have an equivalent to Scala's structural typing?

In Scala, I can define structural types as follows: type Pressable = { def press(): Unit } This means that I can define a function or method which takes as an argument something that is Pressable, like this: def foo(i: Pressable) { // etc. The object which I pass to this function must have defined for it a method called press() that ...

Optional structural typing possibilty in C++ or anyother language?

In C++ how to tell compiler that Ogre::Vector3 IS_SAME_AS SomeOtherLIB::Vector3 ? I feel that.. in languages like c++ which are not structural typed but there are cases when it makes sense. Normally as game developer when working with 4+ libraries that provide sort or their own Vector3 implementation. The code is littered with ToOgre, T...

Scala - how to define a structural type that refers to itself?

I'm trying to write a generic interpolate method that works on any type that has two methods, a * and a +, like this: trait Container { type V = { def *(t: Double): V def +(v: V): V } def interpolate(t: Double, a: V, b: V): V = a * (1.0 - t) + b * t } This doesn't work though (on Scala 2.8.0.RC7), I get the following er...

Generalized structural type conformance in Scala

I'm interested in the problem of conforming a specific type to a more general structural type. Consider the following examples: trait Sup trait Sub extends Sup type General = { def contra(o: Sub): Unit def co(): Sup def defaults(age: Int): Unit def defaults2(age: Int): Unit def defaults3(first: String): Unit } trait ...

Why do compile-time generative techniques for structural typing prevent separate compilation?

I was reading (ok, skimming) Dubochet and Odersky's Compiling Structural Types on the JVM and was confused by the following claim: Generative techniques create Java interfaces to stand in for structural types on the JVM. The complexity of such techniques lies in that all classes that are to be used as structural types anywhere in the p...

Funny observation about (recursive) structural types in Scala

I needed some recursive structural type in some piece of code using with traits and the structural type as type parameter constraint. It worked fine, but later I learned Scala does not support recursive structural types. So can someone explain me why this works fine: scala> trait Test[M[A] <: { def map[B](f: A => B) : M[B] } ] {} defin...

Why cannot this case of implicit conversions be optimized?

Why cannot Scala optimize the following: a. implicit def whatever[A](a: A) = new { ... } to: b. class some$generated$name(a: A) { ... } implicit def whatever[A](a: A) = new some$generated$name(a) ? Why does it have to use structural typing in this case? I would like Scala compiler to perform this optimization as writing in sty...