Generic param must implement "shift" operation

B

Bob Altman

Hi all,

I want to write a generic class that does this:

Public Class X (Of T)
Public Sub Method(param As T)
dim x as T = param >> 3
End Sub
End Class

So, I need to constrain T to types that implement the ">>" operator. Is
there a way to do this?

TIA - Bob
 
A

Armin Zingler

Bob Altman said:
Hi all,

I want to write a generic class that does this:

Public Class X (Of T)
Public Sub Method(param As T)
dim x as T = param >> 3
End Sub
End Class

So, I need to constrain T to types that implement the ">>" operator.
Is there a way to do this?


No, probably not. See topic "base numeric type?" three days ago.

What else do you intend to do with this type? If it was only shifting, you
wouldn't need this Sub.


Armin
 
S

Steven Cheng[MSFT]

Hi Bob,

Yes, if your generic class's certain method will call the parameter
object's particular operator, you need to make sure the passed parameter
objects support the certain operator. In VB.NET 9 (from VS 2005), VB.NET
has started supporting operator overload:

#Overloading Operators in VB.NET 2.0
http://www.developer.com/net/vb/article.php/10926_3512311_2

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.




--------------------
 
A

Armin Zingler

Steven Cheng said:
Hi Bob,

Yes, if your generic class's

The classes are not classes but value types called SByte, Byte, Short,
UShort, Integer, UInteger, Long, or ULong.

What he is looking for is something like

Public Sub Method(Of t As {SByte, Byte, Short, UShort, Integer, UInteger,
Long, ULong})()
End Sub

which obviously is not possible. So, only method overloading is left.
certain method will call the parameter
object's particular operator, you need to make sure the passed
parameter objects support the certain operator. In VB.NET 9 (from
VS 2005), VB.NET has started supporting operator overload:

#Overloading Operators in VB.NET 2.0
http://www.developer.com/net/vb/article.php/10926_3512311_2


The >> operator is a VB.Net native operator that can only be applied to the
types mentioned above. Therefore the question in my previous post what else
has to be done to the value passed.


Armin
 
B

Bob Altman

Yes, Armin has it exactly correct. I want to be able to perform a numeric
operation (in this case, a shift operation, but it could be as simple as
addition) on a generic argument. This means that I need to constrain the
argument to types that implement the numeric operation. As Armin points
out, the built-in types that implement a shift function are limited to the
numeric value types.

FWIW, here's the longer story of what I'm trying to accomplish. The BCL
contains a class that manages an array of bits (Collections.BitArray). This
class is significant overkill for the simple bit twiddling that I want to
perform. So I wrote a structure (a value-type) that implements simple bit
set and clear operations on an underlying UInt. Since it's a value type its
data is allocated on the stack, so it's a lot cheaper to use that BitArray.

So... my Bit32 structure accepts a 32-bit integer as a constructor argument,
lets you access individual bits, and lets you set or retrieve the entire
32-bit value. That got me to thinking that I really don't want to copy and
paste the code to a 64-bit version, which differs from the 32-bit version
only by the type of the underlying data value. But I can't come up with a
way to code a generic Bits(Of T) structure because I need to perform And,
Or, and Shift operations on a type T.

- Bob
 
S

Steven Cheng[MSFT]

Thanks for Armin's further input.

Hi Bob,

Yes, now I got your actual concern, and your generic types are limited in a
certain colletion of numeric types. If so, I agree with Armin that method
overloading is the reasonable approach here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.


--------------------
 
B

Bob Altman

Hi Steven,

I don't understand. If you go back to my original question (slightly
modified to make it a little more complete):

Public Structure Bits(Of T)
Private _value As T

Public Sub New(value as T)
_value = value
End Sub

Public Function GetBit(bitNumber as Int32) As Boolean
Return CBool((_value >> bitNumber) And 1)
End Function
End Class

How do I use "method overloading" to get the the >> and And functions in
GetBit to compile?

- Bob
 
A

Armin Zingler

Bob Altman said:
Hi Steven,

I don't understand. If you go back to my original question
(slightly modified to make it a little more complete):

Public Structure Bits(Of T)
Private _value As T

Public Sub New(value as T)
_value = value
End Sub

Public Function GetBit(bitNumber as Int32) As Boolean
Return CBool((_value >> bitNumber) And 1)
End Function
End Class

How do I use "method overloading" to get the the >> and And
functions in GetBit to compile?

