- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Recursive Type declaration
Mon, 2009-07-13, 09:47
Hello all,
am I right, that recursive type declaration is
not possible in Scala 2.7 ?
Will it be in 2.8 ?
E.g.:
type S = PartialFunction[A, S]
How would one currently solve the problem
of a set of functions of a given type with
each returning a function of same type?
Concrete example:
var current = funcA
def handle(x) { current = current(x) }
val funcA = {
case ... => funcA
case ... => funcB
}
val funcB = {
case ... => funcA
case ... => funcC
}
val funcC = // you will know the principle
KR
Det
Mon, 2009-07-13, 19:17
#2
Re: Recursive Type declaration
On Mon, Jul 13, 2009 at 4:05 PM, Daniel Sobral wrote:
> abstract class S[A] extends PartialFunction[A, S[A]]
That helps, but the tricky part of the original problem is dealing
with the recursive _values_ (which, because they're recursive, have to
be given explicit types).
The best I can come up with involves an additional wrapper for each
partial function,
trait RPF[T] extends PartialFunction[T, RPF[T]]
def rpf[T](pf : PartialFunction[T, RPF[T]]) = new RPF[T] {
def apply(t : T) = pf(t)
def isDefinedAt(t : T) = pf.isDefinedAt(t)
}
val funcA : RPF[Int] = rpf { // Type annotation is essential
case 1 => funcB
case 2 => funcC
case 3 => funcA
}
val funcB : RPF[Int] = rpf {
case 1 => funcC
case 2 => funcB
case 3 => funcA
}
val funcC : RPF[Int] = rpf {
case 1 => funcA
case 2 => funcB
case 3 => funcC
}
funcA(1)(2)(3)(1)(3)(1)
I'd be interested to know if there's anything more direct.
Cheers,
Miles
Tue, 2009-07-14, 14:57
#3
RE: Recursive Type declaration
Miles,
> The best I can come up with involves an additional wrapper
> for each partial function,
[...]
Thank you, that comes very close to my orignal idea,
and some smart renaming masks the wrapping process,
so no real need for a more direct way.
I played a bit with it and got a nice FSM script.
Next will be to hide the common things into a trait
and make the specific state definition and handling
a class implementation.
===================================================================
//---- the common things
trait StateFunc[T] extends PartialFunction[T, StateFunc[T]]
abstract class Event
type State = StateFunc[Event]
def transitions[T](pf: PartialFunction[T, StateFunc[T]]) = new
StateFunc[T] {
def apply(evt: T) = pf(evt)
def isDefinedAt(evt: T) = pf.isDefinedAt(evt)
}
Tue, 2009-07-14, 15:07
#4
Re: Recursive Type declaration
I like this! :-)
On Tue, Jul 14, 2009 at 10:38 AM, Detering Dirk <Dirk [dot] Detering [at] bitmarck [dot] de> wrote:
--
Daniel C. Sobral
Something I learned in academia: there are three kinds of academic reviews: review by name, review by reference and review by value.
On Tue, Jul 14, 2009 at 10:38 AM, Detering Dirk <Dirk [dot] Detering [at] bitmarck [dot] de> wrote:
Miles,
> The best I can come up with involves an additional wrapper
> for each partial function,
[...]
Thank you, that comes very close to my orignal idea,
and some smart renaming masks the wrapping process,
so no real need for a more direct way.
I played a bit with it and got a nice FSM script.
Next will be to hide the common things into a trait
and make the specific state definition and handling
a class implementation.
===================================================================
//---- the common things
trait StateFunc[T] extends PartialFunction[T, StateFunc[T]]
abstract class Event
type State = StateFunc[Event]
def transitions[T](pf: PartialFunction[T, StateFunc[T]]) = new
StateFunc[T] {
def apply(evt: T) = pf(evt)
def isDefinedAt(evt: T) = pf.isDefinedAt(evt)
}
//--- the specific controller
case class StartEvent() extends Event
case class MiddleEvent(amt:Int) extends Event
case class EndEvent() extends Event
val stateA:State = transitions {
case e:StartEvent => println("Received in A: "+e+" going to
B"); stateB
case e:MiddleEvent => println("Received in A: "+e+" going to
C"); stateC
case e:EndEvent => println("Received in A: "+e+" going to
A"); stateA
}
val stateB:State = transitions {
case e:StartEvent => println("Received in B: "+e+" going to
C"); stateC
case e @ MiddleEvent(a) if (a > 5) => println("Received in B:
"+e+" going to B"); stateB
case e @ MiddleEvent(a) if (a <= 5) => println("Received in B:
"+e+" going to A"); stateA
case e:EndEvent => println("Received in B: "+e+" going to
A"); stateA
}
val stateC:State = transitions {
case e:StartEvent => println("Received in C: "+e+" going to
A"); stateA
case e:MiddleEvent => println("Received in C: "+e+" going to
B"); stateB
case e:EndEvent => println("Received in C: "+e+" going to
C"); stateC
}
//--- run it (order of events doesn't make sense)
stateA(StartEvent()) (MiddleEvent(7)) (MiddleEvent(3)) (EndEvent())
(MiddleEvent(0)) (EndEvent()) (StartEvent())
===================================================================
--
Daniel C. Sobral
Something I learned in academia: there are three kinds of academic reviews: review by name, review by reference and review by value.
On Mon, Jul 13, 2009 at 5:47 AM, Detering Dirk <Dirk [dot] Detering [at] bitmarck [dot] de> wrote:
--
Daniel C. Sobral
Something I learned in academia: there are three kinds of academic reviews: review by name, review by reference and review by value.