this is rust-based question. In Substrate code, there are lots of type casting. For example, for the BalanceOf type alias,

<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance

How is it possible to cast generic type(T) to some trait(Config), which has no exact size on compile time?

Thank you

  • Monomorphisation. If you have 3 different impl of the trait then when you pass it to a function, three concrete functions taking those types will be created. (The alternative would be dyn objects where a virtual lookup table is used and there's only one copy of the final function.)
    – Squirrel
    Apr 28, 2022 at 11:59
  • You will see this pattern often to make the code a bit more readable: type BalanceOf<T> = <<T as Config>::EVM as EVM<AccountIdOf<T>>>::Balance;
    – Squirrel
    Apr 28, 2022 at 12:02
  • so... does T as config, type-casting..? or just simply bouding the type
    – coco_yoon
    Apr 28, 2022 at 12:53

1 Answer 1


<T as Config> is just a generic bound.

type BalanceOf<T> = <<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance

Is just a type alias, because typing out the right side is just annoying, when compared to BalanceOf<T>.

This kind of type alias appears when we are trying to extract a concrete associated type from some trait. In this case the type Balance is an associated type of trait Currency, and we want to easily access this type.

  • Thank you for the details, Shawn!
    – coco_yoon
    May 1, 2022 at 16:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.