Repository navigation
Expand file tree
/
Copy pathObjectExt.cs
More file actions
67 lines (61 loc) · 2.23 KB
/
Copy pathObjectExt.cs
File metadata and controls
67 lines (61 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Reflection;
using System.Runtime.CompilerServices;
using LanguageExt.ClassInstances;
namespace LanguageExt
{
public static class ObjectExt
{
/// <summary>
/// Returns true if the value is equal to this type's
/// default value.
/// </summary>
/// <example>
/// 0.IsDefault() // true
/// 1.IsDefault() // false
/// </example>
/// <returns>True if the value is equal to this type's
/// default value</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsDefault<A>(this A value) =>
Check<A>.IsDefault(value);
/// <summary>
/// Returns true if the value is null, and does so without
/// boxing of any value-types. Value-types will always
/// return false.
/// </summary>
/// <example>
/// int x = 0;
/// string y = null;
///
/// x.IsNull() // false
/// y.IsNull() // true
/// </example>
/// <returns>True if the value is null, and does so without
/// boxing of any value-types. Value-types will always
/// return false.</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsNull<A>(this A value) =>
Check<A>.IsNull(value);
internal static class Check<A>
{
static readonly bool IsReferenceType;
static readonly bool IsNullable;
static Check()
{
IsNullable = Nullable.GetUnderlyingType(typeof(A)) != null;
IsReferenceType = !typeof(A).GetTypeInfo().IsValueType;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static bool IsDefault(A value) =>
default(EqDefault<A>).Equals(value, default);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static bool IsNull(A value) =>
(IsReferenceType && ReferenceEquals(value, null)) || (IsNullable && value.Equals(default));
}
}
}