Angle Bracket Inside Function Call

Hello. I need understanding one line in this code. Specifically, I don't understand the 2nd to last line where the function call contain <order::Post> . I try to google this but it seems to be related to Universal Function Call Syntax but not sure.

As far as I know, the client type has a method call issue but why does it use <order::Post> as part of the method call. Please advise.

#[tokio::main]
async fn main() {

  let api_info = ApiInfo::from_env().unwrap();
  let client = Client::new(api_info);

  // Create request for a limit order for AAPL with a limit price of USD 100.
  let request = order::OrderReqInit {
    type_: order::Type::Limit,
    limit_price: Some(Num::from(100)),
    ..Default::default()
  }
  // We want to go long on AAPL, buying a single share.
  .init("AAPL", order::Side::Buy, order::Amount::quantity(1));

  let order = client.issue::<order::Post>(&request).await.unwrap();
  println!("Created order {}", order.id.to_hyphenated_ref());
}

Looking at the signature of issue

impl Client {
    pub async fn issue<E>(
        &self,
        input: E::Input
    ) -> Result<E::Output, RequestError<E::Error>>
    where
        E: Endpoint, 
    { … }
}

you’ll see that it’s a generic method with a generic type argument E. Such a method can be called like client.issue(&request).await, in which case the compiler tries to infer the generic argument E. But this type inference can fail, which is the case here. To help out the compiler, you can manually specify which type E is supposed to be, that’s what the syntax client.issue::<order::Post>(&request).await is about. It’s saying that E is order::Post.

2 Likes

It’s actually really not related to universal function call syntax.

By the way, the :: -part before the <…> may seem a bit weird; it’s because of syntactic ambiguity with < and > meaning less-than and greater-than. This ::<> syntax is also colloquially known by the name “turbofish”.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.