dfinity / motoko

Simple high-level language for writing Internet Computer canisters
Apache License 2.0
517 stars 97 forks source link

Bugs. Typing #2057

Closed crusso closed 3 years ago

crusso commented 4 years ago

93 @Claudio Russo ran into two problems with recent edu work:

I’ve been getting many of these issues: type error, expression of type [Word8] cannot produce expected type [Nat8] Where both are defined as [Word8]

2) Not sure if this is a bug, but I’m not sure why you’re able to used await in functions that aren’t marked async

crusso commented 4 years ago

Reported on slack by Nicolas Zoghb

https://dfinity.slack.com/archives/C016X3LCSLB/p1603147616015100

Awaiting more detailed repro

kritzcreek commented 3 years ago

Reading the Slack thread this seems related to Candid's handling of Word types, and not really typing? Is this covered by #1824 @crusso?

crusso commented 3 years ago

Yes, I think we can close this issue - its not a bug, just a misunderstanding about how candid works.

Copying my answers from Slack:

For 1) what I suspect might be happening is that you have a canister, A, using Word8 in its Motoko interface, and are using it from another Motoko canister, B, via import C "canister:A". The Word8 will be exported as Candid nat8 (Candid doesn't have a word8 type but uses nat8 for Word8 and well as Nat8) and then imported in the second canister as a Motoko Nat8 leading to the confusion. Basically, the mapping from Motoko types to Candid types is many-to-one, but mapping from Candid to Motoko types is one-to-one, so composing the two can lead to surprises.

1) Will go away if do #1824 (removing word types).

For 2):

actor A {
   public func f() : () {
     await async {};
   };
}

If 2) was an instance of the above that's actually ok. Motoko supports oneway methods that can't be awaited, but can send messages and await their results. This is an instance of that and is shorthand for:

actor A {
   public shared func f() : () {
     await async {};
   };
}