Replies: 25 comments
-
Also doesn't involve any language changes. You could create an |
Beta Was this translation helpful? Give feedback.
-
I have been using my own type for a long type. But you are right, the right repo would be CoreFX. Link to CoreFX related issue: |
Beta Was this translation helpful? Give feedback.
-
I think the better solution is nullable reference types. |
Beta Was this translation helpful? Give feedback.
-
Reference types are already nullable. And nullable values are a very poor replacement for maybe types. |
Beta Was this translation helpful? Give feedback.
-
@DavidArno in fact we are adding non-nullable references (for code analysis). Nullable ones are also concept for code analysis. They are not runtime-level concept at all. |
Beta Was this translation helpful? Give feedback.
-
Please no. My experience with Java is that the option type creates a lot of noise. It's a feature much better handled by the language, or at least not by a type on its own. |
Beta Was this translation helpful? Give feedback.
-
I'm not sure I'm following you. What do you mean by "in fact we are adding non-nullable references..."? I thought non-nullable refs were still in the "the team might do it, one day" stage? I'm also unsure what you mean by "code analysis" here. Non-nullable types will allow folk to avoid having
|
Beta Was this translation helpful? Give feedback.
-
Java's use of |
Beta Was this translation helpful? Give feedback.
-
They are for the same purpose, and in F# there's one type |
Beta Was this translation helpful? Give feedback.
-
We don't need a type to indicate nullability because all types reference types are nullable in .NET. What we need is to have the C# compiler be able to issue warnings for missing null checks on return types annotated with With |
Beta Was this translation helpful? Give feedback.
-
You are mistaken. Please see, for example, details of F#'s nullable operators. F# has to interop with the rest of .NET, so has to handle Sure, any self-respecting (F#) developer will only go near
Clearly we do, as otherwise var x = (new List<string>()).FirstOrDefault();
var y = (new List<string>{null}).FirstOrDefault();
// x == y: we have information loss
var x = (new List<string>()).TryFirst();
var y = (new List<string>{null}).TryFirst();
// x == none; y == null. No information has been lost. |
Beta Was this translation helpful? Give feedback.
-
No they won't, and they can't.
Why? Why is a specific Today we have |
Beta Was this translation helpful? Give feedback.
-
IMO this proposal is in the wrong place (DUs and other closed type hierarchies should be directly supported in the CLR and options, either and other common DUs should be supported by the BCL). However, it's become clear that folk are downvoting this, not because of that reason, but simply because they don't understand the purpose of options. As such, I've therefore upvoted it to counter that. |
Beta Was this translation helpful? Give feedback.
-
No one is confused about that. You're just parroting the option type from another language instead of analyzing how nullable types work in C# and trying to improve on that instead. Duct taping another nullability layer on the already existing nullability pattern is, in my opinion, a very bad idea, and I think Java's |
Beta Was this translation helpful? Give feedback.
-
The purpose of Option<T> is to make clear that something could have no
value.
Today all methods returning objects could potentialy to return null. Right?
There is no way to say that there is guarantees that the method will return
a Object every time. In the other side, there is no way to say that a
method could, by design decision, return null. The implications of this
fact is that we have to adopt a defensive approach all the time, but,...
sometimes people do not follow this strategy.
NullReferenceException is so common that there is no way to say that the
"null" is a good idea.
Option<T> and other elevated times like Either<L, R>, would help us to
write code with less mistakes. Why? Because we would have the opportunity
to better express our intention in the code making everything clearer.
Em qua, 19 de abr de 2017 às 06:41, Henning Moe <notifications@github.com>
escreveu:
… but simply because they don't understand the purpose of options.
No one is confused about that. You're just parroting the option type from
another language instead of analyzing how nullable types work in C# and
trying to improve on that instead. Duct taping another nullability layer on
the already existing nullability pattern is, in my opinion, a *very* bad
idea, and I think Java's Optional<T> perfectly shows it.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://github.com/dotnet/csharplang/issues/426#issuecomment-295190525>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAGd8qexYX7YDK_5QKzESV39ZnNsWvQOks5rxda8gaJpZM4M44NF>
.
|
Beta Was this translation helpful? Give feedback.
-
I've already shown you example: the information loss that var x = SomeThing.?Method1().?Method2(); here, if var x = anOption.Method1().Method2(); avoids those problems of null, avoids the need to protect again it at every step (or risk the code falling over in a heap) and ensures that The fact that you makes statements like "duct taping another nullability layer on the already existing nullability pattern is, in my opinion, a very bad idea" shows you are indeed ignorant of the purpose of options. You prove my own point for me. |
Beta Was this translation helpful? Give feedback.
-
Now where's your god? ;) It will return the same thing twice if Optional is a value type, and if it's a reference type it return |
Beta Was this translation helpful? Give feedback.
-
public struct Option<T> ...
var y = (new List<Option<string>>{null}).FirstOrDefault(); // Error. Option can't be null Your move... |
Beta Was this translation helpful? Give feedback.
-
public class Foo
{
public Option<string> Name { get; set; }
}
// ...
var f = new Foo();
var emptyList = new List<Option<string>>();
var list = new List<Option<string>>(f.Name); // Ops! default initialization will be a null option. We just end up with two ways stuff can be defined as not having a value. |
Beta Was this translation helpful? Give feedback.
-
@GeirGrusom You seem mightily confused about Options for someone who claims to not be confused by Options. Could I invite you to check the README for my library which discusses the need for Link: https://github.com/louthy/language-ext/ --- scroll down to "Null reference problem"
Depending on the And just to be clear, default initialisation of structs allows for creating an Option in a None state. So public struct Option<A>
{
public readonly bool IsSome;
readonly A Value;
Option(A value)
{
IsSome = value != null;
Value = value;
}
public static readonly Option<A> None = default(Option<A>);
public static Option<A> Some(A value) => new Option<A>(value);
public static implicit operator Option<A>(A value) => new Option<A>(value);
public B Match<B>(Func<A, B> Some, Func<B> None) =>
IsSome
? Some(Value)
: None();
} |
Beta Was this translation helpful? Give feedback.
-
Look at Kotlin. It doesn't use a Option type for nulls, but it does basically have it, but it's all in the compiler. fun getFoo() : Foo? -> ...
fun getFoo2() : Foo? -> ...
fun doFoo(v: Foo) -> ...
val f = getFoo() ?: getFoo2();
if(f != null)
{
doFoo(f);
} Kotlin will change the type of C# probably can't give an error, but it could give a warning. I would much rather have that than an Option type. Option types cannot be removed when nullable references are introduced. There is absolutely no chance of |
Beta Was this translation helpful? Give feedback.
-
And of course, if we fix your Kotlin code, the whole need to check for null goes away: fun getFoo = () : Foo -> ...
fun getFoo2 = () : Foo -> ...
fun doFoo = (v: Foo) -> ...
val f = if (someCond) getFoo() else getFoo2();
doFoo(f);
Indeed. It achieved that without using a lemon sorbet as well. That's because non-nullable references are different things to both lemon sorbets and options. |
Beta Was this translation helpful? Give feedback.
-
Nullable references was proposed by Andres Heljsberg and the idea is that it's more useful to indicate that something is nullable than the other way around. .NET has null, It will insert null when it bloody well pleases and that's a underlying machine functionality. It's completely unavoidable. You're just hiding them inside an abtraction that throw ValueWasNullException instead. Make the compiler enforce the runtime's rules about null rather than padding on an ugly abstraction on top. |
Beta Was this translation helpful? Give feedback.
-
@GeirGrusom Why do you keep ignoring what @DavidArno is saying to you?
No it won't. Stop making stuff up. This is just not true.
The underlying machine bears no relevance here. The CLR and the language implementation is what matters.
No he's not. It's perfectly possible to have a type-safe Option that never carries a I have actually taken a look at some of your code on your repository, and frankly I'm very surprised that you don't understand what the problem is. Take a look at this function from your [return: NotNull]
public static string GetValue([NotNull] this IConfiguration config, [NotNull] string key)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
string value = config.TryGetValue(key);
if (value == null)
{
throw new MissingConfigurationKeyException(key, config);
}
return value;
} The thing is cluttered with attempts to deal with This would be my public static Option<string> GetValue(Map<string, string> config, string key) =>
config.Find(key);
Option<string> GetValue(Map<string, string>, string) |
Beta Was this translation helpful? Give feedback.
-
The CoreFx issue #18159 seems to be a better place do discuss Maybe monads, except you propose some special syntax for it. |
Beta Was this translation helpful? Give feedback.
-
instead of
The proposed method signature reveals much better the real intent of this method/function. Right?
Option<T>
could provide aMatch
function....This proposal does not incur any CLR changes.
Beta Was this translation helpful? Give feedback.
All reactions