0

I have a large package of models which I'd like cleaning up. So basically, a model looks like;

case class ExampleModel(…) extends Model {
    …
}

object ExampleModel {
    def find = new Finder[Long, ExampleModel](classOf[Long], classOf[ExampleModel]) // very repetitive
    …
}

Now, I've been trying to define an abstract class that provides me a companion object specific find method;

abstract class Findable[T: ClassTag] {
  self: T => def find = new Finder[Long, T](classOf[Long], classOf[T])
}

But compilation fails with:

class type required but T found

What is the correct way to approach this?

Ashesh
  • 2,978
  • 4
  • 27
  • 47

2 Answers2

3

You can't use T as self-type as it's unknown at the moment of class definition (same as you can't write class A[T] extends T), also you don't need to pass classOf when you have ClassTag, so should be enough to:

scala> class Finder[A: ClassTag, B: ClassTag](){def getA = classTag[A].runtimeClass}
defined class Finder

scala> abstract class Findable[T: ClassTag] {def find = new Finder[Long, T]}
defined class Findable

And finally:

scala> :paste
// Entering paste mode (ctrl-D to finish)

case class ExampleModel()
object ExampleModel extends Findable[ExampleModel]


// Exiting paste mode, now interpreting.

defined class ExampleModel
defined object ExampleModel

Note that T is case class ExampleModel here, but you could simply obtain the companion

Community
  • 1
  • 1
dk14
  • 22,206
  • 4
  • 51
  • 88
  • What if I don't want to define my own `Finder`? – Ashesh Jan 12 '15 at 11:42
  • then use it as is - `def find = new Finder[Long, T](classOf[Long], classTag[T].runtimeClass)` – dk14 Jan 12 '15 at 12:22
  • if you need the companion object instead of case class - you could just obtain it using JavaReflection's Class.forName, naming convention here - http://stackoverflow.com/questions/27629215/how-can-i-provide-a-scala-companion-objects-class-to-java – dk14 Jan 12 '15 at 12:26
0

why not just

class Findable[T](clazz: Class[T]) {
     def find(clazz: Class[T]) = new Finder(classOf[Long], clazz)
}

object ExampleModel extends Findable(classOf[ExampleModel]) 
Dima
  • 39,570
  • 6
  • 44
  • 70
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – bardiir Jan 12 '15 at 13:33
  • @bardiir what do you mean "it does not provide the answer"??? Did you read it at all? What aspect of " an answer" is it lacking in your view? – Dima Jan 12 '15 at 14:13
  • 1
    Oh, come on. A "question" is rhetorical. "Why not do this?" is just a polite form of saying "do this". Is this linguistics forum now? And this is the correct approach, because it compiles and works, which is what the OP was asking about. The comments are *not* the correct way to do this. Hover your mouse over "add comment", to see why. – Dima Jan 12 '15 at 14:31
  • 1
    @Dima - if you remember I also mentioned the same thing when I commented on another answer of yours. When you give an answer try to phrase it not as a question and explain it rather than just write code. This will make your answers more SO's compliant and you'll avoid having them sent to the closing votes. – Avi Jan 12 '15 at 14:31
  • @Avi, yes I remember. You were wrong there too. It has been a few decades since somebody taught me how to phrase my sentences, thank you very much. – Dima Jan 12 '15 at 14:33
  • 1
    @Dima - I'm not saying this as to offend you personally. It's just a fact that your answers are repeatedly get close votes. I said it in order to help you figure out why. Instead of having the need to argue with each specific close-voter at a time, just try to understand that there's a reason it happens. – Avi Jan 12 '15 at 14:35
  • @Avi I don't mind arguing with people when they are wrong. If you don't want to be argued with, next time try to read and *understand* both the question and the answer before posting a comment. An answer does not have to be long or end with a period to answer the question. It is not about length, and not about form, but about content, and usefulness. The OP asked how to do a certain thing, I showed him an approach that works. The SO bot brought it up for a vote, because it is shorter than some threshold. The bot does not care about the content, but people should.Don't act like a bot next time. – Dima Jan 12 '15 at 14:39
  • 1
    @Dima - Ok, I really don't want to get into an argument here. I saw your name coming up again and really wanted to help you figure out why you get close votes. In SO the format of the question/answer is also important and not only the content. Just like in Wikipedia, which is edited by the community, not everything goes because there are some standards. Have a good luck arguing with each person voting to delete your answers separately. I tried to help, that's it. Even if you convince me, you'll have thousands more people to convince. – Avi Jan 12 '15 at 14:42
  • 2
    @Dima - Yes it's about usefulness and this answer is imho not really useful for any purpose than solving this exact issue, there is no explanation or possibility to learn from it, thus it might be worthless for other people that might come accross the question by using google trying to figure it out too. Thus the downvote and automated comment which comes included with the downvote. True, it's not all about form, but some sort of explanation never hurts ;) – bardiir Jan 12 '15 at 14:43
  • @Avi like I said, I *know* why I get close votes (because I know how bots work). I also know why the posts flagged by the bots are not just deleted automatically - that's because bots are often wrong. And so are people that act like bots. – Dima Jan 12 '15 at 14:45
  • @bardiir If it is "useful for solving this exact issue", then how can you say, that it "does not provide an answer to the question"? If you find something about it unclear, or in need of further explanation, why don't you ask a specific question (or just edit the answer and improve it if you can) rather than slapping a useless and factually incorrect canned comment on it? Did it make you feel important? – Dima Jan 12 '15 at 14:47
  • @Dima - Yes it sure did, thanks a lot for asking. I sure do feel important in helping to maintain some sort of basic level of helpfulness of answers in a general usability context. The canned comment comes prepackaged with the flag. you'll probably see a lot more of this specific one if you don't change your mind. – bardiir Jan 12 '15 at 16:10
  • @bardiir don't fool yourself. I mentioned some ways you could help, if you were really interested, in my previous comment. What you are doing is not helping anyone or anything at all, and I am sure that you damn well know it (at least, by now). But if it gives you that warm feeling of importance you are after, well ... good for you. – Dima Jan 12 '15 at 16:17