Skip to content

Conversation

@crusso
Copy link
Contributor

@crusso crusso commented Sep 26, 2025

  • Add support for implicit argument declarations (feat: add support for implicit arguments #5517).

    Function and class parameters can be declared implicit using a specially named type of the form:

    • implicit : <typ> (implicit named or anonymous parameter).
    • implicit : (<id> : <typ>) (implicit parameter matching name <id> and <typ>).
    • implicit : (_ : <typ>) (implicit parameter matching <typ> under any name).

    This is just a special case of a named type in Motoko with special name "implicit".

    The arguments corresponding to implicit parameters can be omitted at call sites, provided the compiler can unambiguously resolve using a declaration of the given name and matching type, in either the current scope or the fields of a module in the current scope. By default, the name is determined by the parameter declared implicit but can be specified to be another name,
    <id>, using the second form of implicit type annotation above (see Pair.toText below for an example).
    An unnamed parameter or a named parameter declared with the third form of anonymous declaration matches any name in the context that has a matching type only.

    Any candidate from the current scope is preferred over candidates from nested scopes.

    In the rare case that a function has more than one implicit parameter, either all arguments must be specified or all implicit arguments omitted.

    Functions that require two implicits of the same (external) name can declare them with different argument names (to distinguish their implementations), and common (external) names, using a nested name.

    Example:

    module Nat {
      public func toText(n : Nat) : Text { debug_show n };
    };
    module Array {
      public func toText<T>(as : [T], toText: (implicit : T -> Text)) : Text {
         var t = "";
         for (a in as.vals()) {
           t := t # (toText(a));
         };
         t
      }
    };
    module Pair {
      public func toText<T, U>(
        p : (T, U),
        toTextT : (implicit : (toText : T -> Text)),
        toTextU : (implicit : (toText : U -> Text)))
        : Text {
          "(" # toTextT(p.0) # "," # toTextU(p.1) # ")"
        };
    };
    Array.toText([1,2,3], Nat.toText) // explicit arguments
      |> debugPrint _;
    Array.toText([1,2,3]) // implicit arguments
      |> debugPrint _;
    Pair.toText((1,2))
      |> debugPrint _; // implicit arguments

@github-actions
Copy link
Contributor

github-actions bot commented Sep 27, 2025

Comparing from 7c0fd36 to b3d3958:
In terms of gas, no changes are observed in 5 tests.
In terms of size, no changes are observed in 5 tests.

@crusso
Copy link
Contributor Author

crusso commented Sep 28, 2025

Latest commits lock good @christoph-dfinity!

@crusso crusso marked this pull request as ready for review September 29, 2025 09:31
@crusso crusso requested a review from a team as a code owner September 29, 2025 09:31
crusso and others added 5 commits September 29, 2025 10:46
…#5526)

* track whether the argument was parenthesized on a call expression

* don't flatten parenthesized tuple arguments for implicit search

* add repro

* don't insert holes twice

---------

Co-authored-by: Claudio Russo <[email protected]>
@crusso crusso changed the title experiment: implicits feat: add support for implicit arguments Sep 29, 2025
@crusso crusso requested a review from Kamirus September 30, 2025 16:42

let t : Nat = 0;

// let _ = map.get(t);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is no bug anymore, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think not. Let me rewrite the test.

crusso and others added 4 commits October 1, 2025 11:22
* implicit-bug to implicit-map test

* diamond resolution fail test + covariant and contravariant implicit reoslution

* cleanup
Copy link
Member

@Kamirus Kamirus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@crusso crusso added the automerge-squash When ready, merge (using squash) label Oct 1, 2025
@crusso crusso merged commit 64889ef into master Oct 1, 2025
21 of 22 checks passed
@crusso crusso deleted the claudio/implicits branch October 1, 2025 12:13
@mergify mergify bot removed the automerge-squash When ready, merge (using squash) label Oct 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants