# Parametric polymorphism and async

**URL:** <https://forum.dfinity.org/t/parametric-polymorphism-and-async/1192>\
**Category:** Language Support\
**Created:** [September 1, 2020, 3:42am UTC](https://forum.dfinity.org/t/parametric-polymorphism-and-async/1192 "2020-09-01T03:42:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![paulyoung](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/paulyoung/32/5251_2.png) [@paulyoung](https://forum.dfinity.org/u/paulyoung)\
**Post date:** [September 1, 2020, 3:42am UTC](https://forum.dfinity.org/t/parametric-polymorphism-and-async/1192/1 "2020-09-01T03:42:15Z")

</div>

Is it possible to write a function whose type is `<A> (A) -> async A`?

At the moment I get errors like `type error, async has non-shared content type A/52`

This makes me think there’s a way to say that `A` must be constrained to the set of values that are `shared` but I haven’t been able to figure out how to do that.

---

<div class="post-metadata">

**Author:** ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)\
**Post date:** [September 1, 2020, 5:40pm UTC](https://forum.dfinity.org/t/parametric-polymorphism-and-async/1192/2 "2020-09-01T17:40:56Z")

</div>

We don’t support polymorphism for async functions. It has to be a concrete sharable type. There is no notion of type class, so we cannot constrain the type variable.

---

<div class="post-metadata">

**Author:** ![paulyoung](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/paulyoung/32/5251_2.png) [@paulyoung](https://forum.dfinity.org/u/paulyoung)\
**Post date:** [September 4, 2020, 2:18am UTC](https://forum.dfinity.org/t/parametric-polymorphism-and-async/1192/3 "2020-09-04T02:18:32Z")

</div>

Thanks Yan.

I think you’re saying this is a limitation of Motoko and not the IDL (probably because things will need to be specialized by then anyway)

Is that right?

---

<div class="post-metadata">

**Author:** ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)\
**Post date:** [September 4, 2020, 5:02am UTC](https://forum.dfinity.org/t/parametric-polymorphism-and-async/1192/4 "2020-09-04T05:02:55Z")

</div>

I would say it’s the limitation of both. We need to extend both type systems to support this. It’s theoretically possible to transmit polymorphic values over the wire, but not a very high priority.

---

<div class="post-metadata">

**Author:** ![paulyoung](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/paulyoung/32/5251_2.png) [@paulyoung](https://forum.dfinity.org/u/paulyoung)\
**Post date:** [January 12, 2022, 2:01pm UTC](https://forum.dfinity.org/t/parametric-polymorphism-and-async/1192/5 "2022-01-12T14:01:51Z")

</div>

This was mentioned here:

> <https://github.com/dfinity/motoko/blob/a7cd4fba954b2fa7aeadc5680e2dc5d25e38cb69/design/WhitePaper.md#shared-generics-2096>

and previously here:

> <https://github.com/dfinity/motoko/issues/2096>
>
> \### Motivation
> 
> Currently, polymorphism is fully parametric and erased in Moto…ko. Consequently, values of variable type cannot be used in any of the operations that requires intensional polymorphism:
> 
> \- message sends
> \- async (may be unnecessary)
> \- serialisation & deserialisation
> \- structural comparison
> \- structural hashing
> \- polymorphic printing
> 
> It is neither possible to provide such operations as generic library functions, nor can user code abstract over types that involve such operations. Instead, any relevant operation and abstraction can only be made accessible by adding new primitive syntax in the language. That clearly is an abstraction failure.
> 
> \### Proposal
> 
> This proposal extends the language with \_shared type variables\_. On the surface, the proposal is simple:
> 
> \* Allow the annotation \`shared\` on type bindings:
> \`\`\`
> func foo\<shared T\>(x : T) : Bool { x == x };
> \`\`\`
> \* Type variables thus declared are of \_shared kind\_ (a subkind of all types), and qualify as shared types.
> \* They can only be instantiated by shared types.
> 
> \### Implementation
> 
> The gross of this proposal is under the surface. The compiler lowers shared generics by introducing implicit runtime type arguments to respective functions. For example, the above is lowered to
> \`\`\`
> func foo\<T\>(\_T : Rtt\<T\>, x : T) : Bool { prim\_equal\<T\>(\_T, x, x); }
> \`\`\`
> where \`Rtt\<T\>\` is a runtime representation of the type \`T\`. This representation is passed to all intensional operators. The lowering also inserts the respective arguments at call sites. In some cases, that involves constructing witness RTT values dynamically, for example:
> \`\`\`
> func bar\<shared T\>(x : T) : Bool { foo\<\[T\]\>(\[x\]); }
> \`\`\`
> This lowers to
> \`\`\`
> func bar\<T\>(\_T : Rtt\<T\>, x : T) : Bool { foo\<\[T\]\>(\_rtt\_array(\_T), \[x\]); }
> \`\`\`
> where \`\_rtt\_array\` is an internal intro form or function to construct the RTT for an array.
> 
> These techniques are of course well-known from other evidence-passing translations.

---

<div class="post-metadata">

**Author:** ![ggreif](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ggreif/32/301_2.png) [@ggreif](https://forum.dfinity.org/u/ggreif)\
**Post date:** [February 18, 2022, 10:08am UTC](https://forum.dfinity.org/t/parametric-polymorphism-and-async/1192/6 "2022-02-18T10:08:18Z")

</div>

> [@paulyoung](#):
>
> function whose type is `<A> (A) -> async A` ?

You’ll have a hard time to get such a thing working with inter-canister messaging. While messages carry a self-describing type table, there is currently no mechanism to capture it in raw form and interpret it.

For strictly self-sends (i.e. `private`) such a signature would be simpler to implement in the compiler, since it can generate both sides of marshalling simultaneously. But what would be the utility to it?

---

<div class="post-metadata">

**Author:** ![paulyoung](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/paulyoung/32/5251_2.png) [@paulyoung](https://forum.dfinity.org/u/paulyoung)\
**Post date:** [February 19, 2022, 7:01am UTC](https://forum.dfinity.org/t/parametric-polymorphism-and-async/1192/7 "2022-02-19T07:01:38Z")

</div>

@ggreif I think at the time I was attempting to write something akin to `do` notation in user space for monadic values, and this came up.

I think the gist of it was to force unwrap optional values and short circuit by catching them and returning.

I think Motoko has a version of this built in now, but only for optional values. what I was attempting would have potentially worked for `Result` and others.
