Uploaded image for project: 'Scala Programming Language'
  1. Scala Programming Language
  2. SI-4389

Calling Traversable#map from java: impossible?

    Details

    • Type: Bug
    • Status: CLOSED
    • Priority: Major
    • Resolution: Not a Bug
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: Compiler (Misc)
    • Labels:
      None

      Description

      Chosen as a sample of the impressive difficulty of calling into scala. I can't make this compile at all. Is there a way to make it work? If there is not, is it due to the signature? If nontrivial things aren't going to work anyway then there's no point in suffering so much for the cause.

      import scala.*;
      import scala.collection.Traversable;
      import scala.collection.Traversable$$;
      import scala.collection.generic.CanBuildFrom;
       
      class A { };
      class B { };
       
      public class J {
        // The generic signature we are trying to implement:
        //
        // public abstract <B,That> That scala.collection.TraversableLike.map(scala.Function1<A, B>,scala.collection.generic.CanBuildFrom<Repr, B, That>)
        
        public void sum(Traversable<A> x) {
          // 1) This way, or any other deviation from 2), and we get "incompatible types"
          // at the declaration.
          CanBuildFrom<Traversable<A>, B, Traversable<B>> cbf1 = Traversable$$.MODULE$$.<B>canBuildFrom();
          // 2) This way, we get "cannot find symbol" at the call site.
          CanBuildFrom<Traversable<?>, B, Traversable<B>> cbf2 = Traversable$$.MODULE$$.<B>canBuildFrom();
          // A function.
          Function1<A, B> f = new scala.runtime.AbstractFunction1<A, B>() {
            public B apply(A a) {
              return null;
            }
          };
          x.map(f, cbf1);
          x.map(f, cbf2);
          
          // For our 3) and last trick, we try to take the working part from each of those, and
          // cast the cbf to the type it wants.  And it won't even let me cast.
          x.map(f, (CanBuildFrom<Traversable<A>, B, Traversable<B>>)cbf2);
       
          // Error 1:
          // J.java:12: incompatible types
          // found   : scala.collection.generic.CanBuildFrom<scala.collection.Traversable<?>,B,scala.collection.Traversable<B>>
          // required: scala.collection.generic.CanBuildFrom<scala.collection.Traversable<A>,B,scala.collection.Traversable<B>>
          //     CanBuildFrom<Traversable<A>, B, Traversable<B>> cbf1 = Traversable$$.MODULE$$.<B>canBuildFrom();
          //                                                                                                ^
       
          // Error 2:
          // J.java:23: cannot find symbol
          // symbol  : method map(scala.Function1<A,B>,scala.collection.generic.CanBuildFrom<scala.collection.Traversable<?>,B,scala.collection.Traversable<B>>)
          // location: interface scala.collection.Traversable<A>
          //     x.map(f, cbf2);
          //      ^
          
          // Error 3:
          // J.java:43: inconvertible types
          // found   : scala.collection.generic.CanBuildFrom<scala.collection.Traversable<?>,B,scala.collection.Traversable<B>>
          // required: scala.collection.generic.CanBuildFrom<scala.collection.Traversable<A>,B,scala.collection.Traversable<B>>
          //     x.map(f, (CanBuildFrom<Traversable<A>, B, Traversable<B>>)cbf2);
          //                                                               ^
          // 3 errors
          // 
       
          x.map(f, null); // "works"
        }
        
        // Nope, not these either
        //
        // CanBuildFrom<Traversable, B, Traversable<B>> cbf3 = Traversable$$.MODULE$$.<B>canBuildFrom();
        // CanBuildFrom<? super Traversable<A>, B, Traversable<B>> cbf4 = Traversable$$.MODULE$$.<B>canBuildFrom();
        // CanBuildFrom<Object, B, Traversable<B>> cbf5 = Traversable$$.MODULE$$.<B>canBuildFrom();
        // CanBuildFrom<Traversable<?>, B, Traversable> cbf6 = Traversable$$.MODULE$$.<B>canBuildFrom();
        // 
        // You can try explicit parameterization, it won't help.
        //
        // x.<B, Traversable<B>>map(f, cbf2);
      }

        Attachments

          Activity

            People

            • Assignee:
              odersky Martin Odersky
              Reporter:
              extempore Paul Phillips
              TracCC:
              Paul Phillips, Seth Tisue
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: