apply.carbon 539 B

1234567891011121314151617181920212223242526
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. //
  5. // AUTOUPDATE
  6. // RUN: %{explorer-run}
  7. // RUN: %{explorer-run-trace}
  8. // CHECK:STDOUT: result: -2
  9. package ExplorerTest api;
  10. fn apply[T:! Type, U:! Type](f: __Fn (T) -> U, x: T) -> U {
  11. return f(x);
  12. }
  13. fn positive(x: bool) -> i32 {
  14. if (x) {
  15. return 2;
  16. } else {
  17. return -2;
  18. }
  19. }
  20. fn Main() -> i32 {
  21. return apply(positive, false);
  22. }