fosstodon.org is one of the many independent Mastodon servers you can use to participate in the fediverse.
Fosstodon is an invite only Mastodon instance that is open to those who are interested in technology; particularly free & open source software. If you wish to join, contact us for an invite.

Administered by:

Server stats:

8.8K
active users

#nullable

0 posts0 participants0 posts today
Habr<p>Nullable-аннотации: MaybeNull и NotNullWhen в C#</p><p>Привет, Хабр! Сегодня мы рассмотрим nullable-аннотации в C#: как с помощью [MaybeNull] и [NotNullWhen] (плюс родственных атрибутов вроде [MaybeNullWhen] , [NotNullIfNotNull] , [DoesNotReturn] ) формально описывать те самые «ну тут иногда null, а тут точно нет».</p><p><a href="https://habr.com/ru/companies/otus/articles/928286/" rel="nofollow noopener" translate="no" target="_blank"><span class="invisible">https://</span><span class="ellipsis">habr.com/ru/companies/otus/art</span><span class="invisible">icles/928286/</span></a></p><p><a href="https://zhub.link/tags/c" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>c</span></a># <a href="https://zhub.link/tags/nullable" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>nullable</span></a> <a href="https://zhub.link/tags/nullable%D0%B0%D0%BD%D0%BD%D0%BE%D1%82%D0%B0%D1%86%D0%B8%D0%B8" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>nullableаннотации</span></a> <a href="https://zhub.link/tags/nullable_reference_types" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>nullable_reference_types</span></a> <a href="https://zhub.link/tags/SystemDiagnosticsCodeAnalysis" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>SystemDiagnosticsCodeAnalysis</span></a> <a href="https://zhub.link/tags/MaybeNull" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>MaybeNull</span></a> <a href="https://zhub.link/tags/NotNullWhen" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>NotNullWhen</span></a> <a href="https://zhub.link/tags/MaybeNullWhen" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>MaybeNullWhen</span></a></p>
Mike P.<p>Was fällt euch auf? Ist das wirklich sinnvoll so?<br>Diskutiert mal! 👀</p><p>Bitte denkt selbst nach und fragt nicht einfach eine KI – eure eigenen Ideen interessieren mich viel mehr!</p><p><a href="https://det.social/tags/CSharp" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>CSharp</span></a> <a href="https://det.social/tags/dotnet" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>dotnet</span></a> <a href="https://det.social/tags/fail" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>fail</span></a> <a href="https://det.social/tags/dotnetperle" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>dotnetperle</span></a> <a href="https://det.social/tags/cleancode" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>cleancode</span></a> <a href="https://det.social/tags/boolLogic" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>boolLogic</span></a> <a href="https://det.social/tags/CodeReview" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>CodeReview</span></a> <a href="https://det.social/tags/nullable" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>nullable</span></a> <a href="https://det.social/tags/bug" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>bug</span></a></p>
Edaqa Mortoray<p><span class="h-card" translate="no"><a href="https://fosstodon.org/@badcel" class="u-url mention" rel="nofollow noopener" target="_blank">@<span>badcel</span></a></span> <span class="h-card" translate="no"><a href="https://mastodon.social/@khalidabuhakmeh" class="u-url mention" rel="nofollow noopener" target="_blank">@<span>khalidabuhakmeh</span></a></span> I guess I'm not fully understanding what <a href="https://peoplemaking.games/tags/nullable" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>nullable</span></a> does then, since it clearly does not mean things can't be null, at least not in all contexts.</p><p>I did use `where T: notnull` now and the error also goes away. But in my mind, that should be the default when <a href="https://peoplemaking.games/tags/nullable" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>nullable</span></a> is enabled.</p>
Marcel T. : :gnome: :dotnet:<p><span class="h-card" translate="no"><a href="https://peoplemaking.games/@mortoray" class="u-url mention">@<span>mortoray</span></a></span> <span class="h-card" translate="no"><a href="https://mastodon.social/@khalidabuhakmeh" class="u-url mention">@<span>khalidabuhakmeh</span></a></span> I think it is a bit confusing because T could either be a reference type or a value type. Value types having a completely different nullability mechanic which is not at all related to <a href="https://fosstodon.org/tags/nullable" class="mention hashtag" rel="tag">#<span>nullable</span></a> pragma. If you exclude structure types via : “ where T : class” the warning goes away, too and the class should behave as expected as it does not need to care for structs anymore.</p>
Edaqa Mortoray<p><span class="h-card" translate="no"><a href="https://mastodon.social/@khalidabuhakmeh" class="u-url mention" rel="nofollow noopener" target="_blank">@<span>khalidabuhakmeh</span></a></span> But yes, nonetheless, declaring it nonnull does fix the problem.</p><p>`class Something&lt;T&gt; where T : notnull`</p><p>But I still don't understand why. It seems to go against what `<a href="https://peoplemaking.games/tags/nullable" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>nullable</span></a> enable` means.</p>
Edaqa Mortoray<p><span class="h-card" translate="no"><a href="https://mastodon.social/@khalidabuhakmeh" class="u-url mention" rel="nofollow noopener" target="_blank">@<span>khalidabuhakmeh</span></a></span> But I have `<a href="https://peoplemaking.games/tags/nullable" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>nullable</span></a> enable` on the file, implying all references are non-null unless declared otherwise, like `T?`.</p><p>Also, if I mark source as `T? source` then I get errors in my code, implying that it isn't actually nullable, except in this circumstance.</p>
Brad Wilson<p><span class="h-card" translate="no"><a href="https://hachyderm.io/@andrewlock" class="u-url mention" rel="nofollow noopener" target="_blank">@<span>andrewlock</span></a></span> I see you enabled nullable, which is quite a minefield with a source-only package that wants to be able to work not only with nullable on or off, but also in a language where nullable isn't an option.</p><p>You end up with stuff like this:</p><p><a href="https://github.com/xunit/assert.xunit/blob/309647ac2fc29997937a22afaaeb0a73fb80d14b/CollectionAsserts.cs#L1-L8" rel="nofollow noopener" translate="no" target="_blank"><span class="invisible">https://</span><span class="ellipsis">github.com/xunit/assert.xunit/</span><span class="invisible">blob/309647ac2fc29997937a22afaaeb0a73fb80d14b/CollectionAsserts.cs#L1-L8</span></a></p><p><a href="https://github.com/xunit/assert.xunit/blob/309647ac2fc29997937a22afaaeb0a73fb80d14b/CollectionAsserts.cs#L457-L472" rel="nofollow noopener" translate="no" target="_blank"><span class="invisible">https://</span><span class="ellipsis">github.com/xunit/assert.xunit/</span><span class="invisible">blob/309647ac2fc29997937a22afaaeb0a73fb80d14b/CollectionAsserts.cs#L457-L472</span></a></p><p>You can't just "<a href="https://mastodon.social/tags/nullable" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>nullable</span></a> disable" because the compiler might not support it. So if they enable nullable but forget to "tell you", then you have to turn off a bunch of nullable "violations".</p>
Brad Wilson<p>This is super duper extra important.</p><p>C# Nullable Reference Types are a compile-time thing, not a runtime thing. "string?" and "string" are both just "string" at runtime.</p><p>This means:</p><p>- Always guard against your users passing `null` even for types that _shouldn't_ allow it, because it's *not a runtime guarantee*.</p><p>- You can't create overloads that differ only on nullability (i.e., Method(string) vs. Method(string?)) because *they're the same type at runtime*.</p><p><a href="https://mastodon.social/tags/dotnet" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>dotnet</span></a> <a href="https://mastodon.social/tags/csharp" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>csharp</span></a> <a href="https://mastodon.social/tags/nullable" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>nullable</span></a> <a href="https://mastodon.social/tags/nrt" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>nrt</span></a></p>
Egil Hansen<p>Hey <span class="h-card" translate="no"><a href="https://mstdn.social/@jasonbock" class="u-url mention" rel="nofollow noopener" target="_blank">@<span>jasonbock</span></a></span>, excellent walkthrough on source generators in <a href="https://www.youtube.com/watch?v=KTsyS3rDUgg" rel="nofollow noopener" translate="no" target="_blank"><span class="invisible">https://www.</span><span class="ellipsis">youtube.com/watch?v=KTsyS3rDUg</span><span class="invisible">g</span></a>.</p><p>You don't need to restart VS to observe changes to a source generator in another project. A `dotnet build-server shutdown` at the command line resets the build-server cache and forces the generators to re-run from scratch.</p><p>Wrt. to nullable, I typically wrap generated code in `<a href="https://mastodon.social/tags/nullable" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>nullable</span></a> annotation`/`<a href="https://mastodon.social/tags/nullable" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>nullable</span></a> restore`. Then I do not have to use the damn-it operator.</p>
Fandermill<p><span class="h-card" translate="no"><a href="https://dotnet.social/@anthony_steele" class="u-url mention">@<span>anthony_steele</span></a></span> for DTO&#39;s I&#39;ll take a &quot;<a href="https://fosstodon.org/tags/nullable" class="mention hashtag" rel="tag">#<span>nullable</span></a> disable&quot;</p>
Anthony Steele<p>for <a href="https://dotnet.social/tags/dotnet" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>dotnet</span></a> people, when we have <a href="https://dotnet.social/tags/nullable" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>nullable</span></a> checking on, how do you feel about DTO fields initialised to " = null!;"<br>e.g.</p><p> public string Name { get; set; } = null!;</p><p>It seems wrong to me "Name is not nullable, and also, surprise, it's null!"</p><p>It seems like that could go wrong.<br>But if it's the accepted idiom than I'll go with it.</p><p>Is there a better one? <br>"= string.Empty;" is verbose.</p><p>What do you think?</p>
Pxtl<p><span class="h-card"><a href="https://mastodon.social/@khalidabuhakmeh" class="u-url mention" rel="nofollow noopener" target="_blank">@<span>khalidabuhakmeh</span></a></span> like with "<a href="https://mastodon.social/tags/nullable" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>nullable</span></a> disable" or in VB.net "option strict" and "option explicit".</p>
billseipel<p>I&#39;m being punished. </p><p>I&#39;ve done something to anger the gods. lol</p><p><a href="https://fosstodon.org/tags/linq" class="mention hashtag" rel="tag">#<span>linq</span></a> <a href="https://fosstodon.org/tags/dotnet" class="mention hashtag" rel="tag">#<span>dotnet</span></a> <a href="https://fosstodon.org/tags/nullable" class="mention hashtag" rel="tag">#<span>nullable</span></a> <a href="https://fosstodon.org/tags/hell" class="mention hashtag" rel="tag">#<span>hell</span></a></p>
billseipel<p>I am dying of nullable type-itis. </p><p><a href="https://fosstodon.org/tags/linq" class="mention hashtag" rel="tag">#<span>linq</span></a> <a href="https://fosstodon.org/tags/dotnet" class="mention hashtag" rel="tag">#<span>dotnet</span></a> <a href="https://fosstodon.org/tags/nullable" class="mention hashtag" rel="tag">#<span>nullable</span></a> <a href="https://fosstodon.org/tags/hell" class="mention hashtag" rel="tag">#<span>hell</span></a></p>
JaykeBird<p>Maybe not that controversial take, but I love the nullable feature in C#. There are occasional situations where the nullable warning appears and I'm like "why???" But all in all, it has been a great help, and has generally made me a better programmer, even when doing stuff with .NET Framework.</p><p><a href="https://dotnet.social/tags/dotnet" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>dotnet</span></a> <a href="https://dotnet.social/tags/csharp" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>csharp</span></a> <a href="https://dotnet.social/tags/nullable" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>nullable</span></a> <a href="https://dotnet.social/tags/coding" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>coding</span></a></p>
VincentH.NET<p><a href="https://fosstodon.org/tags/CSharpForMarkup" class="mention hashtag" rel="tag">#<span>CSharpForMarkup</span></a> 2 for <a href="https://fosstodon.org/tags/WinUI3" class="mention hashtag" rel="tag">#<span>WinUI3</span></a> and <span class="h-card" translate="no"><a href="https://mastodon.social/@unoplatform" class="u-url mention">@<span>unoplatform</span></a></span> is coming along nicely🙂</p><p>-🔥Windows C# <a href="https://fosstodon.org/tags/hotreload" class="mention hashtag" rel="tag">#<span>hotreload</span></a> updates UI on Ctrl+S<br />- All <a href="https://fosstodon.org/tags/dotnet" class="mention hashtag" rel="tag">#<span>dotnet</span></a> 7 target platforms, <a href="https://fosstodon.org/tags/csharp" class="mention hashtag" rel="tag">#<span>csharp</span></a> 11<br />- Nested view files in sln explorer<br />- Fully <a href="https://fosstodon.org/tags/nullable" class="mention hashtag" rel="tag">#<span>nullable</span></a> + <a href="https://fosstodon.org/tags/editorconfig" class="mention hashtag" rel="tag">#<span>editorconfig</span></a> enabled dotnet new template</p>
I use :vim:<p>Is there a recommended pattern to follow when dealing with <a href="https://fosstodon.org/tags/nullable" class="mention hashtag" rel="tag">#<span>nullable</span></a> types in <a href="https://fosstodon.org/tags/TypeScript" class="mention hashtag" rel="tag">#<span>TypeScript</span></a> ?</p><p>This snippet is just from the <a href="https://fosstodon.org/tags/supabase" class="mention hashtag" rel="tag">#<span>supabase</span></a> <a href="https://fosstodon.org/tags/nextjs" class="mention hashtag" rel="tag">#<span>nextjs</span></a> quickstart guide. Since data maybe null when fetched from the Supabase bucket, TS complains since createObjectURL requires the parameter.</p><p>I know that I could guard the line with an &quot;if&quot; statement but that just seems unnecessary. Like there should be a more elegant solution, something like optional chaining. :thinking: :javascript: :react:</p>
Trek, Drugs & Jem’hadar<p>RT @praeclarum@twitter.com</p><p>New null policy: whenever I get a NullReferenceException, I put</p><p><a href="https://mastodon.social/tags/nullable" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>nullable</span></a> enable</p><p>at the top of the file and enable warnings as errors. C#’s progressive inclusion of null reference checking is a life savor on old projects.</p><p>🐦🔗: <a href="https://twitter.com/praeclarum/status/1161830903556915200" rel="nofollow noopener" target="_blank"><span class="invisible">https://</span><span class="ellipsis">twitter.com/praeclarum/status/</span><span class="invisible">1161830903556915200</span></a></p>