carbon-lang icon indicating copy to clipboard operation
carbon-lang copied to clipboard

CTAD in Carbon?

Open CJ-Johnson opened this issue 8 months ago • 2 comments

Doc: https://docs.google.com/document/d/1QwW0KF1OikLYPtr6EWDCw_zSCcts1kvvwijGRwozv8s/

In case we decide we want to take on CTAD-like behavior in Carbon, I'm creating this as a long-term issue so the doc doesn't get lost and is hopefully eventually picked up as a proposal.

class C(T:! type) {
  fn Make(t: T) -> Self;
  var value: T;
}

// Notice that C is unparameterized in the function name
fn C.Make[T:! type](t: T) -> C(T) {
  // Notice C(T) is deduced from the context
  return .Make(t);
}

fn Run() -> i32 {
  // Notice that C is unparameterized in the variable type specifier
  var c: C = .Make(0);
  return c.value;
}

CJ-Johnson avatar Mar 09 '25 20:03 CJ-Johnson