13

I am writing a ServiceStack webservice in F# and need to limit some of the features (removing SOAP support for instance).

In C# I am using the pipe operation to assign multiple Enums (ServiceStack.ServiceHost.Feature) to the EnableFeatures property like so:

SetConfig(new EndpointHostConfig
{
    DebugMode = true, //Show StackTraces in responses in development
    EnableFeatures = Feature.Json | Feature.Xml | Feature.Html | Feature.Metadata | Feature.Jsv
});

However in F# you can't use pipe to accomplish this, and everything else I try is attempting to do function application to the enums. How do I go about assigning multiple enums in this case?

John B Fair
  • 195
  • 8

3 Answers3

22

Use a triple pipe:

EnableFeatures = Feature.Json ||| Feature.Xml ||| Feature.Html ||| Feature.Metadata ||| Feature.Jsv
Craig Stuntz
  • 125,891
  • 12
  • 252
  • 273
  • Thank you sir! I figured I was missing something simple, and indeed I was. [F# Bitwise Operators](http://msdn.microsoft.com/en-us/library/dd469495.aspx) – John B Fair May 18 '12 at 17:00
13

If you have a bunch of them you can save a few keystrokes with reduce:

List.reduce (|||) [Feature.Json; Feature.Xml; Feature.Html; Feature.Metadata]
Daniel
  • 47,404
  • 11
  • 101
  • 179
0

You would create the value based on the construction of the underlying values:

EnabledFeatures = enum<Feature>(16); // or whatever the full flag value would be for Json + Xml + Html + Metadata, etc
Tejs
  • 40,736
  • 10
  • 68
  • 86
  • 7
    You could, but that's why we have symbolic names for flags and bitwise combining operators in the first place - so we don't have to. – Matthew Walton May 18 '12 at 15:57