开发者

c# generic constraint where is not class?

is there a where clause for a generic that determines that T is of type primitive?

 void Method<T>(T val) where T : primitive

case:

have a functional language written in C that feeds on primitive unmanaged blitable types, or things that can be pushed into a primitive eas开发者_如何学Pythonily (eg. a date without hours/mins/seconds could be pushed to an int, etc.) The original plan was to utilise GPU. Not doing that though. C# is holding up well in its co-ordinator role so far. I tend to think of the schema's home as living in C#. This isn't strictly true, but the idea serves the project well.

I like OO, but when it comes to functional ideas, I'd like to constrain those thoughts to types that are supported in that domain. Interestingly, I'm still leaning on C# to help me stay structured and disciplined. I don't see that changing.


There are other reasons why getting more detailed with constraints would be a good thing for me.

btw: resharper suggested explicit interfaces which I tried for a while. I really liked this notation... and the constraints can live with the interface too. Nice. However, I came across Jon Skeet's warning on S/O about this messing up inheritance. So, back to a more labour intensive run-time AssertIsXYZ.

To take that a little further, where constraints to me are a step towards proof of correctness (old ideas, but still good ones). The typing system seems to enable some of this to be pushed to the compiler. Using the word "where" made think about a clause or phrase (like in SQL/LINQ). I'm not asking for it to be taken to the nth degree. The more work the compiler can do the better as far as I'm concerned.

Getting tactile with constraints helped me clarify some ideas. Got to give credit there... but it's a pity that I had to comment the constraints out afterwards.


I suspect that what you want based on your comments on Jon's answer is a way to constrain a type parameter to either blittable types or unmanaged types.

An "unmanaged type" is a type whose definition precludes any reference to memory tracked by the garbage collector; you can only make pointer types out of unmanaged types. Blittable types are those types which can be marshalled from managed to unmanaged code without any modification to their bits; they are a subset of the unmanaged types.

A number of people have told us that it would be quite handy to have a generic constraint that constrains a type parameter to be only an unmanaged type. We have experimented with prototypes of C# and the CLR that have this constraint, but have no plans at this time to actually put the feature into the product. If you can describe the scenario that is motivating the feature request, that would help us prioritize the feature against the hundreds of other features that are also possible.


There's

where T : struct

That's not the same as it being a primitive type, mind you. It forces it to be a non-nullable value type. That would include, say, Guid (which isn't a primitive) but exclude Nullable<Guid> (which isn't a primitive either, but also isn't a class).

If you can be more precise about your requirements, we may be able to help you more.


No! There is no constraint for primitive type.

Your best bet in enforcing primitive type is as under:

void Method<T>(T val) where T:struct
{
    if (!typeof(T).IsPrimitive)
        throw new ArgumentException("Only primitive types are allowed.", "val");
}

OR

public void Method(int val)
{
    GenericMethod(val);
}

public void Method(double val)
{
    GenericMethod(val);
}

private void GenericMethod<T>(T val) where T:struct
{
}


I am just appending some new information to this thread, sorry for digging it up...

As many people have already answered, the following construct can be used for this reason for quite some time now:

void Method<T>(T val) where T : struct

With the release of C# 7.3, one will be able to write:

void Method<T>(T val) where T : unmanaged

which insures that you can take a pointer/address/size of T, meaning that the following is now perfectly valid:

unsafe static U BinaryConvert<T, U>(T input)
    where T : unmanaged
    where U : unmanaged => *(U*)&input;


float value = 42.80f;
int bits = BinaryConvert<float, int>(value);

This does technically help you in strictly constraining T to language primitives ... but it's a start, I guess.


Sources:

  • Proposal: https://github.com/dotnet/csharplang/blob/master/proposals/csharp-7.3/blittable.md
  • Issue: https://github.com/dotnet/csharplang/issues/187


You can't do this(at least currently). If you want it to be a value type:

void Method<T>(T val) where T : struct

or you can check it at run time to make sure it's really primitive(I think you want int/float/etc instead of all value types?)

public static class Ext
{
    public static bool IsPrimitive(this Type t)
    {
        return t == typeof(int) || t == typeof(float) 
            || t == typeof(double) || ...
    }
}

EDIT LOL just found that there is a built-in property in Type named IsPrimitive, that's what I'm going to say...


There is not, but you can use struct constraint.

where T: struct


A constraint has to be a non-sealed type, so there isn't a way to constrain a generic to a primitive type.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