Maintenance - We're currently working on things and you might experience some issues. Should be wrapped up soon!

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

2
Is it possible for a trait to define a method only if used in combination with another trait?
Post Body

Simplified example—suppose I have the following traits:

trait Operator {
  type T
  def apply(x: T, y: T): T
  def identity: Option[T] = None
}

trait Addition extends Operator {
  def add(x: T, y: T): T = apply(x, y)
  def zero: Option[T] = identity
}

trait Multiplication extends Operator {
  def times(x: T, y: T): T = apply(x, y)
  def one: Option[T] = identity
}

Now instead of defining identity as an Option, I want to move it from the Operator trait to a new Identity trait:

trait Operator {
  type T
  def apply(x: T, y: T): T
}

trait Identity extends Operator {
  def identity: T
}

How could I modify Addition and Multiplication so that instead of being Options, one and zero aren’t defined at all unless an operation extends "Addition with Identity" or "Multiplication with Identity"?

Author
Account Strength
100%
Account Age
18 years
Verified Email
Yes
Verified Flair
No
Total Karma
313,973
Link Karma
65,427
Comment Karma
246,756
Profile updated: 9 hours ago
Posts updated: 7 months ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
9 years ago