-
Notifications
You must be signed in to change notification settings - Fork 105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Stack overflow in type checking #3135
Comments
Thanks for the report, this looks like a duplicate of #3057. |
I am amazed how the functor/monad mindset already penetrated the IC space! |
Closing as a duplicate. |
Let me add some context. We have some code in But I am a bit puzzled about what you are tying to accomplish here. It seems to me that you want a generic interface for chainable things, but OTOH you hard-code So I can only suggest to use the top-level |
Yeah, it took me too many lines to deal with error checking. I really love the Haskell way. Everybody love the Haskell way. |
The type switch (
chain<A, Text>(getA) // where getA may return an #err("Some error getting A")
.then<B>(getBFromA) // where getBFromA may return an #err("Something wrong getting B")
.then<C>(getCFromB) // there may be errors as well
.value
) {
case (#ok(c)) { /* do something with c */ };
case (#err(msg)) { /* report the error */ };
} HKT is a nice feature, however, I fully understand it's not the priority for now. I can use the |
BTW, here's the code snippet I had trouble with (too many indentations) public shared({ caller }) func buy(list: [(Nat, Nat, Nat)], tokens: Nat): async R<Inventory> {
switch (userMap.getValue<Inventory>(inventories, caller)) {
case (null) {#err(ERR_USER_NOT_FOUND) };
case (?inventory) {
if (inventory.tokens < tokens) {
#err(ERR_INSUFFICIENT_TOKENS)
} else {
Result.chain<Nat, Inventory, Text>(
priceForList(list),
func (realtimePrice) {
if (realtimePrice > tokens) {
#err(ERR_PRICE_CHANGED)
} else {
let (tokens, _) = LN.safeSub(inventory.tokens, realtimePrice);
Result.chain<Map<Nat, (Nat, Nat)>, Inventory, Text>(
foldLeft<(Nat, Nat, Nat), R<Map<Nat, (Nat, Nat)>>>(
list,
#ok(inventory.crops),
incCrops),
func (crops) {
let newInventory = { tokens; crops };
inventories := userMap.putKeyValue<Inventory>(inventories, caller, newInventory);
#ok(newInventory)
}
)
}
}
)
}
};
}
}; |
You might be able to avoid some of those type parameters if you, instead, annotate the argument and result types of the lambdas Motoko type inference is certainly not as powerful as ML or Haskell, unfortunately. We've contemplated adding a form of general, monadic |
Motoko Version
Motoko 0.6.21 (source wahvzdqw-grg3wqkh-0n8s7fj6-f4rxx0rh)
Repro Steps
chain.mo
moc -r --package base $(dfx cache show)/base chain.mo
Expects
No crash, print out
#ok(123)
Actual
moc
crashes, refer to crash_log.txt for detailProblematic Code
The text was updated successfully, but these errors were encountered: