473,406 Members | 2,847 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Is partial deduction of generic parameters possible

Vin
Hi,

I just wanted to check if I am missing something or if what I am
looking for isn't possible.

I have a function with 2 generic parameter types, one of these occurs
in the argument list the other doesn't.

So the function is like this:
void CreateEntities<T, IT>(IList<ITentities){}
I'd like to be able to call it as follows:
IList<IMyTypeentities = new List<IMyType>();
CreateEntities<MyType>(entities);

So 'IT' would be deduced as 'IMyType' and 'T' is explicitly set to
'MyType'.

The reasoning behind this is that in the function I will do the
following
entities.Add(new T());
I haven't been able to get this syntax to work.

I have to have:
CreateEntities<MyType, IMyType>(entities);
If I change the function to take an extra useless parameter of type
'MyType' it'll deduce the arguments.
So this will work:
void CreateEntities<T, IT>(IList<ITentities, T t){}
CreateEntities(entities, new MyType());
but clearly that isn't a useful option.
So is it possible for the compiler to deduce one parameter type or is
it all or none?

Thanks, Vin
Jun 27 '08 #1
10 2140
On May 14, 3:40 pm, Vin <vfinn...@gmail.comwrote:

<snip>
So is it possible for the compiler to deduce one parameter type or is
it all or none?
All or none, I'm afraid.

