Skip to content

Instantly share code, notes, and snippets.

View davidelettieri's full-sized avatar

Davide Lettieri davidelettieri

View GitHub Profile
@davidelettieri
davidelettieri / Tests.fs
Last active August 9, 2019 12:43
Repro code
module Tests
open System
open Xunit
open FsUnit.Xunit
let f x =
if true then (None,[])
else
(Some x,[])
@davidelettieri
davidelettieri / Foo.cs
Created October 5, 2019 18:01
Boxing in ToString()
public class Foo
{
public int Value { get; set; }
public override string ToString()
{
return $"{{{nameof(Value)}={Value}}}";
}
}
@davidelettieri
davidelettieri / Foo.cs
Created October 12, 2019 16:16
FooNoBoxing
public class Foo
{
public int Value { get; set; }
public override string ToString()
{
return $"{{{nameof(Value)}={Value.ToString()}}}";
}
}
@davidelettieri
davidelettieri / Program.cs
Created February 4, 2020 11:51
Custom serializer ignored when deserializing arrays
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
namespace TestEnumSerialization
{
class Program
{
static void Main(string[] args)
{
@davidelettieri
davidelettieri / Union.cs
Last active March 25, 2020 21:57
Base union type
public class Union<TOne, TOther>
{
private readonly TOne _toneValue;
private readonly TOther _totherValue;
public Union(TOne value) => _toneValue = value;
public Union(TOther value) => _totherValue = value;
}
int? a = 1;
int? b = null;
bool ba = a.HasValue; // true
bool bb = b.HasValue; // false
int c = b.Value + 1; // throws
public class Union<TOne, TOther>
{
private readonly TOne _toneValue;
private readonly TOther _totherValue;
private readonly UnionType _type;
public Union(TOne value)
{
_toneValue = value;
_type = UnionType.TOne;
Union<int, None> a = new Union<int, None>(1);
Union<int, None> b = new Union<int, None>(new None());
// Here we are saying:
// if a (or b) contains an int, add 1 to the value and return the result
// if a (or b) contains None, just return 0 as a default value
var c = a.Match(v => v + 1, _ => 0); // this is 2
var d = b.Match(v => v + 1, _ => 0); // this is 0
public class Order
{
public bool IsPaid { get; set; }
private List<OrderItem> _items;
public List<OrderItem> GetItems() => new List<OrderItem>(_items);
public void Add(OrderItem item)
{
if (IsPaid)
public class Order
{
private List<OrderItem> _items;
public List<OrderItem> GetItems() => new List<OrderItem>(_items);
public void Add(OrderItem item)
{
_items.Add(item);
}