#2171 Units - How?

SlimerDude Sun 14 Jul 2013

I've not used units much so I'm a bit confused as to how they work...

Why does time := 69sec compile but size := 69kB does not?

Looking in FAN_HOME/etc/sys/units.txt there's a lot of definitions - but how do you reference them?

brian Sun 14 Jul 2013

Literals like 69sec map to a Duration type which is implicitly based on time. The unit database is accessed with sys::Unit class.

Our commercial software SkySpark has a scripting language called Axon which actually does allow units with any number literal (based on Fantom unit database). It definitely has its pros and cons.

ttmrichter Sun 14 Jul 2013

You'll want to check out the docs for sys::Unit.

Short version: 69sec is a duration literal and is part of the language grammar ( http://fantom.org/doc/docLang/Literals.html#duration ). 69kB is nothing at all unless you use the sys::Unit classes and methods.

Here would be some sample code:

MB := Unit.fromStr("MB")

B := Unit.fromStr("byte")

MB.convertTo(3.15f, B)

SlimerDude Tue 16 Jul 2013

Cool,

Thanks for the example. For some reason I was under the impression that all units in units.txt became Fantom literals - I think I confused it with Duration.

So to reiterate (for future ref):

  • there are no unit literals, just the sys::Unit class.
  • there are sys::Duration literals of ns, ms, sec, min, hr & day

Oh, and the sys::Unit.convertTo doc says:

m  := Unit.find("meter")
km := Unit.find("kilometer")

it should be:

m  := Unit.fromStr("meter")
km := Unit.fromStr("kilometer")

brian Thu 18 Jul 2013

thanks, I fixed docs

Login or Signup to reply.