Fenius now has syntax for functional record updates! Records now have a with(field=value, …) method, which allows creating a new record from an existing one with only a few fields changed. For example, if you have a record:
fenius> record Person(name, age) <class `Person`> fenius> let p = Person("Hildur", 22) Person("Hildur", 22)
You can now write:
fenius> p.with(age=23)
Person("Hildur", 23)
to obtain a record just like p but with a different value for the age field. The update is functional in the sense that the p is not mutated; a new record is created instead. This is similar to the with() method in dictionaries.
Another new trick is that now records can have custom printers. Printing is now performed by calling the repr_to_port(port) method, which can be overridden by any class. Fenius doesn't yet have much of an I/O facility, but we can cheat a bit by importing the functions from the host Scheme implementation:
fenius> record Point(x, y) <class `Point`> fenius> import chezscheme # Define a new printing function for points. fenius> method Point.repr_to_port(port) = { chezscheme.fprintf(port, "<~a, ~a>", this.x, this.y) } # Now points print like this: fenius> Point(1, 2) <1, 2>
A native I/O API is coming Real Soon Now™.
Copyright © 2010-2023 Vítor De Araújo
O conteúdo deste blog, a menos que de outra forma especificado, pode ser utilizado segundo os termos da licença Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International.
Powered by Blognir.