473,322 Members | 1,473 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,322 software developers and data experts.

C# generics: is it possible to infer associated types?

Hi,

after reading the documentation on C# generics from several sources, I'm
still unclear whether it is possible to infer associated types from a
type argument. For example, if I have an enumerator or iterator for a
sequence passed to a generic function, is it possible to arrange things
such that the element type of the underlying sequence can be inferred?

Inferring associated types from type arguments becomes necessary as soon
as you are doing more than absolutely trivial stuff like the ubiquitous
stack example. A simple concrete example is a function which given an
enumerator and a predicate returns the enumerator when the predicate
yields true fo the element:

public interface Enumerator<T> {
public T next();
}
public interface Predicate<T> {
public bool test(T val);
}

public class algos {
public static E find<E, P>(E enumerator, P predicate)
where E: Enumerator<S>
where P: Predicate<T>
{
S tmp = null
do
tmp = enumerator.next();
while (tmp != null && !predicate.text(tmp))
return enumerator;
}
}

The essential portion in this example is that the type parameters 'S' and
'T' can be inferred from the type parameters 'E' and 'P': otherwise it is
impossible to spell out the constraints for 'E' and 'P' to have a 'next()'
and a 'test()' function, respectively. The type returned from 'next()' and
the type expected by 'test()' are potentially different because 'T' can be
a base class of 'S' to make the algorithm still applicable.

The above code uses a syntax I have made up because I haven't seen it in
this form anywhere (which doesn't mean that it isn't described somewhere):
Essentially, the constraints are used for two purposes. On the one hand,
they spell out the expected interfaces of the two parameters. On the other
hand, they are used to infer the types 'S' and 'T': 'S' is the element
type of the sequence iterated over by 'E' and 'T' is the type the
predicate operates on. Whether the notation I used or something else is
used I don't care at all. However, I do care about the possibility to
infer associated types.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>
Nov 15 '05 #1
4 2689
C# for now doesn't support templates :(

kuba florczyk
Hi,

after reading the documentation on C# generics from several sources, I'm
still unclear whether it is possible to infer associated types from a
type argument. For example, if I have an enumerator or iterator for a
sequence passed to a generic function, is it possible to arrange things
such that the element type of the underlying sequence can be inferred?

Inferring associated types from type arguments becomes necessary as soon
as you are doing more than absolutely trivial stuff like the ubiquitous
stack example. A simple concrete example is a function which given an
enumerator and a predicate returns the enumerator when the predicate
yields true fo the element:

public interface Enumerator<T> {
public T next();
}
public interface Predicate<T> {
public bool test(T val);
}

public class algos {
public static E find<E, P>(E enumerator, P predicate)
where E: Enumerator<S>
where P: Predicate<T>
{
S tmp = null
do
tmp = enumerator.next();
while (tmp != null && !predicate.text(tmp))
return enumerator;
}
}

The essential portion in this example is that the type parameters 'S' and
'T' can be inferred from the type parameters 'E' and 'P': otherwise it is
impossible to spell out the constraints for 'E' and 'P' to have a 'next()'
and a 'test()' function, respectively. The type returned from 'next()' and
the type expected by 'test()' are potentially different because 'T' can be
a base class of 'S' to make the algorithm still applicable.

The above code uses a syntax I have made up because I haven't seen it in
this form anywhere (which doesn't mean that it isn't described somewhere):
Essentially, the constraints are used for two purposes. On the one hand,
they spell out the expected interfaces of the two parameters. On the other
hand, they are used to infer the types 'S' and 'T': 'S' is the element
type of the sequence iterated over by 'E' and 'T' is the type the
predicate operates on. Whether the notation I used or something else is
used I don't care at all. However, I do care about the possibility to
infer associated types.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>

Nov 15 '05 #2
Kuba Florczyk wrote:
C# for now doesn't support templates :(


Yes, but apparently a specification on generics for C# and the CLR is
already on its way. I have seen some of the stuff coming up but it
seems to be substantially restricted. If so, maybe it is not too late
to change things.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>
Nov 15 '05 #3
The question, which I can't get my head around, is whether
directly specifying S and T as type parameters would work.
If that works it seems like inferring S and T from E and P is
a task for the C# compiler writers and not fundamentally
incompatible with the proposed generics support in .NET.

The example would then look like this:

public interface Enumerator<T> {
public T next();
}
public interface Predicate<T> {
public bool test(T val);
}
public class algos {
public static E find<E, P, S, T>(E enumerator, P predicate)
where E: Enumerator<S>
where P: Predicate<T>
{
S tmp = null
do
tmp = enumerator.next();
while (tmp != null && !predicate.text(tmp))
return enumerator;
}
}

Would this work or not?

Thanks,
Andrew
"Dietmar Kuehl" <di***********@yahoo.com> wrote in message
news:5b**************************@posting.google.c om...
Hi,

after reading the documentation on C# generics from several sources, I'm
still unclear whether it is possible to infer associated types from a
type argument. For example, if I have an enumerator or iterator for a
sequence passed to a generic function, is it possible to arrange things
such that the element type of the underlying sequence can be inferred?

Inferring associated types from type arguments becomes necessary as soon
as you are doing more than absolutely trivial stuff like the ubiquitous
stack example. A simple concrete example is a function which given an
enumerator and a predicate returns the enumerator when the predicate
yields true fo the element:

public interface Enumerator<T> {
public T next();
}
public interface Predicate<T> {
public bool test(T val);
}

