patterncsharpMinor
Constrained type alias
Viewed 0 times
typealiasconstrained
Problem
What do you think about the following syntax for some very simple and intensively reusable validations?
Does this:
mean for you a none-safe cast to none-empty required text? How does it play with C# syntax/semantics? So these two should throw:
More examples:
Basically, I am just looking for type alias with a guard constrain, but not so many things are possible in C#...
Let’s define the casts (yes, it is ugly):
Where library code is:
P.S. More examples:
P.P.S. Even more examples:
Does this:
string name = (SomeText)"Hm…";mean for you a none-safe cast to none-empty required text? How does it play with C# syntax/semantics? So these two should throw:
string name = (SomeText)null;
string name = (SomeText)"";More examples:
int Age = (AdultAge)43; // OK
int Age = (AdultAge)10; // throws…Basically, I am just looking for type alias with a guard constrain, but not so many things are possible in C#...
Let’s define the casts (yes, it is ugly):
class SomeText : Constrain
{
public static implicit operator SomeText(string value) =>
Create(value, !string.IsNullOrWhiteSpace(value));
}
class AdultAge : Constrain
{
public static implicit operator AdultAge(int value) =>
Create(value, value >= 21);
}Where library code is:
abstract class Constrain
where TConstrain : Constrain, new()
{
public static implicit operator TValue(Constrain check) =>
check.Value;
protected static TConstrain Create(TValue value, bool satisfied, string message = null)
{
if (!satisfied)
throw new ArgumentException(message ?? $"{typeof(TConstrain).Name} required.");
else
return new TConstrain() { Value = value };
}
TValue Value { get; set; }
}P.S. More examples:
string email = (EMail)"test@example.com";
Stream stream = (InputStream)File.OpenText("c:\Autoexec.bat");
double weight = (Positive)160;P.P.S. Even more examples:
class Adult
{
public Adult(string name, int age)
{
Name = (SomeText)name;
Age = (AdultAge)age;
}
public override string ToString() => $"{Name}, {Age}";
string Name { get; }
int Age { get; }
}Solution
Your recent questions give me impression, that you are purposely trying to to find new ways to mock C# syntax. :) You know this saying from MSDN: "code is read way more often, than it is written"? I think you have to keep this in mind, when designing those things. I would definitely have a WTF-moment, if I were to see
or this:
or even this:
Because those options are easy to read and easy to understand. They feel natural. Your code does not. It's clever, but it is obscure and it is easy to get wrong:
int Age = (AdultAge)43 in code. I then would have to go and look for AdultAge and Constrain implementations in order to figure out what is going on there. I would rather have this:Validator.ValidateEmail("test@example.com");or this:
reference.ThrowIfNull();or even this:
var age = (Age)21;
if (age.IsAdult) ...Because those options are easy to read and easy to understand. They feel natural. Your code does not. It's clever, but it is obscure and it is easy to get wrong:
int Age1 = (AdultAge)43; // OK
var Age2 = (AdultAge)43; // OK?
double Age3 = (AdultAge)43.1; // OK?
var Age4 = String.Format("Age: {0}", (AdultAge)43); // OK?Code Snippets
Validator.ValidateEmail("test@example.com");reference.ThrowIfNull();var age = (Age)21;
if (age.IsAdult) ...int Age1 = (AdultAge)43; // OK
var Age2 = (AdultAge)43; // OK?
double Age3 = (AdultAge)43.1; // OK?
var Age4 = String.Format("Age: {0}", (AdultAge)43); // OK?Context
StackExchange Code Review Q#131917, answer score: 2
Revisions (0)
No revisions yet.