#class methods?

8 messages · Page 1 of 1 (latest)

errant current
#

How do i make a class method?
I have an excel sheet with data named "sheet" and i want to return a range sheet.get-range("A2:B4") configure/structure the code
it so it is convient to do this?

since dictionaries are first class i could

sheet = (rawdata: getdatafrombytes("excelfile.xlsx"),
                get-range: 
)

My brain obviously isnt working today. cause i feel like i see this pattern everywhere.

stiff mortar
#

Typst doesn't have classes. It will at some point have user-defined types (read e.g. here) which is sufficient for what you want, but for now this is not directly possible. The get-range would somehow have to know what dictionary it's in to access rawdata (i.e. some kind of this), but it can't.

What you can do is make a module sheet (or whatever name you want) and put #let get-range(s) = ... in there, then call like sheet.get-range(my-sheet)

errant current
#
#let sheet = (rawdata: ("a","b","c"),)

#let pget-range(sheetobj,rng) = {
 return range(rng).map(x=> sheetobj.rawdata.at(x))
}

#let iget-range(range) = {
  pget-range(sheet,range)
}

#sheet.insert("get-range",iget-range)

#sheet.get-range(1)   //Doesn't Work
#(sheet.get-range)(1) //Does Work
#

This actually works. but the last part is weird.

#

more cleaning:

#let newSheet(rawdata) = { 
  let sheet = (rawdata: rawdata, )
  
  let pget-range(sheetobj,rng) = {
   return range(rng).map(x=> sheetobj.rawdata.at(x))
  }

  let iget-range(range) = {
    pget-range(sheet,range)
  }
  //Add method
  sheet.insert("get-range",iget-range)
  
  return sheet
}

#let sheet = newSheet(("a","b","c"))

#(sheet.get-range)(2)
#

wish i could do something about the prenthisis.

stiff mortar
#

yes, but note that changing the sheet by inserting #{sheet.rawdata = ("d", "e", "f")} as the second-to-last line won't change the result, which may be unexpected. That's because the sheet that iget-range() has access to is effectively a snapshot from when it was defined. Try this for debugging:

  let iget-range(range) = {
    repr(sheet)
  }

You'll see it prints (rawdata: ("a","b","c")), without get-range in there.

errant current
#

Yeah i suppos its not quite there yet i can just provide the c style methods.