1

Topic: Minute WTF-20: it is Less than code - less errors

For a long time did not see the Author: Sinix Date: 28.02.17, . I will be short.//c# 7.1 static bool IsDefault <T> (T value) => Equals (value, default); static void Main (string [] args) {Console. WriteLine (IsDefault (0)); Console. ReadKey ();} the Question standard: what not so?

2

Re: Minute WTF-20: it is Less than code - less errors

3

Re: Minute WTF-20: it is Less than code - less errors

Hello, Sinix, you wrote: S> For a long time did not see the Author: Sinix Date: 28.02.17, . S> I Will be short. S> S>//c# 7.1 S> static bool IsDefault <T> (T value) => Equals (value, default); S> static void Main (string [] args) S> {S> Console. WriteLine (IsDefault (0)); S> Console. ReadKey (); S>} S> S> the Question standard: what not so? I will assume that will be caused Equals (object) As the compiler does not know (there is no restriction where) that the type can be significant. Therefore all compiles as for object. Well and accordingly 10 == null that is false.

4

Re: Minute WTF-20: it is Less than code - less errors

Hello, Jack128, you wrote: J> to Answer without compilation?? J> <spoiler> of Ugu. The studio, however, insists on the reverse. Unlucky diagnostics any.

5

Re: Minute WTF-20: it is Less than code - less errors

Hello, Sinix, you wrote: S> the Question standard: what not so? The answer standard: sudden boxing.

6

Re: Minute WTF-20: it is Less than code - less errors

Hello, Sharov, you wrote: S> Hello, Sinix, you wrote: S>> the Question standard: what not so? S> the Answer standard: sudden boxing. No, not . That as are compiled sample type/methods. If there is no restriction (where) everything is compiled, as for object type. As the compiler cannot do assumptions of what types will be transferred in runtime If in restriction is specified where T: SomeClass that methods for SomeClass (for example it is possible  if to have class hierarchy at which are redefined operators equals but not to turn these implementations through virtual Equals) will be compiled. Here already the compiler knows that at least SomeClass will be, instead of object the general, therefore so.

7

Re: Minute WTF-20: it is Less than code - less errors

Hello, Sharov, you wrote: S>> the Question standard: what not so? S> the Answer standard: sudden boxing.  "Pushkin/gladiolus" in

8

Re: Minute WTF-20: it is Less than code - less errors

V> I Will assume that will be caused Equals (object) V> As the compiler does not know (there is no restriction where) that the type can be significant. Therefore all compiles as for object. Well and accordingly 10 == null that is false. And why so works correctly? static bool IsDefault <T> (T value) => Equals (value, default (T)); here too is not present where.

9

Re: Minute WTF-20: it is Less than code - less errors

Hello, yenik, you wrote: V>> I Will assume that will be caused Equals (object) V>> As the compiler does not know (there is no restriction where) that the type can be significant. Therefore all compiles as for object. Well and accordingly 10 == null that is false. Y> And why so works correctly? Y> Y> static bool IsDefault <T> (T value) => Equals (value, default (T)); Y> Y> here too is not present where. It is There and then explicitly specified default (T).

10

Re: Minute WTF-20: it is Less than code - less errors

V>>> I Will assume that will be caused Equals (object) V>>> As the compiler does not know (there is no restriction where) that the type can be significant. Therefore all compiles as for object. Well and accordingly 10 == null that is false. Y>> And why so works correctly? Y>> Y>> static bool IsDefault <T> (T value) => Equals (value, default (T)); Y>> Y>> here too is not present where. _NN> it is There and then explicitly specified default (T). And T - referential or structural? Same it is not specified. Yes, actually, the answer is already given: http://rsdn.org/forum/dotnet/6956070.1 the Author: Jack128 Date: 06.11 19:11 In dock it is told. You can now omit the type on the right-hand side of the initialization: C# Func <string, bool> whereClause = default; But in a case with Equals (value, default) the compiler cannot guess type and selects object. Equals (value, default (object)).

11

Re: Minute WTF-20: it is Less than code - less errors