Jon
Jun 27 '08 #2
Vin
On May 14, 3:44*pm, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
All or none, I'm afraid.
Thought it might be :o(

Thanks, Vin
Jun 27 '08 #3
A shame really... it would be nice (especially when working with
anonymous types) to be able to mix'n'match - perhaps something like:

SomeFunc<?,int>(...)

(where ? means "infer")

I can't think of the examples right now, but I know I've had to refactor
things a few times to get around this... mainly where there isn't any
additional argument relating to a specific argument, or where the
compiler can't fully do the job by itself. Of course, if you have an
anonymous type it becomes very hard to help it!

(we all have our C# wishlists, this is on mine...)

Marc
Jun 27 '08 #4
On May 16, 3:48*pm, Marc Gravell <marc.grav...@gmail.comwrote:
A shame really... it would be nice (especially when working with
anonymous types) to be able to mix'n'match - perhaps something like:

SomeFunc<?,int>(...)

(where ? means "infer")

I can't think of the examples right now, but I know I've had to refactor
things a few times to get around this... mainly where there isn't any
additional argument relating to a specific argument, or where the
compiler can't fully do the job by itself. Of course, if you have an
anonymous type it becomes very hard to help it!

(we all have our C# wishlists, this is on mine...)
It's been mentioned by other people, too:

http://www.interact-sw.co.uk/iangblo...mbda-inference

In particular, Ian mentions "in C# team member blogs" which sounds
positive.
Not that I'd like to guess whether it'll be in C# 4 or not...

Jon

Jun 27 '08 #5
It's been mentioned by other people, too

Good link, cheers. I like the terminology: "mumble types" ;-p

Marc
Jun 27 '08 #6
Vin
On May 16, 4:25*pm, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On May 16, 3:48*pm, Marc Gravell <marc.grav...@gmail.comwrote:
A shame really... it would be nice (especially when working with
anonymous types) to be able to mix'n'match - perhaps something like:
SomeFunc<?,int>(...)
(where ? means "infer")
I can't think of the examples right now, but I know I've had to refactor
things a few times to get around this... mainly where there isn't any
additional argument relating to a specific argument, or where the
compiler can't fully do the job by itself. Of course, if you have an
anonymous type it becomes very hard to help it!
(we all have our C# wishlists, this is on mine...)

It's been mentioned by other people, too:

http://www.interact-sw.co.uk/iangblo...mbda-inference

In particular, Ian mentions "in C# team member blogs" which sounds
positive.
Not that I'd like to guess whether it'll be in C# 4 or not...

Jon
Hi,

Maybe I am missing something but why would you need the extra syntax?

I'd have thought anything not available from arguments would have to
be specified and anything available wouldn't so the current syntax
would work fine.

void Fn<T1, T2, T3>(T2 t2, T3 t3) {}
would have to be called as
Fn<int>(0, 8);

This does put the restriction that explicit types would have to come
first but the call is cleaner than having to call
Fn<int, ?, ?>(0, 8);

I know this is all speculation at this stage but is there an advantage
to the mumble syntax?

Vin
Jun 27 '08 #7
On May 21, 11:11 am, Vin <vfinn...@gmail.comwrote:
Maybe I am missing something but why would you need the extra syntax?

I'd have thought anything not available from arguments would have to
be specified and anything available wouldn't so the current syntax
would work fine.
I suspect you'd find it would work fine in some simple cases but not
other more complicated ones. For instance:
void Fn<T1, T2, T3>(T2 t2, T3 t3) {}
would have to be called as
Fn<int>(0, 8);
And what about when I want to pass null as the second argument? It
would be nice to be able to specify the type for that without casting
the null.

There are various other times where you want to explicitly state the
area of ambiguity.
This does put the restriction that explicit types would have to come
first but the call is cleaner than having to call
Fn<int, ?, ?>(0, 8);
That's true when it's blindingly obvious which types you want to be
inferred. Other times it may not be so obvious, and that's where
mumble typing helps.

Jon
Jun 27 '08 #8
void Fn<T1, T2, T3>(T2 t2, T3 t3) {}
would have to be called as
Fn<int>(0, 8);
And what if we also have a Fn<T>(...)?

Marc
Jun 27 '08 #9
On May 21, 1:16 pm, Marc Gravell <marc.grav...@gmail.comwrote:
void Fn<T1, T2, T3>(T2 t2, T3 t3) {}
would have to be called as
Fn<int>(0, 8);

And what if we also have a Fn<T>(...)?
That's a much better answer than mine :)

Jon
Jun 27 '08 #10
Vin
On May 21, 1:16*pm, Marc Gravell <marc.grav...@gmail.comwrote:
void Fn<T1, T2, T3>(T2 t2, T3 t3) {}
would have to be called as
Fn<int>(0, 8);

And what if we also have a Fn<T>(...)?

Marc
Fair enough!

Thanks, Vin
Jun 27 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Gianni Mariani | last post by:
Does anyone know if this is supposed to work ? template <unsigned N> int strn( const char str ) { return N; } #include <iostream>
14
by: Bart Samwel | last post by:
Hi everybody, I would really like some help explaining this apparent discrepancy, because I really don't get it. Here is the snippet: void foo(int&); void foo(int const&); ...
4
by: Erik Wikström | last post by:
In school (no I will not ask you to do my schoolwork for me) we talked about policy-based design and got an assignment where we got the a code- fragment from a stack-implementation. The idea with...
4
by: Neelesh | last post by:
Hi all, I had some confusion about deduction of non-type template parameters for function templates : template <class T, int i> void foo (T, int p = i) ; void bar() { foo<int, 10>(40,40);...
5
by: Andreas Mueller | last post by:
Hi All, I have an overloaded generic method anmed "Foo": Class Test Class Xox(Of T) End Class Class Lulli(Of T) Inherits Xox(Of T) End Class
6
by: rincewind | last post by:
Hi, can anybody summarise all options for partial template specialization, for all kind of parameters (type, nontype, template)? I *think* I understand options for partial specialization on...
2
by: coolpint | last post by:
Can anyone kindly provide an explanation as to why the compiler does not "see" the function template in the contrieved code below? I think the argument deduction fails but can't figure out...
6
by: year1943 | last post by:
For template <typename Tclass My ; I can define partial spec-ns somewhat like template <typename Tclass My<T*; or template <typename Tclass My<Another<T ; And full spec-n, say template <class...
4
by: KD | last post by:
I have a template function and I'm looking for a way to force the caller to specify the template parameters. In other words, I would like to turn off template parameter deduction. For example, ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.