mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-21 19:46:48 +00:00
Basic support for named constraints as described in [the generics design](https://github.com/carbon-language/carbon-lang/blob/trunk/docs/design/generics/details.md#named-constraints). Support is provided for `extends` and `impl` declarations in `constraint`, but not yet for member aliases. The missing prelude named constraints `Add`, `Mul`, `Ordered`, etc. are also added.
54 lines
1.1 KiB
Plaintext
54 lines
1.1 KiB
Plaintext
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
// AUTOUPDATE
|
|
// RUN: %{explorer-run}
|
|
// RUN: %{explorer-run-trace}
|
|
// CHECK:STDOUT: 5
|
|
// CHECK:STDOUT: 5
|
|
// CHECK:STDOUT: 5
|
|
// CHECK:STDOUT: 5
|
|
// CHECK:STDOUT: 5
|
|
// CHECK:STDOUT: result: 0
|
|
|
|
package ExplorerTest api;
|
|
|
|
interface Maker(T:! Type) {
|
|
fn Make[me: Self]() -> T;
|
|
}
|
|
|
|
constraint IndirectMaker(T:! Type) {
|
|
extends Maker(T*);
|
|
}
|
|
|
|
constraint MoreIndirectMaker {
|
|
extends IndirectMaker(i32);
|
|
}
|
|
|
|
class PointerFactory {
|
|
var p: i32*;
|
|
external impl as Maker(i32*) {
|
|
fn Make[me: Self]() -> i32* { return me.p; }
|
|
}
|
|
}
|
|
|
|
fn CallIndirect[T:! IndirectMaker(i32)](x: T) -> i32 {
|
|
return *x.Make();
|
|
}
|
|
|
|
fn CallMoreIndirect[T:! MoreIndirectMaker](x: T) -> i32 {
|
|
return *x.Make();
|
|
}
|
|
|
|
fn Main() -> i32 {
|
|
var n: i32 = 5;
|
|
var f: PointerFactory = {.p = &n};
|
|
Print("{0}", *f.(Maker(i32*).Make)());
|
|
Print("{0}", *f.(IndirectMaker(i32).Make)());
|
|
Print("{0}", *f.(MoreIndirectMaker.Make)());
|
|
Print("{0}", CallIndirect(f));
|
|
Print("{0}", CallMoreIndirect(f));
|
|
return 0;
|
|
}
|