Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# generic constraint where is not class?

Tags:

c#

generics

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 easily (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.

like image 434
sgtz Avatar asked Sep 26 '11 06:09

sgtz


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


2 Answers

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.

like image 71
Eric Lippert Avatar answered Sep 21 '22 20:09

Eric Lippert


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.

like image 30
Jon Skeet Avatar answered Sep 20 '22 20:09

Jon Skeet