How Does TArray::ContainsByPredicate Work?

To add some info to an already perfect explanation :slight_smile:
A Lambda is more or less the c++ way to write a function inside a function.

Instead of doing this:

bool UMyClass::DoThis() {

  // DoStuff

  return DoThat();
}

you can do

bool UMyClass::DoThis() {
  auto DoThat = []() -> bool {
    return true;
  };

  return DoThat();
}

Sometimes you need the "-> " this is called a trailing type.

You can also call a lambda recursively by passing a lambda into itself:

auto HidePropertiesRecursive = [&DetailBuilder](auto&& HidePropertiesRecursive, const FString& InPropertyName, const UClass* InContainerClass) {
	
	
	// DoStuff
	
	HidePropertiesRecursive(HidePropertiesRecursive, PropPath, InContainerClass);
};

About the parameter naming of lambdas… I’m a bit bothered by “InHIt” as well but it really depends on what style you are using through the entire source code, you should keep that consistent. I reserve “In, Ref, Out” for method parameters but in loops and lambdas I like to remove that prefix and Suffix with an X to remind myself I am doing something in a loop.

for (AActor* ActorX : AllActors) {
  
}
2 Likes