question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Drop generic type constraint Query<T> to be T : class, and allow value types

See original GitHub issue

As of now https://github.com/elastic/elasticsearch-net/blob/master/src/Nest/QueryDsl/Query.cs is next

public static class Query<T> where T : class

Could you drop class? I.e.:

public static class Query<T> 

So I could write for stucts (i.e. value types)? Any reasons it is impossible?

Use case: During usage of ElasticSearch as NoSQL storage for audit we do client side aggregation as elastic does not support summaries (sum,count) during usage of top_hits aggregation. Given different tactics, one of these like take first of all and take last for that day, leads to store millions entities in memory so it could be good to have these to be structs. Another issues that trying to build LINQ provider for our case is impossible as LINQ provider methods has no class constraint.

If class consideration was for inheritance and proxy generation in future, do not do it.

Follow VITA ORM or other delegation pattern coding/code generation - use interfaces.

Why not build LINQ to low level client? I like typing and query validation done by NEST, otherwise why I use NEST at all for our usage case.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:5
  • Comments:14 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
codebraincommented, Sep 2, 2019

Discussed and will consider making the change to drop the “where T : class” for user-defined document types.

1reaction
Mpdreamzcommented, Apr 26, 2017

The main reason why T is constrained to class is for all the inference we do based on types and down the line in expressions inside e.g Field(). It also signals that you can’t do Search<string> since _source is always an object.

C# needs a struct constraint badly 😦

I’m not opposed to dropping the constraint but would like to explore the ramifications in terms of API misuse.

I agree with @KodrAus that struct on its own won’t have that much effect on GC (and his points on IQueryable) but NEST should not be in the way of others attempting this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - How to restrict T to value types using a constraint?
It's not possible to constrain a generic parameter to a specific value type. You can however force it to be a value type...
Read more >
where (generic type constraint) - C# Reference
Constraints can specify interfaces, base classes, or require a generic type to be a reference, value, or unmanaged type.
Read more >
Generics - Documentation - Swift.org
Generic code enables you to write flexible, reusable functions and types that can work with any type, subject to requirements that you define....
Read more >
C# Generic Constraints
C# Generic Constraints. C# allows you to use constraints to restrict client code to specify certain types while instantiating generic types.
Read more >
Typescript Generics Explained - Ross Bulat - Medium
Generics can be applied to functions, interfaces and classes in Typescript. ... class, T is a type variable for programming language meta data,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found