Undefinable return and Branching control feature #521
Replies: 4 comments
-
I find this construct rather complicated and confusable. If I had something similar to accomplish like you did in your example, then I would probably code it by hand anyhow. On the other hand, by thinking about how your wish for a direct |
Beta Was this translation helpful? Give feedback.
-
Depending on how ref-like types are going to work (see #472), you might be able to use them here. I think the code could look something like: [RefLike]
struct NullableRef<T>
{
private ref T value;
public ref T Value => HasValue ? ref value : throw new InvalidOperationException();
public bool HasValue { get; }
public NullableRef(ref T value)
{
this.value = ref value;
HasValue = true;
}
}
public NullableRef<Matrix4x4> TryGetTransform(string json)
{
if(string.IsNullOrEmpty(json))
return new NullableRef<Matrix4x4>(); // parameterless constructor is syntax for undefined
// { string name , Matrix4x4 transform }
var jobj = TryParseJson(json);
if(jobj == null || string.IsNullOrEmpty(jobj["name"]))
return new NullableRef<Matrix4x4>();
var name = (string)jobj["name"];
if(!refValueDictionary.ContainKeys(name)))
refValueDictionary[name] = json["transform"].ToObject<Matrix4x4>();
return new NullableRef<Matrix4x4>(ref refValueDictionary[name]);
} You wouldn't get all the syntactic convenience you're hoping for (no |
Beta Was this translation helpful? Give feedback.
-
Off topic:
I already had some situations where the null-evaluating 'convenience' operators would fit nicely in a structs operating flow. It'd be nice to have them sometimes. Yet I am not done with a well-engineered proposal. |
Beta Was this translation helpful? Give feedback.
-
Surely #497 already addresses this? |
Beta Was this translation helpful? Give feedback.
-
Currently now I think programming language should try to flatten the callback into return types. We have
IEnumerable
Task
Tuple
and so on added more and more into each versionThere are somethings can be return instead of callback. One I would like to have is nullable reference of valuetype
But nullable was already exist in the system as
Nullable<T>
struct. So I think I would need to rename this feature asundefinable
In essence this feature try to enforce consumer to use branching syntax that ensure that all the code will not touch the object when it will be undefined by throwing compile error
Except the
undefinable
new keyword I think we could reuse all nullable syntax to work withundefinable
And I think it could work like
async
by splitting a function into many sub function. But instead of making a syntax that will be execute once finished. It is a syntax that will make a branch and have compiler ensure that on the call site it will not touch the undefined branchMight work by transpiled into this
Alternative
Actually present alterternative is
try/catch
that we could just throw error and have the caller try to catch that. But it too slow. It defeat the purpose of using reference ValueType which we want to make it fast in the first placeThere also #497 about
ref?
. Actually this feature could useref?
in place ofundefinable
keyword if it not cause confusionAnother one is #494 that let we could return reference of any types. Just that we could check that some struct is null. So we could use nullable syntax over it
Last one is Nullable and tuple. Could we make some way to have nullable can return reference?
ps
Are there any problem to use this syntax with iterator or async?
async undefinable
orIEnumerable
ofundefinable Matrix
something like thatAnd I think we could use feature in place of nullable for better speed
This feature was revised from dotnet/roslyn#11552 about rework nullable. I think it would be breaking change to just rework nullable to I come up with another syntax instead
But ideally I wish
null
andnullable
should work this wayBeta Was this translation helpful? Give feedback.
All reactions