HELP! dependent type in Scala to decide return type of function!

5 views
Skip to first unread message

Rutvik Patel

unread,
Apr 3, 2018, 4:19:46 AM4/3/18
to Shapeless Dev
I am working on implementation of simple imperative language. I am trying to follow pure functional code only in order to apply in practice what I have learned recently. 

here is my ADT

sealed trait Syntax
case class Num(value: Long) extends Syntax
case class Add[A, B](left: A, right: B) extends Syntax

here is my type class

trait Construct[A] {
  type S <: Syntax
  def reduce(a: A): S
}

here is my type class instance for Add

implicit def addConstruct[A, B](implicit lc: Construct[A], rc: Construct[B]) = new Construct[Add[A, B]] {
    override type S <: ???
    override def reduce(a: Add[A, B]): S = a match {
      case Something1 => Add(lc.reduce(left), right)                // returning Add
      case Something2 => Add(left, rc.reduce(right))               // returning Add
      case Something3 => Num(x + y)                                     // returning Num
   }
}


what can I do to make my reduce function to return Num OR Add?
Is there anyway that I can manage it using Shapeless or some other workaround?
I need help and just pointing out on right direction would be enough!

Thanks!

Miles Sabin

unread,
Apr 3, 2018, 4:56:07 AM4/3/18
to shapel...@googlegroups.com
Hi Rutvik,

This mailing list is deprecated in favour of shapeless's gitter
channel: I recommend you take your question there ... there will be a
lot of in the channel who will be able to help you with it,

https://gitter.im/milessabin/shapeless

Yours,


Miles
> --
> You received this message because you are subscribed to the Google Groups
> "Shapeless Dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to shapeless-de...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Miles Sabin
tel: +44 7813 944 528
skype: milessabin
gtalk: mi...@milessabin.com
http://milessabin.com/blog
http://twitter.com/milessabin

Rutvik Patel

unread,
Apr 3, 2018, 8:21:20 AM4/3/18
to shapel...@googlegroups.com

Thank you Miles! 

Rutvik.


> For more options, visit https://groups.google.com/d/optout.



--
You received this message because you are subscribed to the Google Groups "Shapeless Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shapeless-dev+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages