You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@spark.apache.org by Sa...@wellsfargo.com on 2015/08/25 15:25:11 UTC

Scala: Overload method by its class type

Hi all,

I have SomeClass[TYPE] { def some_method(args: fixed_type_args): TYPE }

And on runtime, I create instances of this class with different AnyVal + String types, but the return type of some_method varies.

I know I could do this with an implicit object, IF some_method received a type, but in this case, I need to have the TYPE defined on its class instance, so for example:

val int_instance = new SomeClass[Int]
val str_instance = new SomeClass[String]
val result: Boolean = int_instance.some_method(args) > 0       <--- I expected INT here
val result2: Boolean = str_instance.som_method(args) contains "asdfg"     <---- I expected STRING here.

without compilation errors.

Any ideas? I would like to implement something like this:

class SomeClass[TYPE] {

def some_method(args: Int): Int = {
process_integer_overloaded_method
}

def some_method(args: Int): String = {
process_string_overloaded_method
}

and so on.

Any ideas? maybe store classe's TYPE in a constructor instead as a variable somehow?

Thanks
Saif


Re: Scala: Overload method by its class type

Posted by Jonathan Coveney <jc...@gmail.com>.
What error are you seeing? Becasue while it's not the most idiomatic scala,
it should work. Can you give an example of an actual usage with the
compiler error?

2015-08-25 9:25 GMT-04:00 <Sa...@wellsfargo.com>:

> Hi all,
>
> I have SomeClass[TYPE] { def some_method(args: fixed_type_args): TYPE }
>
> And on runtime, I create instances of this class with different AnyVal +
> String types, but the return type of some_method varies.
>
> I know I could do this with an implicit object, IF some_method received a
> type, but in this case, I need to have the TYPE defined on its class
> instance, so for example:
>
> val int_instance = new SomeClass[Int]
> val str_instance = new SomeClass[String]
> val result: Boolean = int_instance.some_method(args) > 0       <--- I
> expected INT here
> val result2: Boolean = str_instance.som_method(args) contains “asdfg”
> <---- I expected STRING here.
>
> without compilation errors.
>
> Any ideas? I would like to implement something like this:
>
> class SomeClass[TYPE] {
>
> def some_method(args: Int): Int = {
> process_integer_overloaded_method
> }
>
> def some_method(args: Int): String = {
> process_string_overloaded_method
> }
>
> and so on.
>
> Any ideas? maybe store classe’s TYPE in a constructor instead as a
> variable somehow?
>
> Thanks
> Saif
>
>

Re: Scala: Overload method by its class type

Posted by Akhil Das <ak...@sigmoidanalytics.com>.
This is more of a scala related question, have a look at the case classes
in scala http://www.scala-lang.org/old/node/107

Thanks
Best Regards

On Tue, Aug 25, 2015 at 6:55 PM, <Sa...@wellsfargo.com> wrote:

> Hi all,
>
> I have SomeClass[TYPE] { def some_method(args: fixed_type_args): TYPE }
>
> And on runtime, I create instances of this class with different AnyVal +
> String types, but the return type of some_method varies.
>
> I know I could do this with an implicit object, IF some_method received a
> type, but in this case, I need to have the TYPE defined on its class
> instance, so for example:
>
> val int_instance = new SomeClass[Int]
> val str_instance = new SomeClass[String]
> val result: Boolean = int_instance.some_method(args) > 0       <--- I
> expected INT here
> val result2: Boolean = str_instance.som_method(args) contains “asdfg”
> <---- I expected STRING here.
>
> without compilation errors.
>
> Any ideas? I would like to implement something like this:
>
> class SomeClass[TYPE] {
>
> def some_method(args: Int): Int = {
> process_integer_overloaded_method
> }
>
> def some_method(args: Int): String = {
> process_string_overloaded_method
> }
>
> and so on.
>
> Any ideas? maybe store classe’s TYPE in a constructor instead as a
> variable somehow?
>
> Thanks
> Saif
>
>