Scala: having issue with a forward reference + looping counter

randombits

I am using the Akka scheduler in order to stagger the sending of messages from a List, built from reading in lines of a file. Here's some code:

 def startClock {
          val filename = "conf/my_file.json"
          val lines = scala.io.Source.fromFile(filename).getLines.toList
          var linePtr = 0

          if(linePtr == lines.length) { 
            clock.cancel()
          }
          else {
            Logger.info(s"num: $linePtr")
            default ! Message(lines(linePtr))
            linePtr = linePtr + 1
          }
        }

lazy val clock: Cancellable = Akka.system.scheduler.schedule(Duration.Zero, Duration.create(5000, TimeUnit.MILLISECONDS))(startClock)
val x = clock

Now in order to get around the forward reference compiler error, which looked like the following: play.PlayExceptions$CompilationException: Compilation error[forward reference extends over definition of value clock]

I had to set clock to lazy and then reference it on the next line in order to trigger startClock, and it does that part just fine. The issue I have here is, linePtr is always 0. It is never incrementing yet the the code in the else {} block is confirmed always hitting. I essentially just have infinite recursion here and I'm not familiar enough with Scala to identify what I did wrong here. I've also tried defining the val clock before startClock but then I have a forward reference in the inverse scenario as well. Seems like a chick and egg problem here.

som-snytt

Maybe the lesson is not about how variable capture works, but about using API. Since getLines gives you an iterator, just use it to iterate. Speculatively:

val lines = Source.fromFile(f).getLines
val clock = scheduler schedule send
def send  = if (lines.hasNext) d ! Message(lines.next) else clock.cancel()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Having Issue on Continuous Looping on an Array

From Dev

Ansible throwing error on nested looping - counter issue

From Dev

Funny issue with incorrect forward reference

From Dev

Funny issue with incorrect forward reference

From Dev

Having HASH reference issue with perl

From Dev

Scala's wrong forward reference error

From Dev

Scala forward reference of nested recursive function

From Dev

scala forward reference extends over definition

From Dev

scala forward reference extends over definition

From Dev

Scala forward reference of nested recursive function

From Dev

Wrong Forward Reference yet Scala script runs perfectly

From Dev

Wrong Forward Reference yet Scala script runs perfectly

From Dev

Scala Errors- forward reference extends over definition of value

From Dev

Looping and adding to a counter in R

From Dev

Looping counter - shortest method

From Dev

Looping batchfile with counter

From Dev

Looping array using a counter

From Dev

Having an issue with javafx objects losing reference once they're put into an array

From Dev

Scala case having 22 fields but having issue with play-json in scala 2.11.5

From Dev

Scala case has 22 fields but having issue with play-json in scala 2.11.5

From Dev

Understanding weak reference counter

From Dev

Increment Reference Counter for a CFUNCTYPE

From Dev

Scala error: "forward reference extends over definition of value" when code appears in a function

From Dev

In Scala what is the correct way to fix "error: forward reference extends over definition of value"?

From Dev

Issue with Counter and a Date

From Dev

Java threads counter "issue"?

From Dev

what is the issue in this android counter

From Dev

what is the issue in this android counter

From Dev

Strange Python Counter issue

Related Related

  1. 1

    Having Issue on Continuous Looping on an Array

  2. 2

    Ansible throwing error on nested looping - counter issue

  3. 3

    Funny issue with incorrect forward reference

  4. 4

    Funny issue with incorrect forward reference

  5. 5

    Having HASH reference issue with perl

  6. 6

    Scala's wrong forward reference error

  7. 7

    Scala forward reference of nested recursive function

  8. 8

    scala forward reference extends over definition

  9. 9

    scala forward reference extends over definition

  10. 10

    Scala forward reference of nested recursive function

  11. 11

    Wrong Forward Reference yet Scala script runs perfectly

  12. 12

    Wrong Forward Reference yet Scala script runs perfectly

  13. 13

    Scala Errors- forward reference extends over definition of value

  14. 14

    Looping and adding to a counter in R

  15. 15

    Looping counter - shortest method

  16. 16

    Looping batchfile with counter

  17. 17

    Looping array using a counter

  18. 18

    Having an issue with javafx objects losing reference once they're put into an array

  19. 19

    Scala case having 22 fields but having issue with play-json in scala 2.11.5

  20. 20

    Scala case has 22 fields but having issue with play-json in scala 2.11.5

  21. 21

    Understanding weak reference counter

  22. 22

    Increment Reference Counter for a CFUNCTYPE

  23. 23

    Scala error: "forward reference extends over definition of value" when code appears in a function

  24. 24

    In Scala what is the correct way to fix "error: forward reference extends over definition of value"?

  25. 25

    Issue with Counter and a Date

  26. 26

    Java threads counter "issue"?

  27. 27

    what is the issue in this android counter

  28. 28

    what is the issue in this android counter

  29. 29

    Strange Python Counter issue

HotTag

Archive