Overloading wasn't meant in combination with generics.

The overloaded versions of this (impossible) method

Public Sub Method(Of t As {SByte, Byte, Short, UShort, Integer, UInteger,
Long, ULong})()
End Sub

are

Public Sub Method(Value as SByte)
End Sub

Public Sub Method(Value as Byte)
End Sub

...


Armin
 
A

Armin Zingler

Bob Altman said:
Yes, Armin has it exactly correct. I want to be able to perform a
numeric operation (in this case, a shift operation, but it could be
as simple as addition) on a generic argument. This means that I
need to constrain the argument to types that implement the numeric
operation. As Armin points out, the built-in types that implement a
shift function are limited to the numeric value types.

FWIW, here's the longer story of what I'm trying to accomplish. The
BCL contains a class that manages an array of bits
(Collections.BitArray). This class is significant overkill for the
simple bit twiddling that I want to perform. So I wrote a structure
(a value-type) that implements simple bit set and clear operations
on an underlying UInt. Since it's a value type its data is
allocated on the stack, so it's a lot cheaper to use that BitArray.

cheaper than?
So... my Bit32 structure accepts a 32-bit integer as a constructor
argument, lets you access individual bits, and lets you set or
retrieve the entire 32-bit value. That got me to thinking that I
really don't want to copy and paste the code to a 64-bit version,
which differs from the 32-bit version only by the type of the
underlying data value. But I can't come up with a way to code a
generic Bits(Of T) structure because I need to perform And, Or, and
Shift operations on a type T.

Well,, it's not possible because VB needs specific types to do this. These
are basic operations on the native data types that are compiled inline and
therefore the compiler exactly need to know what it is dealing with. It
needs to know the size, the storage format of the value. For example,
performing an And operation on Integer values must create different code and
only access 32 bits. Without the compiler knowing the type it can not know
which code to generate.


Armin
 
B

Bob Altman

Since it's a value type its data is
cheaper than?

A Structure behaves in many ways like a Class. It encapsulates data and
code, and it can expose public data, methods, and properties. However, data
for a Structure is allocated on the stack. If you allocate a local
Structure variable then its data resides on the stack until it goes out of
scope, at which time its stack space is simply reclamed. On the other hand,
a class is allocated from the managed heap. When its life is over then the
GC needs to run to clean it up.

In my case, I have a routine that executes frequently. That routine wants
to manipulate bits in an Integer. I could do this:

Sub ExecuteThis10TimesPerSecond()
Dim myBits As New BitArray(m_value)
...
End Sub

The problem with this is that every time my routine runs I create another
instance of the BitArray class which needs to be returned to the heap by the
garbage collector. But by writing my own "lightweight" bit manipulation
Structure rather than a Class then the above code no longer allocates memory
from the managed heap.
Well,, it's not possible because VB needs specific types to do this. These
are basic operations on the native data types that are compiled inline and
therefore the compiler exactly need to know what it is dealing with. It
needs to know the size, the storage format of the value. For example,
performing an And operation on Integer values must create different code
and only access 32 bits. Without the compiler knowing the type it can not
know which code to generate.

I actually knew that from the start. My real reason for posting the
question was to see if anyone had some brilliant (or sneaky) way around the
fact that generics don't allow you to constrain their type to either numeric
value types or to types that implement a particular operator. The answer
will probably be "no, you can't get there from here", but it doesn't hurt to
ask.
 
A

Armin Zingler

Bob Altman said:
cheaper than?

A Structure behaves in many ways like a Class. [...]

Nothing new so far. :)
[...] But by writing my own "lightweight" bit manipulation Structure
rather than a Class then the above code no longer allocates memory from
the managed heap.

Then I think you consider your structure being "cheaper than" the BitArray.
You wrote "it's a lot cheaper to use that BitArray." Therefore my question
"cheaper than?".

[...] The answer will probably be "no, you can't get
there from here", but it doesn't hurt to ask.

No, it doesn't. Sometimes the answer hurts. ;)


Armin
 
S

Steven Cheng[MSFT]

Thanks for Armin's clarify.

Hi Bob,

Yes, as Armin explained, method overload means create multiple version of
the same function which accept different type of the same parameter. Method
overload no longer involve generic. For your scenario here, generic doesn't
quite work.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.


--------------------
From: "Armin Zingler" <[email protected]>
References: <[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top