Snippets tagged discriminated union

  • Re-creating arithmetic with DU

    You never know when you might need this.

    7 people like this

    Posted: 13 years ago by Dmitri Pavlenkov

  • DSL for financial contracts

    Simple domain-specific language (DSL) for describing financial contracts in F#. A contract is represented using a discriminated union. Evaluating a contract gives the orders that may happen at a given date.

    8 people like this

    Posted: 13 years ago by Tomas Petricek

  • Finalizing Tesco purchase

    The sample shows two different reprezentations of Tesco checkout. The first one stores scanned items - as a list of either purchase or cancel items - and the second stores final bill with product and total quantity. The snippet implements transformation that corresponds to finalizing the purchase.

    3 people like this

    Posted: 12 years ago by Tomas Petricek

  • Refactoring discriminated unions

    A simple example that shows how to refactor discriminated unions to extract common members

    3 people like this

    Posted: 11 years ago by Tomas Petricek

  • C#-Friendly Single-Case Discriminated Unions

    Single-case Discriminated Unions (https://fsharpforfunandprofit.com/posts/designing-with-types-single-case-dus/) are a lightweight way to create types for important concepts in your domain. Unfortunately, there's a few gotchas when consuming from C# to be aware of. For one, if you use the `==` or `!=` operators in C#, it will do a reference comparison, which is almost certainly not what you want for these wrapper types. By overriding `op_Equality` and `op_Inequality`, this will force it to use a structural comparison. Secondly, when using string concatenation via `+`, `String.Format`, or string interpolation, C# will implicitly convert non-string arguments to a string via `ToString`. This means if you forget to use `.Item` to unwrap your value, you will not get a compiler error, it will just implicitly call `ToString`, which by default will look like this: `Id "c148b684-2c40-4383-a1b9-0e8f37752fd0"`. By overriding `ToString`, we can make sure it will look like the raw underlying type when converted to a string: `c148b684-2c40-4383-a1b9-0e8f37752fd0`.

    2 people like this

    Posted: 6 years ago by Justin Hewlett

  • Alternative to using spaces in discriminated union cases

    This shows an alternative to using spaces in the discriminated union cases, which makes it harder to write in code, and tends to be needed when interoping with the likes of WPF or any other element that uses the ToString of the union case to display its value. Also useful as an alternative to using special characters like á or ç which make it harder to work with colleagues that don't have keyboards with those characters. Uses code from snippet http://fssnip.net/9l

    0 people like this

    Posted: 5 years ago by Micael Morais

  • Enforcing business rules

    A simple example of how to define a custom type that enforces extra business rules by using a single-case discriminated union with a private constructor

    7 people like this

    Posted: 4 years ago by Tomas Petricek