andrewducker: (Default)
[personal profile] andrewducker
Generic constructors in C# cannot use type inference, so I either have to specify the types manually or use a static factory method.  I can't see why this would have been so hard...

Date: 2009-10-21 01:05 pm (UTC)
From: [identity profile] channelpenguin.livejournal.com
hmmm, being as I haven't got onto 3 I haven't even used type inference. Presume it gets rid of the need to do umpteen overloads. I assume it handles it a bit better than VB6's evil type co-oercion? I really do like my type incompatibility errors at compile time rather than runtime...

Date: 2009-10-21 02:03 pm (UTC)
simont: A picture of me in 2016 (Default)
From: [personal profile] simont
Disclaimer: I know next to no C#. The only C# I've ever written was a hasty line-by-line translation of a couple of C functions for the sake of somebody who wanted to include them in a C# program, so I didn't get to use any of this exciting stuff anyway.

That said, and assuming I've understood correctly, is this not just because you narrow the choice by declaring specific creator functions?

I mean. What are the possible types of the expression 37? It could be int, or it could be implicitly promoted to float, or (assuming C# hasn't dispensed with the outmoded idea of different-size integer types) it could be short or long or char. So if you just said new KeyValuePair("Andy",37), it wouldn't know whether to instantiate a KeyValuePair<string,int> or a KeyValuePair<string,char> or whatever, and it can't even guess from the type of the thing you're assigning into. But doing it with the function, it can rule out all those other options: the reason it can tell you meant int rather than char is because you defined CreateKeyValuePair<string,int> and didn't define CreateKeyValuePair<string,char>. So the compiler does need some means of narrowing down the available choices, and although that function looks as if it didn't do anything interesting, the utility of it is not in the function itself but in the knowledge of which set of such functions you bothered to define.

Or have I completely misunderstood?

eta: no, I have completely misunderstood, oops. Somehow I failed to notice that your creation function was actually totally generic itself. I'll go back to sleep.
Edited Date: 2009-10-21 02:04 pm (UTC)

Date: 2009-10-21 03:32 pm (UTC)
From: [identity profile] channelpenguin.livejournal.com
point: I am well up on generics, used them day in and day out for years. I dunno why it's important to me too ensure that you know that but it just IS. Probably a geek thing.... But someone else may have found that explanation useful.

37 is a valid value for several different types (e.g int, long), I can see why in the absence of any other info that C# would not be able to make a reasonable guess. It does seem inconsistent not to do what it does for method sigs for constructor sigs - I can't think offhand if there is any technical aspect of constructor meachanics that might tell us why that might be so.

I tend to specify the types most of the time anyway. I like to be as explicit as possible as a general rule. I'd particularly favour it where automatic inference could assume it was (say) an int where I know that it will in general be a long....

Date: 2009-10-21 03:37 pm (UTC)
From: [identity profile] channelpenguin.livejournal.com
there are nearly no situations where I'd be using literals like that anyway. A variable or property would have a type and so you'd know it explicitly anyway (or get it by reflection). IIRC the onlytime I really rely on inference is in dealing with .Value from DataRows or the like.

Date: 2009-10-21 04:29 pm (UTC)
From: [identity profile] channelpenguin.livejournal.com
wow. I really do NOT use literals, so that's mostly passed me by. Think I might have used M in some test code once...

you can say

long myval = 37;

Date: 2009-10-21 01:06 pm (UTC)
From: [identity profile] princealbert.livejournal.com
If i'm not mistaken the main restrictions in C# are to do with the Common Language Infrastructure, and .NET runtime architecture which is designed for portability to any .NET runtime enviroment be it Windows, WinCE or even xbox360

Date: 2009-10-21 02:54 pm (UTC)
From: [identity profile] momentsmusicaux.livejournal.com
I'd like PHP to give me error messages in ENGLISH instead of talking about T_THINGAMY. I mean, how hard is a lookup array that turns that into 'unquoted string' for instance?
Then again. I mostly wish it was perl.

March 2026

S M T W T F S
1 2 3 4 56 7
8 9 10 11 12 13 14
15 161718192021
22232425262728
293031    

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Mar. 16th, 2026 07:21 pm
Powered by Dreamwidth Studios