alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (t2060.scala)

This example Scala source code file (t2060.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

double, double, i, intop, intop, op, op, rich, rich, test

The Scala t2060.scala source code

/* The problem here is that we cannot insert an implicit to
 * add a polymorphic method which is then instantiated to the
 * right type. When searching for the implicit in the `fails to compile
 * line':
 *
 *   val failure = 1.0 + new Op[Int]
 * 
 * we reduce the problem to finding a function from Double to
 * {+: _ >: Op[Int] <: Any}, that is, a method which takes
 * an argument which is an Op[Int] or a supertype thereof.
 * Class Rich is not a subtype of this structural record, because
 * polymorphic method instantiation is not contained in subtyping.
 * That is: The method type [I](op : Op[I]): Op[I] is not a subtype
 * of (Op[Int]): Op[Int].
 * At present it is unclear whether this problem can be solved.
 */
object Test {
  class Op[I];
  class IntOp extends Op[Int];

  class Rich(x : Double) {
    def +       (op : IntOp): IntOp = op;
    def +    [I](op : Op[I]): Op[I] = op;
    def plus [I](op : Op[I]): Op[I] = op;
  }

  implicit def iToRich(x : Double) =
    new Rich(x);

  // fails to compile
  val x = 1.0 + new Op[Int]

  // works as expected --
  //   problem isn't in adding new "+"
  val a = 1.0 + new IntOp;

  // works as expected --
  //   problem isn't in binding type variable I
  val b = 1.0 plus new Op[Int];

  // works as expected --
  //   problem isn't in using Rich.+[I](op : Op[I])
  val c = iToRich(1.0) + new Op[Int];
}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala t2060.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.