TL;DR Polymorphic recursion occurs when you try to do something like
type Something<A> = {
map: <B>( (A) -> B ) -> Something<B>
}
and is not currently supported. However there is a workaround for what I was trying to do Motoko compiler bug # polymorphic recursion - #13 by infu
OOPS! You’ve triggered a compiler bug.
Works:
public class Something<A>() {
public func map<B>() : Something<B> {
Something<B>()
};
};
Works:
public class Something<A>() {
public func map<B>( f : (A) -> B ) : () {
};
};
Combining these two gives error:
public class Something<A>() {
public func map<B>(f: (A) -> B) : Something<B> {
Something<B>()
};
};
If it’s a bug, any ideas on how to go around it before it gets fixed ?
Tried with two different motoko versions
Motoko 0.7.0 (source gkg65w0i-1jxsxgpi-jyzd1iws-ci2znrp1)
Fatal error: exception Stack overflow
Raised by primitive operation at Mo_types__Type.rel_typ in file "mo_types/type.ml", line 741, characters 6-81
Called from Stdlib__list.for_all2 in file "list.ml", line 173, characters 24-31
Called from Mo_types__Type.rel_list in file "mo_types/type.ml", line 669, characters 6-38
Called from Mo_types__Type.rel_typ in file "mo_types/type.ml", line 741, characters 6-81
Called from Mo_types__Type.rel_fields in file "mo_types/type.ml", line 761, characters 6-36
Called from Stdlib__list.for_all2 in file "list.ml", line 173, characters 24-31
Called from Mo_types__Type.rel_list in file "mo_types/type.ml", line 669, characters 6-38
Called from Mo_types__Type.rel_fields in file "mo_types/type.ml", line 761, characters 6-36
Called from Stdlib__list.for_all2 in file "list.ml", line 173, characters 24-31
Called from Mo_types__Type.rel_list in file "mo_types/type.ml", line 669, characters 6-38
Called from Mo_types__Type.rel_fields in file "mo_types/type.ml", line 761, characters 6-36
Called from Stdlib__list.for_all2 in file "list.ml", line 173, characters 24-31
Called from Mo_types__Type.rel_list in file "mo_types/type.ml", line 669, characters 6-38
Called from Mo_types__Type.rel_fields in file "mo_types/type.ml", line 761, characters 6-36
Called from Stdlib__list.for_all2 in file "list.ml", line 173, characters 24-31
...
Motoko 0.6.20 (source 640crzw6-q5fr2cc2-j0arj8l2-rfv2v0jh)
Fatal error: exception Stack overflow
Raised by primitive operation at Mo_types__Type.rel_typ in file "mo_types/type.ml", line 725, characters 6-81
Called from Stdlib__list.for_all2 in file "list.ml", line 173, characters 24-31
Called from Mo_types__Type.rel_list in file "mo_types/type.ml", line 653, characters 6-38
Called from Mo_types__Type.rel_typ in file "mo_types/type.ml", line 725, characters 6-81
Called from Mo_types__Type.rel_fields in file "mo_types/type.ml", line 745, characters 6-36
...