473,748 Members | 2,617 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Specialization with generics

I started writing some code with the 2.0 beta compiler, and found out
quickly that specialization is not supported with generics.

For example, I want to do something like this:

class number ...
class integer : number ...
class real : number ...
class multiplier<_num ber> where _number : number ...
class multiplier<_num ber> where _number : integer ...

Here, the first implementation of 'multiplier' will work with 'number'
or anything that derives from 'number'. The second implementation of
'multiplier' will work with 'integer' or anything that derives from
'integer. Since 'integer' derives from 'number', it is a
specialization. At least that's what I'm calling it - there may be a
more appropriate name in this exact context.

This may be preaching to the choir - I haven't been in this newsgroup
for a while, but here is an example of where it would be useful to have
the above feature:

class fancy_computati on<_number> where _number : number
{
public _number doit(_number x,_number y)
{
multiplier<_num ber> mult=new multiplier<_num ber>;
return mult.doit(x,y);
}
}

The advantage here is that I can type 'fancy_computat ion<integer>' and
the compiler will automatically select the correct implementation of
'multiplier'.

Anyway, it look like this is not supported. I am wondering what
specific limitation caused the design team to leave out support for
specialization.

Thanks for any insight you can provide.

Mike
Nov 16 '05 #1
4 9205
No, user-defined specialization is not supported by generics. Apparrently
the designers felt that user-defined specialization was too tied to C++.
Since most other differences between templates and generics are emergent
from the different instantiation times (compile-time for templates, run-time
for generics) I wouldn't be too surprised if this had an impact on
user-defined specialization as well.

"Michael Sparks" <mi**@remove.th is.sparks.name> wrote in message
news:2Z******** ************@co mcast.com...
I started writing some code with the 2.0 beta compiler, and found out
quickly that specialization is not supported with generics.

For example, I want to do something like this:

class number ...
class integer : number ...
class real : number ...
class multiplier<_num ber> where _number : number ...
class multiplier<_num ber> where _number : integer ...

Here, the first implementation of 'multiplier' will work with 'number' or
anything that derives from 'number'. The second implementation of
'multiplier' will work with 'integer' or anything that derives from
'integer. Since 'integer' derives from 'number', it is a specialization.
At least that's what I'm calling it - there may be a more appropriate name
in this exact context.

This may be preaching to the choir - I haven't been in this newsgroup for
a while, but here is an example of where it would be useful to have the
above feature:

class fancy_computati on<_number> where _number : number
{
public _number doit(_number x,_number y)
{
multiplier<_num ber> mult=new multiplier<_num ber>;
return mult.doit(x,y);
}
}

The advantage here is that I can type 'fancy_computat ion<integer>' and the
compiler will automatically select the correct implementation of
'multiplier'.

Anyway, it look like this is not supported. I am wondering what specific
limitation caused the design team to leave out support for specialization.

Thanks for any insight you can provide.

Mike

Nov 16 '05 #2
Michael Sparks wrote:
The advantage here is that I can type 'fancy_computat ion<integer>' and
the compiler will automatically select the correct implementation of
'multiplier'.
you can do something like that with runtime-dispatch using if ( x
instanceof ) ..., that won't give you qualified-type (static) dispatch
though -- but that probably isn't what you want anyway.
Anyway, it look like this is not supported. I am wondering what
specific limitation caused the design team to leave out support for
specialization.


I can think of at least one thing that's always been a c++ problem:
specialization allows distribution of the definition of the specialized
set of classes, giving different semantics to statements depending on
how much of the specialization you've seen.

In C# you don't include files (headers) but access types in libraries,
so the good old "specializa tion in separate header-file changes
behaviour of program" cannot happen, but specialization in separate
type-libraries .... the horror!

X.lib
class foo<_number>: where _number: number { static number doit(number,
number); }

Y.lib
class foo<_number>: where _number: int { static int doit(int, int); }

--
Helge
Nov 16 '05 #3
>> Anyway, it look like this is not supported. I am wondering what
specific limitation caused the design team to leave out support for
specialization.


I can think of at least one thing that's always been a c++ problem:
specialization allows distribution of the definition of the specialized
set of classes, giving different semantics to statements depending on
how much of the specialization you've seen.


That possibility occurred to me, but then I remembered partial classes,
which are also new with this beta. That allows you do define different
parts of the same class in multiple files, and the compiler magically
figures out how to piece them together. The same could be done here,
but instead of piecing together a single class, you are piecing together
the collection of specializations for a class.

It is limited in that a user of your library couldn't create his own
specialization, but that could probably lead to security trouble anyway,
so it would be an acceptable loss (to me at least).

Mike
Nov 16 '05 #4
Michael Sparks wrote:

