- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
is there a cleaner way to write this?
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
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










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.
Re: is there a cleaner way to write this?
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
Re: Re: is there a cleaner way to write this?
Matthew
On 30 December 2011 22:57, Julien Richard-Foy <julien [dot] rf [at] gmail [dot] com> wrote:
--
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