killercup / scribbles

Some notes on various topics.
https://deterministic.space
64 stars 11 forks source link

Error in a state machine code example #6

Closed olivren closed 7 years ago

olivren commented 7 years ago

In this nice write-up, the following code example bothers me:

let package = Package::new(); // -> OpenPackage
package.insert([stuff, padding, padding]); // -> OpenPackage
package.seal_up(); // -> ClosedPackage
// package.insert([more_stuff]); // ERROR: No method `insert` on `ClosedPackage`
package.send(address, postage); // -> DeliveryTracking

I don't see how the package type could change from one line to another. I think a realistic example would be:

let package = Package::new(); // -> OpenPackage
let package = package.insert([stuff, padding, padding]); // -> OpenPackage
let package = package.seal_up(); // -> ClosedPackage
// let package = package.insert([more_stuff]); // ERROR: No method `insert` on `ClosedPackage`
let package = package.send(address, postage); // -> DeliveryTracking
killercup commented 7 years ago

Hey, thanks for your feedback, Oliver! You're right, the code is totally confusing.

I think I'll change it to something like

let p: OpenPackage = Package::new()
let p: OpenPackage = package.insert([stuff, padding, padding]);
let p: ClosedPackage = package.seal_up();

and so on.

Am 01.02.2017 um 16:13 schrieb Olivier Renaud notifications@github.com:

In this nice write-up, the following code example bothers me:

let package = Package::new(); // -> OpenPackage

package. insert([stuff, padding, padding]); // -> OpenPackage

package. seal_up(); // -> ClosedPackage // package.insert([more_stuff]); // ERROR: No method insert on ClosedPackage

package. send(address, postage); // -> DeliveryTracking I don't see how the package type could change from one line to another. I think a realistic example would be:

let package = Package::new(); // -> OpenPackage let package = package.insert([stuff, padding, padding]); // -> OpenPackage let package = package.seal_up(); // -> ClosedPackage // let package = package.insert([more_stuff]); // ERROR: No method insert on ClosedPackage let package = package.send(address, postage); // -> DeliveryTracking — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.