how to define a structural type that refers to itself?

Posted by IttayD on Stack Overflow See other posts from Stack Overflow or by IttayD
Published on 2010-04-18T04:24:51Z Indexed on 2010/04/18 4:33 UTC
Read the original article Hit count: 275

Filed under:

I want to create a method sum that I can call on different types, specifically sum(1,2).

def sum[A](a1: A, a2: A) = a1 + a2

This fails because the compiler can't tell if A has a method '+'

I tried to define a structural type:

type Addable = {def +(a: Addable)}

This fails because of an illegal cyclic reference

How can I achieve this in a type safe way without requiring A to extend a specific trait?

© Stack Overflow or respective owner

Related posts about scala