Hello, yenik, you wrote: the Code without sugar of questions after all does not cause? static bool IsDefault <T> (T value) {T d = default (T); return Equals (value, d);//Works as it is necessary} Or it is necessary to look IL: Equals (value, default); IL_0000: ldarg.0 IL_0001: box!! T IL_0006: ldnull IL_0007: call bool [mscorlib] System. Object:: Equals (object, object) Equals (value, default (T));//Method begins at RVA 0x2050//Code size 26 (0x1a).maxstack 2.locals init ([0]!! T) IL_0000: ldarg.0 IL_0001: box!! T IL_0006: ldloca.s 0 IL_0008: initobj!! T IL_000e: ldloc.0 IL_000f: box!! T IL_0014: call bool [mscorlib] System. Object:: Equals (object, object)

12

Re: Minute WTF-20: it is Less than code - less errors

Hello, yenik, you wrote: V>> I Will assume that will be caused Equals (object) V>> As the compiler does not know (there is no restriction where) that the type can be significant. Therefore all compiles as for object. Well and accordingly 10 == null that is false. Y> And why so works correctly? Y> Y> static bool IsDefault <T> (T value) => Equals (value, default (T)); Y> Y> here too is not present where. These are different scenarios. Equals (value, default) - means to cause method Equals c in the first parameter value and the second "by default" for type of the second argument. . Equals (object, object) it will be caused as Equals (value, default (object)) In the second case you explicitly specify what type by default it is necessary to take value.

13

Re: Minute WTF-20: it is Less than code - less errors

Hello, Sinix, you wrote: S> static bool IsDefault <T> (T value) => Equals (value, default); If the code looks correctly, and works - incorrectly, to stick into it with a nose of all of these Lippertov and as at school: "Rewrite anew, !".

14

Re: Minute WTF-20: it is Less than code - less errors

Hello, Sinix, you wrote: S> For a long time did not see the Author: Sinix Date: 28.02.17, . S> I Will be short. S> S>//c# 7.1 S> static bool IsDefault <T> (T value) => Equals (value, default); S> static void Main (string [] args) S> {S> Console. WriteLine (IsDefault (0)); S> Console. ReadKey (); S>} S> default (T)?

15

Re: Minute WTF-20: it is Less than code - less errors

Hello, Sinclair, you wrote: S> default (T)? It. In real life looks somehow so.

16

Re: Minute WTF-20: it is Less than code - less errors

Hello, vasmann, you wrote: V> Is not present, not . That as are compiled sample type/methods. V> if there is no restriction (where) everything is compiled, as for object type. As the compiler cannot do assumptions of what types will be transferred in runtime where here not and. For T without problems it is possible to generate correctly  value. Here a problem in Equals. It not generalized, and accepts two object. Accordingly the compiler cannot deduce type for default and substitutes null (for object). Well, and Equals considers 0 and null as different values (not With ++ nevertheless). If to set type explicitly default (T) or to use a dzhenerik-method: static bool IsDefault <T> (T value) => EqualityComparer <T>.Default. Equals (value, default); that the compiler can deduce types and all will be a bundle. On mind the compiler would need to give the warning that default is substituted in a place where it is required object and what better explicitly to set null.

17

Re: Minute WTF-20: it is Less than code - less errors

Hello, Kolesiki, you wrote: K> If the code looks correctly, and works - incorrectly, to stick into it with a nose of all of these Lippertov and as at school: "Rewrite anew, !" . Here it is necessary not to rewrite, and  in the compiler to add. default has no special sense for object as default without parameters is instructions to the compiler to deduce type from usage. And the output object is useless. Well, and these curves - Equals th on object should finish the inheritance for a long time. Still in 2.0 it was necessary to enter except it also the generalized implementation causing EqualityComparer <T>.Default. Equals (T, T). Then it would be preferred by the compiler, at resolution of an overload and the type would successfully be deduced. The operator == for reference types in which it still makes sense to forbid is not defined. For them == causes check of equality of links that as can lead to bugs. The person explicitly should specify that wants to receive referential , instead of logical equality. Lead object or use object. ReferenceEquals ().

18

Re: Minute WTF-20: it is Less than code - less errors

Hello, vasmann, you wrote: V> Equals (value, default) - means to cause method Equals c in the first parameter value and the second "by default" for type of the second argument. . Equals (object, object) it will be caused as Equals (value, default (object)) In this "scenario" there is no sense. And the compiler should notify about it. All sense of the given subject that similar errors of figs you will note in the code. These are a rake about which it is necessary to know. A C ++ way (tm)

19

Re: Minute WTF-20: it is Less than code - less errors

Hello, VladD2, you wrote: VD> the operator == for reference types in which it Still makes sense to forbid is not defined. For them == causes check of equality of links that as can lead to bugs. The person explicitly should specify that wants to receive referential , instead of logical equality. Lead object or use object. ReferenceEquals (). Yes it is exact. Changeover of 100500 checks on null in the code on ReferenceEquals is a fine idea. In Nemerle as, forbade already?... <<RSDN@Home 1.0.0 alpha 5 rev. 0 on Windows 8 6.2.9200.0>>

20

Re: Minute WTF-20: it is Less than code - less errors

Hello, AndrewVK, you wrote: AVK> Yes it is exact. Changeover of 100500 checks on null in the code on ReferenceEquals is a fine idea. In Nemerle as, forbade already? And and here check on null? With it, just, no problems are present. Well, besides, that in 90 % of cases is a bojlerplejt-code which it is necessary to write because of wretchedness of languages. null is a special literal. The second operand at "! = null" it is possible to object to result the automatic machine. It is a question of comparing of two objects. If at them still besides are redefined GetHashcode and Equals is almost for certain an error. If it becomes intentionally better it explicitly to specify.

21

Re: Minute WTF-20: it is Less than code - less errors

Hello, VladD2, you wrote: VD> It is a question of comparing of two objects. If at them still besides are redefined GetHashcode and Equals is almost for certain an error. If it becomes intentionally better it explicitly to specify. : the majority of sentences in   and look. The people eternally forget about difficult parts (well or it is simple about them does not know, too happens) and start to insist furiously on simple, obvious and wrong decisions On a subject - double. There on determination (NaN == NaN)! = NaN.Equals (NaN). And for esthetes, int? a = null; int? b = 5; if (a <= b == Comparer <int?>.Default. Compare (a, b) <= 0)//what not so?...

22

Re: Minute WTF-20: it is Less than code - less errors

Hello, Sinix, you wrote: VD>> It is a question of comparing of two objects. If at them still besides are redefined GetHashcode and Equals is almost for certain an error. If it becomes intentionally better it explicitly to specify. S> Offtop: the majority of sentences in   and look. S> the people eternally forget about difficult parts (well or it is simple about them does not know, too happens) and start to insist furiously on simple, obvious and wrong decisions With you it is almost impossible to argue. You are a priori right. After all you define that a sentence wrong, and such trifles as the argumentation of you do not trouble. I, here, with you and do not want to argue. I already use more than 10 years language where "wrong" sentences perfectly work in practice and preserve against a row of errors. Here on a fig to me to you something to prove? Continue to consider as correct only that  the MSEC and write next WHF.

23

Re: Minute WTF-20: it is Less than code - less errors

Hello, VladD2, you wrote: VD> With you it is almost impossible to argue. You are a priori right. After all you define that a sentence wrong, and such trifles as the argumentation of you do not trouble. , it is accepted Wrong - not absolutely correct word. I can replace on "as a result will not work without breakage of compatibility with an existing.net-code" then like all it is true. VD> I, here, with you and do not want to argue. I already use more than 10 years language where "wrong" sentences perfectly work in practice and preserve against a row of errors. Here on a fig to me to you something to prove? Continue to consider as correct only that  the MSEC and write next WHF. Well so you also do not have heavy heritage in the form of a heap , necessities of design under 95 % community and needs to save compatibility with the code written 17 years ago, truly? The people in.Net Core already tried to go such way. Two years it as though worked, and then suddenly came  from big clients. As a result as early as two years of a work involving all hands and at us again almost compatible with.full net FW. Not as dispute, it is really interesting: and how in language simultaneously to implement sql-style operators for nullables and thus not to break dictionaries/sorted set? Or we do so that implementations of operators and standard equatable/comparable-interfaces did not coincide, or we continue to attack a rake.

24

Re: Minute WTF-20: it is Less than code - less errors

Hello, VladD2, you wrote: AVK>> Yes it is exact. Changeover of 100500 checks on null in the code on ReferenceEquals is a fine idea. In Nemerle as, forbade already? VD> And and here check on null? With it, just, no problems are present. Well, besides, that in 90 % of cases is a bojlerplejt-code which it is necessary to write because of wretchedness of languages. null is a special literal. The second operand at "! = null" it is possible to object to result the automatic machine. So forbade already in Nemerle or how?... <<RSDN@Home 1.0.0 alpha 5 rev. 0 on Windows 8 6.2.9200.0>>

25

Re: Minute WTF-20: it is Less than code - less errors

Hello, Sinix, you wrote: S> Hello, VladD2, you wrote: VD>> It is a question of comparing of two objects. If at them still besides are redefined GetHashcode and Equals is almost for certain an error. If it becomes intentionally better it explicitly to specify. S> Offtop: the majority of sentences in   and look. S> the people eternally forget about difficult parts (well or it is simple about them does not know, too happens) and start to insist furiously on simple, obvious and wrong decisions As far as I understand, Vlad offers the prohibition only for reference types. Then it will be unambiguous a == b to cause operator ==, or it will not be compiled if it is not present. And if referential comparing it is explicit object is necessary. ReferenceEquals (a, b) or explicit coercion to object: (object) a == b