#1859 Default Parameter Values and Functions

andrewc Mon 26 Mar 2012

Hi Fantom team,

How should I reason about this code

using [java]java.lang

class Main
{
  static Int foo()
  {
    return DateTime.boot.sec
  }
  static Str gogo(Int x := foo)
  {
    return x.toStr
  }
  static Void main(Str[] args)
  {
    echo(DateTime.boot.sec.toStr)

    Thread.sleep(2000)
    echo(gogo)
    Thread.sleep(2000)
    echo(gogo)
  }
}

the output I get is something like:

12
12
12

So clearly the foo method is not dynamically evaluated for every evaluation of gogo. When is it evaluated? At classloading? Seems like that could be weird if my value depends something initialized after class loading.

Thanks, -Andrew

KevinKelley Mon 26 Mar 2012

I think you're misinterpreting DateTime.boot.sec...

fansh> DateTime.now.sec
12
fansh> DateTime.boot.sec
31
fansh> DateTime.now.sec
20
fansh> DateTime.boot.sec
31

It seems to be the time-of-boot, not time-since-boot. That is it doesn't vary.

About the default param: I hope it works like the intuition, I actually wrote code just today to use it like that...

My case is like the java overloading pattern,

void print(Object o) { StringBuffer buf=new SB(); print(o, buf); ...  }
void print(Object o, SB buf) { ... }

translating to Fantom:

Void print(Obj o, StrBuf buf := StrBuf()) { ... }

...

StephenViles Mon 26 Mar 2012

Andrew, you probably want sys::Duration.uptime:

Get the duration which has elapsed since the Fantom VM was booted which is now - boot.

A sys::Duration is a period of time, where a sys::DateTime is a fixed instant.

andrewc Mon 26 Mar 2012

Of course! Thanks guys.

qualidafial Mon 26 Mar 2012

I would not have looked in Duration for uptime. Might this be a better fit in Env?

Login or Signup to reply.