0

I have an abstract superclass with a variety of stateless implementations. It seems like the thing to do is make the abstract superclass a class, and make each implementation an object because I only ever need one of each.

This is a toy example, but it shows the compile error I’m getting:

// Tramsformer.scala

class Transformer {
  def transform(value : String) : String
}

object Transformer {
  getTransformer(String : name) : Transformer = {
    name match {
      case "upper" => UpperTransformer
                      // I'm getting "Cannot Resolve Symbol" on UpperTransformer, 
                      // even though they're in the same package.
      case _ => throw new IllegalArgumentException("...")
    }
  }
}

// ---
// UpperTransformer.scala is in the same package
object UpperTransformer extends Transformer {
  override def transform(vlaue : String) = foo.toUpperCase()
}

I’m really shooting for some sort of dynamic dispatch on (dataProvider, desiredAction) here.

Then some class can call Transformer.getTransformer("upper").transform("stack-overflow") without making any unnecessary objects.

Any idea what I should be doing differently? Should I stop worrying about premature optimization and learn to love the instance?

1 Answer 1

2

The problem isn't visibility, it's that you simply do not define an object named UpperTransformer anywhere - only a class. If you replace class UpperTransformer with object UpperTransformer, your code should work fine.

Sign up to request clarification or add additional context in comments.

3 Comments

@EdBrannin Well, after fixing all your other typos (missing abstract for the Transformer class, value vs. foo, String : name), the code you posted works fine with object instead of class. So you should reconsider what your actual problem is (and make sure that your reproducible compilable code sample, actually compiles and reproduces your problem in the future).
Darn, sorry about that. I'll remake it by redacting the existing code later today.
OK, it turns out both classes started with package main.scala because of some mostly-fixed Maven setup. After fixing that, the classes see each other and it works. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.