This page is no longer maintained — Please continue to the home page at www.scala-lang.org

is there a cleaner way to write this?

3 replies
Matthew Pocock 3
Joined: 2010-07-30,
User offline. Last seen 42 years 45 weeks ago.
Season's greetings one and all!
I've got this implicit that forms part of a DSL.
  implicit def fromSrc[S](s: S): {def <-< [L] (l: L): { def >-> [D] (d: D): PathExpr[S, L, D]}} = new {     def <-< [L] (l: L) = new {      def >-> [D] (d: D) = new PathExpr(s, l, d)    }  }
The structural type annotation for the return type is required, or else scalac complains that it can't use fromSrc as an implicit conversion. However, it means I'm writing the same thing out twice which is sad. Is there some other syntax for this that is readable and less repetitive?
Thanks,
Matthew
--
Dr Matthew PocockIntegrative Bioinformatics Group, School of Computing Science, Newcastle Universitymailto: turingatemyhamster [at] gmail [dot] com gchat: turingatemyhamster [at] gmail [dot] commsn: matthew_pocock [at] yahoo [dot] co [dot] uk irc.freenode.net: drdozerskype: matthew.pococktel: (0191) 2566550mob: +447535664143
Julien Richard-Foy
Joined: 2011-04-24,
User offline. Last seen 42 years 45 weeks ago.
Re: is there a cleaner way to write this?
Could you give a full example where the implicit conversion is inapplicable?
scala> object O {     |   implicit def from[S](s: S) = new {     |     def <-<[L](l: L) = new {     |       def >->[D](d: D) = 42     |     }     |   }     | }defined module O
scala> import O._import O._
scala> "A" <-< "B" >-> "C"
res0: Int = 42
Matthew Pocock 3
Joined: 2010-07-30,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: is there a cleaner way to write this?
No, it's meant to be applicable just about everywhere, like the -> implicit for building tuples.
Matthew

On 30 December 2011 22:57, Julien Richard-Foy <julien [dot] rf [at] gmail [dot] com> wrote:
Could you give a full example where the implicit conversion is inapplicable?
scala> object O {      |   implicit def from[S](s: S) = new {     |     def <-<[L](l: L) = new {     |       def >->[D](d: D) = 42     |     }     |   }      | }defined module O
scala> import O._import O._
scala> "A" <-< "B" >-> "C"
res0: Int = 42



--
Dr Matthew PocockIntegrative Bioinformatics Group, School of Computing Science, Newcastle Universitymailto: turingatemyhamster [at] gmail [dot] com gchat: turingatemyhamster [at] gmail [dot] commsn: matthew_pocock [at] yahoo [dot] co [dot] uk irc.freenode.net: drdozerskype: matthew.pococktel: (0191) 2566550mob: +447535664143
geoff
Joined: 2008-08-20,
User offline. Last seen 1 year 25 weeks ago.
Re: is there a cleaner way to write this?

On Fri, Dec 30, 2011 at 02:59:24PM +0000, Matthew Pocock said
> The structural type annotation for the return type is required, or else
> scalac complains that it can't use fromSrc as an implicit conversion.
> However, it means I'm writing the same thing out twice which is sad. Is
> there some other syntax for this that is readable and less repetitive?

class PathExpr[S,L,D](s: S, l: L, d: D)

object PathExpr {
class A[S](s: S) {
def <-<[L](l: L): B[S,L] = new B[S,L](s,l)
}

class B[S,L](s: S, l: L) {
def >->[D](d: D) = new PathExpr(s, l, d)
}

implicit def fromSrc[S](s: S): A[S] = new A(s)
}

object Test {
import PathExpr._
val x = 1 <-< "a" >-> None
}

is working just fine for me. You should usually avoid using structural
types anyway.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland