Section 6 - Conversions

When we’ve accessed the area, group and serial numbers in previous sections, we’ve used .write to return them as Strings. That’s because the actual return type is Repeated.Exactly[N, Digit], which gives it’s values as a list of Ints. That’s a bit difficult to work with, and they’d be more naturally represented as Ints.

Ropes provides a way to do this using ConvertedTo, which takes a rope type and the type you want to convert to and from:

import ropes.core._

type Area   = Repeated.Exactly[3, Digit] ConvertedTo Int Named "Area"
type Group  = Repeated.Exactly[2, Digit] ConvertedTo Int Named "Group"
type Serial = Repeated.Exactly[4, Digit] ConvertedTo Int Named "Serial"

We can now use .value on each section and get a simple Int:

parsed.section["Area"].value
// res0: Int = 78
parsed.section["Group"].value
// res1: Int = 5
parsed.section["Serial"].value
// res2: Int = 1120