Can't you make it work with a simple switch (if...else...) on the types?
If not, then the (hack :) visitor-pattern can help you.
That possibility occurred to me, but then I remembered partial classes,
which are also new with this beta. That allows you do define different
parts of the same class in multiple files, and the compiler magically
figures out how to piece them together. The same could be done here,
but instead of piecing together a single class, you are piecing together
the collection of specializations for a class.
Are you allowed to specify different parts of the same class in separate
type-libraries? That would surprise me, but then again, I have
absolutely zero .NET2.0 experience.
It is limited in that a user of your library couldn't create his own
specialization, but that could probably lead to security trouble anyway,
so it would be an acceptable loss (to me at least).


You are proposing a limitation: that all specializations must be in the
same type-library, right?

Well that may work..., but it would take quite a bit of usability out of
it, especially since all the types specialized for would have to be
known inside that type-library.

Restrictions like "but it would work just fine for my exact case" tends
to suggest that it should not be a language feature.

BTW: in your case, wouldn't you *really* rather want dynamic-type
dispatch? so the "right" operation gets invoked even if an integer
argument is only qualified (static-typed) as number?

Don't get me wrong, I can see how it is handy (having done a fair amount
of c++), but it also brings its own problems, you are putting semantics
of the code into the type and dispatch based on that, effectively
creating a "meta-language" using the dispatch mechanism.

Before you known it you will be wishing for the complexity of the C++
template-resolution (are/have you by any chance (been) a c++ programmer?).

I think this comes down to "meta-programming", making a program that wil
make the program you want, in contrast to expressing the program directly.

C# is (virtually) impossible to "meta-program" in (you only have
compile-time dispatch by overloading), so you probably shouldn't try to
do it.

In C++, you can do meta-programming (using templates, mainly because of
typedefs and specialization) , but it sucks, boost helps - but it is
still VERY complicated and if there is a reasonable other choice, I
don't do it.

In scheme, meta-prgramming is a breeze, with the "`" quasiquote
operator, and in scheme you find yourself meta-program when appropriate.

--
Helge
Nov 16 '05 #5

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

Similar topics

17
6861
by: Paul MG | last post by:
Hi Template partial specialization always seems like a fairly straightforward concept - until I try to do it :). I am trying to implement the input sequence type (from Stroustrup section 18.3.1, 'Iseq'). I want the version for containers that he gives, but also to provide a specialization for construction from a pair<It,It> (eg because that is returned by equal_range()).
8
7688
by: Agent Mulder | last post by:
Hi group, I have a problem with partial template specialization. In the code below I have a template struct Music with one method, play(), and three kinds of music, Jazz, Funk and Bach. When I specialize Music<Bach>, I expect that the original play() method is available in the specialization, but it is not. How can I fix this? -X
19
2975
by: Nicolas Fleury | last post by:
Hi everyone, I would to know what do you think of this PEP. Any comment welcomed (even about English mistakes). PEP: XXX Title: Specialization Syntax Version: $Revision: 1.10 $ Last-Modified: $Date: 2003/09/22 04:51:49 $ Author: Nicolas Fleury <nidoizo at gmail.com> Status: Draft Type: Standards Track
4
2025
by: anonymous.user0 | last post by:
Using the dotnet v1.1 framework (so no generics possible). I'd like to create a bunch of Factory classes that all inherit from a single abstract base Factory class. Each Factory is responsible for the creation of objects of a type that inherit from a base class. Factory is probably the wrong word here, as it's really a persistance class, but the problem is the same. Example: abstract BaseClass abstract BaseFactory, uses BaseClasses...
6
2136
by: Adam Badura | last post by:
Is there a way to specialize generic types or methods tha same way as it is possible in C++ for example? I know that instead of specializing a class class MyCollection<TType> for string type I could do something like class MyStringCollection : MyCollection<string>
2
7698
by: Barry | last post by:
The following code compiles with VC8 but fails to compiles with Comeau online, I locate the standard here: An explicit specialization of any of the following:
1
984
by: Duncan Smith | last post by:
I know that template specializtion is not included in generics, but was wondering whether or not there is an elegant alternative? The first function is always called, but I was hoping that for instances of type RealPoint (ComparePointByFiaNumber<RealPoint)>) the second function would be called. To implement different behavious based upon the type, do I have to have a switch within Equals() that will check the type of T and then call...
8
2966
by: flopbucket | last post by:
Hi, I want to provide a specialization of a class for any type T that is a std::map. template<typename T> class Foo { // ... };
6
2614
by: abir | last post by:
i have a template as shown template<typename Sclass Indexer{}; i want to have a specialization for std::vector both const & non const version. template<typename T,typename Aclass Indexer<std::vector<T,A {} matches only with nonconst version. anyway to match it for both? and get if it is const or nonconst? Actually i want 2 specialization, one for std::vector<T,Aconst & non const other for std::deque<T,Aconst & non const.
0
8832
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9386
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9333
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8255
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6799
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3319
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.