Scala, generic tuple
up vote
6
down vote
favorite
I have a generic method that can accept any tuple of any size, the only constraint is that the first element of this tuple should be of type MyClass
.
Something like this:
trait MyTrait[T <: (MyClass, _*)] {
getMyClass(x: T): MyClass = x._1
}
I've tried this
trait MyTrait[T <: (MyClass, _) with (MyClass, _, _) with (MyClass, _, _) with ...] {
getMyClass(x: T): MyClass = x._1
}
but I get the error unboud wildcard type
scala generics
add a comment |
up vote
6
down vote
favorite
I have a generic method that can accept any tuple of any size, the only constraint is that the first element of this tuple should be of type MyClass
.
Something like this:
trait MyTrait[T <: (MyClass, _*)] {
getMyClass(x: T): MyClass = x._1
}
I've tried this
trait MyTrait[T <: (MyClass, _) with (MyClass, _, _) with (MyClass, _, _) with ...] {
getMyClass(x: T): MyClass = x._1
}
but I get the error unboud wildcard type
scala generics
add a comment |
up vote
6
down vote
favorite
up vote
6
down vote
favorite
I have a generic method that can accept any tuple of any size, the only constraint is that the first element of this tuple should be of type MyClass
.
Something like this:
trait MyTrait[T <: (MyClass, _*)] {
getMyClass(x: T): MyClass = x._1
}
I've tried this
trait MyTrait[T <: (MyClass, _) with (MyClass, _, _) with (MyClass, _, _) with ...] {
getMyClass(x: T): MyClass = x._1
}
but I get the error unboud wildcard type
scala generics
I have a generic method that can accept any tuple of any size, the only constraint is that the first element of this tuple should be of type MyClass
.
Something like this:
trait MyTrait[T <: (MyClass, _*)] {
getMyClass(x: T): MyClass = x._1
}
I've tried this
trait MyTrait[T <: (MyClass, _) with (MyClass, _, _) with (MyClass, _, _) with ...] {
getMyClass(x: T): MyClass = x._1
}
but I get the error unboud wildcard type
scala generics
scala generics
asked 2 hours ago
Gigitsu
14311
14311
add a comment |
add a comment |
6 Answers
6
active
oldest
votes
up vote
2
down vote
Scala can't use generic tuple with unknown size because Products don's inherit themeselfs. You can try to use Shapeless or Products from play json lib.
New contributor
Can you clarify what "don't inherit themselves" means here? I don't see what that means or how it would help.
– Travis Brown
57 mins ago
Hi :-) The asker didn't specify unknown size but rather any size, which is weaker. For example, shapeless HLists (as you relevantly suggest) are definitely not of unknown size since their size is known at compile time :-) As Travis Brown, I can't quite figure what you mean in this answer, clarifying would be very helpful :-)
– C4stor
36 mins ago
add a comment |
up vote
2
down vote
If you want to do this without either boilerplate or runtime reflection, Shapeless is your best bet. You can use the IsComposite
type class to put type-level constraints on the first element of a tuple:
import shapeless.ops.tuple.IsComposite
trait MustBeFirst
class MyClass[P <: Product](p: P)(implicit ev: IsComposite[P] { type H = MustBeFirst })
And then:
scala> new MyClass(good2)
res0: MyClass[(MustBeFirst, String)] = MyClass@5297aa76
scala> new MyClass(good3)
res1: MyClass[(MustBeFirst, String, Int)] = MyClass@3f501844
scala> new MyClass(good4)
res2: MyClass[(MustBeFirst, String, Symbol, Int)] = MyClass@24e15478
scala> new MyClass(bad2)
<console>:15: error: could not find implicit value for parameter ev: shapeless.ops.tuple.IsComposite[(String, Int)]{type H = MustBeFirst}
new MyClass(bad2)
^
If you need to use a trait, you can put the ev
(for "evidence") requirement inside the definition instead of in the constructor:
trait MyTrait[P <: Product] {
implicit def ev: IsComposite[P] { type H = MustBeFirst }
}
Now any class instantiating MyTrait
will have to provide evidence that P
is a tuple with MustBeFirst
as its first element.
Probably nitpicking, but I if read shapeless source correctly, IsComposite will also witness that the tuple is of size at least 2. It may be a relevant detail for the asker since tuples of size 1, however awkward, are a thing ^^
– C4stor
39 mins ago
@C4stor Good point—I'll update the answer to clarify asap.
– Travis Brown
6 mins ago
add a comment |
up vote
0
down vote
Because tuples of differing sizes are (almost) unrelated types, if you need the compiler to type-check the calls to getMyClass()
then you might have to overload it.
trait MyTrait {
def getMyClass(x :(MyClass,_)) :MyClass = x._1
def getMyClass(x :(MyClass,_,_)) :MyClass = x._1
def getMyClass(x :(MyClass,_,_,_)) :MyClass = x._1
def getMyClass(x :(MyClass,_,_,_,_)) :MyClass = x._1
}
add a comment |
up vote
0
down vote
You need to inherit your trait from Product
, through which you can have productIterator
, productArity
and, productElement
to handle the returned value. Here is an example
case class MyClass()
trait MyTrait[T <: Product] {
def getMyClass(x: T): Option[MyClass] =
if(
x.productIterator.hasNext
&&
x.productIterator.next().isInstanceOf[MyClass]
){
Some(x.productIterator.next().asInstanceOf[MyClass])
} else {
None
}
}
case class Test() extends MyTrait[Product]
And you can invoke like this
Test().getMyClass((MyClass(), 1,3,4,5))
//res1: Option[MyClass] = Some(MyClass())
Test().getMyClass((1,3,4,5))
//res2: Option[MyClass] = None
Hope this helps you.
add a comment |
up vote
0
down vote
It's a little bit unsafe but you can use Structural type in this case:
trait MyTrait {
def getMyClass(x: {def _1: MyClass}): MyClass = x._1
}
1
This is compound type or "structural type" which, unlike "Duck typing", has a clearly-defined meaning is Scala.
– Tim
1 hour ago
@Tim, yes thank you I mean structural type.
– mkUltra
55 mins ago
add a comment |
up vote
0
down vote
As others said you can use shapeless:
import shapeless.ops.tuple.IsComposite
import shapeless.syntax.std.tuple._
class MyClass(i : Int){
def hello() = println(i)
}
object Tup extends App {
def getMyClass[P <: Product](p: P)(implicit ev: IsComposite[P]): MyClass = {
if (p.head.isInstanceOf[MyClass]) p.head.asInstanceOf[MyClass] else throw new Exception()
}
val x= (new MyClass(1),5,6)
val y = (new MyClass(2),7)
val c = getMyClass(x)
val c1 = getMyClass(y)
c.hello()
c1.hello()
val c2 = getMyClass((1,5,6,7)) // exception
c2.hello()
}
add a comment |
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
Scala can't use generic tuple with unknown size because Products don's inherit themeselfs. You can try to use Shapeless or Products from play json lib.
New contributor
Can you clarify what "don't inherit themselves" means here? I don't see what that means or how it would help.
– Travis Brown
57 mins ago
Hi :-) The asker didn't specify unknown size but rather any size, which is weaker. For example, shapeless HLists (as you relevantly suggest) are definitely not of unknown size since their size is known at compile time :-) As Travis Brown, I can't quite figure what you mean in this answer, clarifying would be very helpful :-)
– C4stor
36 mins ago
add a comment |
up vote
2
down vote
Scala can't use generic tuple with unknown size because Products don's inherit themeselfs. You can try to use Shapeless or Products from play json lib.
New contributor
Can you clarify what "don't inherit themselves" means here? I don't see what that means or how it would help.
– Travis Brown
57 mins ago
Hi :-) The asker didn't specify unknown size but rather any size, which is weaker. For example, shapeless HLists (as you relevantly suggest) are definitely not of unknown size since their size is known at compile time :-) As Travis Brown, I can't quite figure what you mean in this answer, clarifying would be very helpful :-)
– C4stor
36 mins ago
add a comment |
up vote
2
down vote
up vote
2
down vote
Scala can't use generic tuple with unknown size because Products don's inherit themeselfs. You can try to use Shapeless or Products from play json lib.
New contributor
Scala can't use generic tuple with unknown size because Products don's inherit themeselfs. You can try to use Shapeless or Products from play json lib.
New contributor
New contributor
answered 1 hour ago
Ivan Aristov
722
722
New contributor
New contributor
Can you clarify what "don't inherit themselves" means here? I don't see what that means or how it would help.
– Travis Brown
57 mins ago
Hi :-) The asker didn't specify unknown size but rather any size, which is weaker. For example, shapeless HLists (as you relevantly suggest) are definitely not of unknown size since their size is known at compile time :-) As Travis Brown, I can't quite figure what you mean in this answer, clarifying would be very helpful :-)
– C4stor
36 mins ago
add a comment |
Can you clarify what "don't inherit themselves" means here? I don't see what that means or how it would help.
– Travis Brown
57 mins ago
Hi :-) The asker didn't specify unknown size but rather any size, which is weaker. For example, shapeless HLists (as you relevantly suggest) are definitely not of unknown size since their size is known at compile time :-) As Travis Brown, I can't quite figure what you mean in this answer, clarifying would be very helpful :-)
– C4stor
36 mins ago
Can you clarify what "don't inherit themselves" means here? I don't see what that means or how it would help.
– Travis Brown
57 mins ago
Can you clarify what "don't inherit themselves" means here? I don't see what that means or how it would help.
– Travis Brown
57 mins ago
Hi :-) The asker didn't specify unknown size but rather any size, which is weaker. For example, shapeless HLists (as you relevantly suggest) are definitely not of unknown size since their size is known at compile time :-) As Travis Brown, I can't quite figure what you mean in this answer, clarifying would be very helpful :-)
– C4stor
36 mins ago
Hi :-) The asker didn't specify unknown size but rather any size, which is weaker. For example, shapeless HLists (as you relevantly suggest) are definitely not of unknown size since their size is known at compile time :-) As Travis Brown, I can't quite figure what you mean in this answer, clarifying would be very helpful :-)
– C4stor
36 mins ago
add a comment |
up vote
2
down vote
If you want to do this without either boilerplate or runtime reflection, Shapeless is your best bet. You can use the IsComposite
type class to put type-level constraints on the first element of a tuple:
import shapeless.ops.tuple.IsComposite
trait MustBeFirst
class MyClass[P <: Product](p: P)(implicit ev: IsComposite[P] { type H = MustBeFirst })
And then:
scala> new MyClass(good2)
res0: MyClass[(MustBeFirst, String)] = MyClass@5297aa76
scala> new MyClass(good3)
res1: MyClass[(MustBeFirst, String, Int)] = MyClass@3f501844
scala> new MyClass(good4)
res2: MyClass[(MustBeFirst, String, Symbol, Int)] = MyClass@24e15478
scala> new MyClass(bad2)
<console>:15: error: could not find implicit value for parameter ev: shapeless.ops.tuple.IsComposite[(String, Int)]{type H = MustBeFirst}
new MyClass(bad2)
^
If you need to use a trait, you can put the ev
(for "evidence") requirement inside the definition instead of in the constructor:
trait MyTrait[P <: Product] {
implicit def ev: IsComposite[P] { type H = MustBeFirst }
}
Now any class instantiating MyTrait
will have to provide evidence that P
is a tuple with MustBeFirst
as its first element.
Probably nitpicking, but I if read shapeless source correctly, IsComposite will also witness that the tuple is of size at least 2. It may be a relevant detail for the asker since tuples of size 1, however awkward, are a thing ^^
– C4stor
39 mins ago
@C4stor Good point—I'll update the answer to clarify asap.
– Travis Brown
6 mins ago
add a comment |
up vote
2
down vote
If you want to do this without either boilerplate or runtime reflection, Shapeless is your best bet. You can use the IsComposite
type class to put type-level constraints on the first element of a tuple:
import shapeless.ops.tuple.IsComposite
trait MustBeFirst
class MyClass[P <: Product](p: P)(implicit ev: IsComposite[P] { type H = MustBeFirst })
And then:
scala> new MyClass(good2)
res0: MyClass[(MustBeFirst, String)] = MyClass@5297aa76
scala> new MyClass(good3)
res1: MyClass[(MustBeFirst, String, Int)] = MyClass@3f501844
scala> new MyClass(good4)
res2: MyClass[(MustBeFirst, String, Symbol, Int)] = MyClass@24e15478
scala> new MyClass(bad2)
<console>:15: error: could not find implicit value for parameter ev: shapeless.ops.tuple.IsComposite[(String, Int)]{type H = MustBeFirst}
new MyClass(bad2)
^
If you need to use a trait, you can put the ev
(for "evidence") requirement inside the definition instead of in the constructor:
trait MyTrait[P <: Product] {
implicit def ev: IsComposite[P] { type H = MustBeFirst }
}
Now any class instantiating MyTrait
will have to provide evidence that P
is a tuple with MustBeFirst
as its first element.
Probably nitpicking, but I if read shapeless source correctly, IsComposite will also witness that the tuple is of size at least 2. It may be a relevant detail for the asker since tuples of size 1, however awkward, are a thing ^^
– C4stor
39 mins ago
@C4stor Good point—I'll update the answer to clarify asap.
– Travis Brown
6 mins ago
add a comment |
up vote
2
down vote
up vote
2
down vote
If you want to do this without either boilerplate or runtime reflection, Shapeless is your best bet. You can use the IsComposite
type class to put type-level constraints on the first element of a tuple:
import shapeless.ops.tuple.IsComposite
trait MustBeFirst
class MyClass[P <: Product](p: P)(implicit ev: IsComposite[P] { type H = MustBeFirst })
And then:
scala> new MyClass(good2)
res0: MyClass[(MustBeFirst, String)] = MyClass@5297aa76
scala> new MyClass(good3)
res1: MyClass[(MustBeFirst, String, Int)] = MyClass@3f501844
scala> new MyClass(good4)
res2: MyClass[(MustBeFirst, String, Symbol, Int)] = MyClass@24e15478
scala> new MyClass(bad2)
<console>:15: error: could not find implicit value for parameter ev: shapeless.ops.tuple.IsComposite[(String, Int)]{type H = MustBeFirst}
new MyClass(bad2)
^
If you need to use a trait, you can put the ev
(for "evidence") requirement inside the definition instead of in the constructor:
trait MyTrait[P <: Product] {
implicit def ev: IsComposite[P] { type H = MustBeFirst }
}
Now any class instantiating MyTrait
will have to provide evidence that P
is a tuple with MustBeFirst
as its first element.
If you want to do this without either boilerplate or runtime reflection, Shapeless is your best bet. You can use the IsComposite
type class to put type-level constraints on the first element of a tuple:
import shapeless.ops.tuple.IsComposite
trait MustBeFirst
class MyClass[P <: Product](p: P)(implicit ev: IsComposite[P] { type H = MustBeFirst })
And then:
scala> new MyClass(good2)
res0: MyClass[(MustBeFirst, String)] = MyClass@5297aa76
scala> new MyClass(good3)
res1: MyClass[(MustBeFirst, String, Int)] = MyClass@3f501844
scala> new MyClass(good4)
res2: MyClass[(MustBeFirst, String, Symbol, Int)] = MyClass@24e15478
scala> new MyClass(bad2)
<console>:15: error: could not find implicit value for parameter ev: shapeless.ops.tuple.IsComposite[(String, Int)]{type H = MustBeFirst}
new MyClass(bad2)
^
If you need to use a trait, you can put the ev
(for "evidence") requirement inside the definition instead of in the constructor:
trait MyTrait[P <: Product] {
implicit def ev: IsComposite[P] { type H = MustBeFirst }
}
Now any class instantiating MyTrait
will have to provide evidence that P
is a tuple with MustBeFirst
as its first element.
answered 1 hour ago
Travis Brown
112k9283528
112k9283528
Probably nitpicking, but I if read shapeless source correctly, IsComposite will also witness that the tuple is of size at least 2. It may be a relevant detail for the asker since tuples of size 1, however awkward, are a thing ^^
– C4stor
39 mins ago
@C4stor Good point—I'll update the answer to clarify asap.
– Travis Brown
6 mins ago
add a comment |
Probably nitpicking, but I if read shapeless source correctly, IsComposite will also witness that the tuple is of size at least 2. It may be a relevant detail for the asker since tuples of size 1, however awkward, are a thing ^^
– C4stor
39 mins ago
@C4stor Good point—I'll update the answer to clarify asap.
– Travis Brown
6 mins ago
Probably nitpicking, but I if read shapeless source correctly, IsComposite will also witness that the tuple is of size at least 2. It may be a relevant detail for the asker since tuples of size 1, however awkward, are a thing ^^
– C4stor
39 mins ago
Probably nitpicking, but I if read shapeless source correctly, IsComposite will also witness that the tuple is of size at least 2. It may be a relevant detail for the asker since tuples of size 1, however awkward, are a thing ^^
– C4stor
39 mins ago
@C4stor Good point—I'll update the answer to clarify asap.
– Travis Brown
6 mins ago
@C4stor Good point—I'll update the answer to clarify asap.
– Travis Brown
6 mins ago
add a comment |
up vote
0
down vote
Because tuples of differing sizes are (almost) unrelated types, if you need the compiler to type-check the calls to getMyClass()
then you might have to overload it.
trait MyTrait {
def getMyClass(x :(MyClass,_)) :MyClass = x._1
def getMyClass(x :(MyClass,_,_)) :MyClass = x._1
def getMyClass(x :(MyClass,_,_,_)) :MyClass = x._1
def getMyClass(x :(MyClass,_,_,_,_)) :MyClass = x._1
}
add a comment |
up vote
0
down vote
Because tuples of differing sizes are (almost) unrelated types, if you need the compiler to type-check the calls to getMyClass()
then you might have to overload it.
trait MyTrait {
def getMyClass(x :(MyClass,_)) :MyClass = x._1
def getMyClass(x :(MyClass,_,_)) :MyClass = x._1
def getMyClass(x :(MyClass,_,_,_)) :MyClass = x._1
def getMyClass(x :(MyClass,_,_,_,_)) :MyClass = x._1
}
add a comment |
up vote
0
down vote
up vote
0
down vote
Because tuples of differing sizes are (almost) unrelated types, if you need the compiler to type-check the calls to getMyClass()
then you might have to overload it.
trait MyTrait {
def getMyClass(x :(MyClass,_)) :MyClass = x._1
def getMyClass(x :(MyClass,_,_)) :MyClass = x._1
def getMyClass(x :(MyClass,_,_,_)) :MyClass = x._1
def getMyClass(x :(MyClass,_,_,_,_)) :MyClass = x._1
}
Because tuples of differing sizes are (almost) unrelated types, if you need the compiler to type-check the calls to getMyClass()
then you might have to overload it.
trait MyTrait {
def getMyClass(x :(MyClass,_)) :MyClass = x._1
def getMyClass(x :(MyClass,_,_)) :MyClass = x._1
def getMyClass(x :(MyClass,_,_,_)) :MyClass = x._1
def getMyClass(x :(MyClass,_,_,_,_)) :MyClass = x._1
}
answered 1 hour ago
jwvh
25k52038
25k52038
add a comment |
add a comment |
up vote
0
down vote
You need to inherit your trait from Product
, through which you can have productIterator
, productArity
and, productElement
to handle the returned value. Here is an example
case class MyClass()
trait MyTrait[T <: Product] {
def getMyClass(x: T): Option[MyClass] =
if(
x.productIterator.hasNext
&&
x.productIterator.next().isInstanceOf[MyClass]
){
Some(x.productIterator.next().asInstanceOf[MyClass])
} else {
None
}
}
case class Test() extends MyTrait[Product]
And you can invoke like this
Test().getMyClass((MyClass(), 1,3,4,5))
//res1: Option[MyClass] = Some(MyClass())
Test().getMyClass((1,3,4,5))
//res2: Option[MyClass] = None
Hope this helps you.
add a comment |
up vote
0
down vote
You need to inherit your trait from Product
, through which you can have productIterator
, productArity
and, productElement
to handle the returned value. Here is an example
case class MyClass()
trait MyTrait[T <: Product] {
def getMyClass(x: T): Option[MyClass] =
if(
x.productIterator.hasNext
&&
x.productIterator.next().isInstanceOf[MyClass]
){
Some(x.productIterator.next().asInstanceOf[MyClass])
} else {
None
}
}
case class Test() extends MyTrait[Product]
And you can invoke like this
Test().getMyClass((MyClass(), 1,3,4,5))
//res1: Option[MyClass] = Some(MyClass())
Test().getMyClass((1,3,4,5))
//res2: Option[MyClass] = None
Hope this helps you.
add a comment |
up vote
0
down vote
up vote
0
down vote
You need to inherit your trait from Product
, through which you can have productIterator
, productArity
and, productElement
to handle the returned value. Here is an example
case class MyClass()
trait MyTrait[T <: Product] {
def getMyClass(x: T): Option[MyClass] =
if(
x.productIterator.hasNext
&&
x.productIterator.next().isInstanceOf[MyClass]
){
Some(x.productIterator.next().asInstanceOf[MyClass])
} else {
None
}
}
case class Test() extends MyTrait[Product]
And you can invoke like this
Test().getMyClass((MyClass(), 1,3,4,5))
//res1: Option[MyClass] = Some(MyClass())
Test().getMyClass((1,3,4,5))
//res2: Option[MyClass] = None
Hope this helps you.
You need to inherit your trait from Product
, through which you can have productIterator
, productArity
and, productElement
to handle the returned value. Here is an example
case class MyClass()
trait MyTrait[T <: Product] {
def getMyClass(x: T): Option[MyClass] =
if(
x.productIterator.hasNext
&&
x.productIterator.next().isInstanceOf[MyClass]
){
Some(x.productIterator.next().asInstanceOf[MyClass])
} else {
None
}
}
case class Test() extends MyTrait[Product]
And you can invoke like this
Test().getMyClass((MyClass(), 1,3,4,5))
//res1: Option[MyClass] = Some(MyClass())
Test().getMyClass((1,3,4,5))
//res2: Option[MyClass] = None
Hope this helps you.
edited 1 hour ago
answered 1 hour ago
Puneeth Reddy V
829715
829715
add a comment |
add a comment |
up vote
0
down vote
It's a little bit unsafe but you can use Structural type in this case:
trait MyTrait {
def getMyClass(x: {def _1: MyClass}): MyClass = x._1
}
1
This is compound type or "structural type" which, unlike "Duck typing", has a clearly-defined meaning is Scala.
– Tim
1 hour ago
@Tim, yes thank you I mean structural type.
– mkUltra
55 mins ago
add a comment |
up vote
0
down vote
It's a little bit unsafe but you can use Structural type in this case:
trait MyTrait {
def getMyClass(x: {def _1: MyClass}): MyClass = x._1
}
1
This is compound type or "structural type" which, unlike "Duck typing", has a clearly-defined meaning is Scala.
– Tim
1 hour ago
@Tim, yes thank you I mean structural type.
– mkUltra
55 mins ago
add a comment |
up vote
0
down vote
up vote
0
down vote
It's a little bit unsafe but you can use Structural type in this case:
trait MyTrait {
def getMyClass(x: {def _1: MyClass}): MyClass = x._1
}
It's a little bit unsafe but you can use Structural type in this case:
trait MyTrait {
def getMyClass(x: {def _1: MyClass}): MyClass = x._1
}
edited 54 mins ago
answered 1 hour ago
mkUltra
533519
533519
1
This is compound type or "structural type" which, unlike "Duck typing", has a clearly-defined meaning is Scala.
– Tim
1 hour ago
@Tim, yes thank you I mean structural type.
– mkUltra
55 mins ago
add a comment |
1
This is compound type or "structural type" which, unlike "Duck typing", has a clearly-defined meaning is Scala.
– Tim
1 hour ago
@Tim, yes thank you I mean structural type.
– mkUltra
55 mins ago
1
1
This is compound type or "structural type" which, unlike "Duck typing", has a clearly-defined meaning is Scala.
– Tim
1 hour ago
This is compound type or "structural type" which, unlike "Duck typing", has a clearly-defined meaning is Scala.
– Tim
1 hour ago
@Tim, yes thank you I mean structural type.
– mkUltra
55 mins ago
@Tim, yes thank you I mean structural type.
– mkUltra
55 mins ago
add a comment |
up vote
0
down vote
As others said you can use shapeless:
import shapeless.ops.tuple.IsComposite
import shapeless.syntax.std.tuple._
class MyClass(i : Int){
def hello() = println(i)
}
object Tup extends App {
def getMyClass[P <: Product](p: P)(implicit ev: IsComposite[P]): MyClass = {
if (p.head.isInstanceOf[MyClass]) p.head.asInstanceOf[MyClass] else throw new Exception()
}
val x= (new MyClass(1),5,6)
val y = (new MyClass(2),7)
val c = getMyClass(x)
val c1 = getMyClass(y)
c.hello()
c1.hello()
val c2 = getMyClass((1,5,6,7)) // exception
c2.hello()
}
add a comment |
up vote
0
down vote
As others said you can use shapeless:
import shapeless.ops.tuple.IsComposite
import shapeless.syntax.std.tuple._
class MyClass(i : Int){
def hello() = println(i)
}
object Tup extends App {
def getMyClass[P <: Product](p: P)(implicit ev: IsComposite[P]): MyClass = {
if (p.head.isInstanceOf[MyClass]) p.head.asInstanceOf[MyClass] else throw new Exception()
}
val x= (new MyClass(1),5,6)
val y = (new MyClass(2),7)
val c = getMyClass(x)
val c1 = getMyClass(y)
c.hello()
c1.hello()
val c2 = getMyClass((1,5,6,7)) // exception
c2.hello()
}
add a comment |
up vote
0
down vote
up vote
0
down vote
As others said you can use shapeless:
import shapeless.ops.tuple.IsComposite
import shapeless.syntax.std.tuple._
class MyClass(i : Int){
def hello() = println(i)
}
object Tup extends App {
def getMyClass[P <: Product](p: P)(implicit ev: IsComposite[P]): MyClass = {
if (p.head.isInstanceOf[MyClass]) p.head.asInstanceOf[MyClass] else throw new Exception()
}
val x= (new MyClass(1),5,6)
val y = (new MyClass(2),7)
val c = getMyClass(x)
val c1 = getMyClass(y)
c.hello()
c1.hello()
val c2 = getMyClass((1,5,6,7)) // exception
c2.hello()
}
As others said you can use shapeless:
import shapeless.ops.tuple.IsComposite
import shapeless.syntax.std.tuple._
class MyClass(i : Int){
def hello() = println(i)
}
object Tup extends App {
def getMyClass[P <: Product](p: P)(implicit ev: IsComposite[P]): MyClass = {
if (p.head.isInstanceOf[MyClass]) p.head.asInstanceOf[MyClass] else throw new Exception()
}
val x= (new MyClass(1),5,6)
val y = (new MyClass(2),7)
val c = getMyClass(x)
val c1 = getMyClass(y)
c.hello()
c1.hello()
val c2 = getMyClass((1,5,6,7)) // exception
c2.hello()
}
answered 32 mins ago
Arnon Rotem-Gal-Oz
18.6k13260
18.6k13260
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53739656%2fscala-generic-tuple%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown