Value objects with a single value #6782
Replies: 5 comments 24 replies
-
What does "default value" mean in this context? What problem is this trying to solve?
You mean it should be a compiler error, or that it is today and you want it to be something different? |
Beta Was this translation helpful? Give feedback.
-
What would be different about this from a record struct with one field? public readonly record struct Name(string Value); |
Beta Was this translation helpful? Give feedback.
-
Will this be solved with Roles from #5496 ? |
Beta Was this translation helpful? Give feedback.
-
I think the OP wants to implement an implicit conversion. Name n = "Eyal"; // works.
string s = n; // works.
public record Name
{
private readonly string _value;
protected Name(string value)
=> _value = value;
public static implicit operator string(Name name)
=> name._value;
public static implicit operator Name(string value)
=> new (value);
} |
Beta Was this translation helpful? Give feedback.
-
You want to declare some wrapper types for primitive types and expect they act as their underlying primitive types. Is that right? I don't think making wrapper types are ideal. It's clear that Now we can declare alias for the exsisting types by: using Name = System.String;
using NickName = System.String; But the alias is only temporarily valid in the current file and the compiler doesn't prevent you from mixing different alias(all alias are considered as the same type). There should be a new mechanism to make the alias reusable and force the alias can only replace another name by one direction(so logicly they are not the same type but subtypes and siblings at design/compile time; at runtime they are still the standard type). The syntax may like: using Name : System.String;
using PersonName : Name;
using CompanyName : Name;
...
void PrintPersonName(PersonName pName) => Console.Write(pName);
...
string personNameAsString = "Person Name";
Name personNameAsName = (Name)personNameAsString; // a string may not be a Name, should use an explicit conversion
PersonName personName = (PersonName)personNameAsName; // a Name may not be a PersonName
CompanyName companyName = (CompanyName)personName; // a PersonName is not a CompanyName
Name someName = personName; // ok, a PersonName must be a Name
string.IsNullOrEmpty(personName); // ok, a PersonName must be a string
companyName.First(); // IEnumerable<char>.First() extension method can be applied, this is not possible for a wrapper class
PrintPersonName((PersonName)companyName); // the method require a PersonName parameter, should use an explicit conversion
personNameAsName.GetType(); // System.String, Name is just an alias for String
personName.GetType(); // System.String
companyName.GetType(); // System.String |
Beta Was this translation helpful? Give feedback.
-
I'd like to have the possibility to declare simple value objects (class or record) that contains a single value and use that as default value.
or
I could avoid a lot of errors with this feature.
Beta Was this translation helpful? Give feedback.
All reactions