public class algos {
public static E find<E, P>(E enumerator, P predicate)
where E: Enumerator<S>
where P: Predicate<T>
{
S tmp = null
do
tmp = enumerator.next();
while (tmp != null && !predicate.text(tmp))
return enumerator;
}
}

The essential portion in this example is that the type parameters 'S' and
'T' can be inferred from the type parameters 'E' and 'P': otherwise it is
impossible to spell out the constraints for 'E' and 'P' to have a 'next()'
and a 'test()' function, respectively. The type returned from 'next()' and
the type expected by 'test()' are potentially different because 'T' can be
a base class of 'S' to make the algorithm still applicable.

The above code uses a syntax I have made up because I haven't seen it in
this form anywhere (which doesn't mean that it isn't described somewhere):
Essentially, the constraints are used for two purposes. On the one hand,
they spell out the expected interfaces of the two parameters. On the other
hand, they are used to infer the types 'S' and 'T': 'S' is the element
type of the sequence iterated over by 'E' and 'T' is the type the
predicate operates on. Whether the notation I used or something else is
used I don't care at all. However, I do care about the possibility to
infer associated types.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>

Nov 15 '05 #4
Please ask on the language message board at

http://msdn.microsoft.com/vcsharp/te...e/default.aspx

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://blogs.gotdotnet.com/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
"andrew queisser" <an*************@hp.com> wrote in message
news:3f********@usenet01.boi.hp.com...
The question, which I can't get my head around, is whether
directly specifying S and T as type parameters would work.
If that works it seems like inferring S and T from E and P is
a task for the C# compiler writers and not fundamentally
incompatible with the proposed generics support in .NET.

The example would then look like this:

public interface Enumerator<T> {
public T next();
}
public interface Predicate<T> {
public bool test(T val);
}
public class algos {
public static E find<E, P, S, T>(E enumerator, P predicate)
where E: Enumerator<S>
where P: Predicate<T>
{
S tmp = null
do
tmp = enumerator.next();
while (tmp != null && !predicate.text(tmp))
return enumerator;
}
}

Would this work or not?

Thanks,
Andrew
"Dietmar Kuehl" <di***********@yahoo.com> wrote in message
news:5b**************************@posting.google.c om...
Hi,

after reading the documentation on C# generics from several sources, I'm
still unclear whether it is possible to infer associated types from a
type argument. For example, if I have an enumerator or iterator for a
sequence passed to a generic function, is it possible to arrange things
such that the element type of the underlying sequence can be inferred?

Inferring associated types from type arguments becomes necessary as soon
as you are doing more than absolutely trivial stuff like the ubiquitous
stack example. A simple concrete example is a function which given an
enumerator and a predicate returns the enumerator when the predicate
yields true fo the element:

public interface Enumerator<T> {
public T next();
}
public interface Predicate<T> {
public bool test(T val);
}

public class algos {
public static E find<E, P>(E enumerator, P predicate)
where E: Enumerator<S>
where P: Predicate<T>
{
S tmp = null
do
tmp = enumerator.next();
while (tmp != null && !predicate.text(tmp))
return enumerator;
}
}

The essential portion in this example is that the type parameters 'S' and 'T' can be inferred from the type parameters 'E' and 'P': otherwise it is impossible to spell out the constraints for 'E' and 'P' to have a 'next()' and a 'test()' function, respectively. The type returned from 'next()' and the type expected by 'test()' are potentially different because 'T' can be a base class of 'S' to make the algorithm still applicable.

The above code uses a syntax I have made up because I haven't seen it in
this form anywhere (which doesn't mean that it isn't described somewhere): Essentially, the constraints are used for two purposes. On the one hand,
they spell out the expected interfaces of the two parameters. On the other hand, they are used to infer the types 'S' and 'T': 'S' is the element
type of the sequence iterated over by 'E' and 'T' is the type the
predicate operates on. Whether the notation I used or something else is
used I don't care at all. However, I do care about the possibility to
infer associated types.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>


Nov 15 '05 #5

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

Similar topics

11
by: andrew queisser | last post by:
I've read some material on the upcoming Generics for C#. I've seen two types of syntax used for constraints: - direct specification of the interface in the angle brackets - where clauses I...
17
by: Andreas Huber | last post by:
What follows is a discussion of my experience with .NET generics & the ..NET framework (as implemented in the Visual Studio 2005 Beta 1), which leads to questions as to why certain things are the...
4
by: Tom Jastrzebski | last post by:
Hello everybody, Here is the problem I came across experimenting with Generics. I would like to write a class or a struct adding integer to any other, initially undefined *numeric type*. So, my...
2
by: Marco Segurini | last post by:
Hi, The following code shows my problem: using System; using System.Collections.Generic; using System.Text; ....
23
by: Luc Vaillant | last post by:
I need to initialise a typed parameter depending of its type in a generic class. I have tried to use the C++ template form as follow, but it doesn't work. It seems to be a limitation of generics...
1
by: Peter Nofelt | last post by:
Hey All, I'm wondering if I'm able to have create generic user controls in .net 2.0 much like one can create generic classes. I would like to do this so that I can handle a set a types derived...
4
by: Cedric Rogers | last post by:
I wasn't sure if I could do this. I believe I am stretching the capability of what generics can do for me but here goes. I have a generic delegate defined as public delegate bool...
18
by: ttl_idiot | last post by:
This post is about how to get generic modules in a C environment. The aim is to get close to Ada generics. Using C++ is not an option. It should all run in a simple C compiler. When I think of a...
10
by: lpinho | last post by:
Hi all, I have a class (named for the example myObject) that can be of several types (int, string, float, etc), instead of using a object to define it's type I used a generic. public class...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.