Common default return value of functions with generics
See original GitHub issueI am writing unit tests for little tasks that are talking to the database. My test cases also include scenarios where a given object is not found in the database, and therefore I’d like to emulate that the queries return null
.
Now, I managed to achieve this by using the following calls:
var _session = // database session
A.CallTo(_session).WithReturnType<Foo>().Returns(null);
A.CallTo(_session).WithReturnType<Bar>().Returns(null);
// ~10 more of these with just different types defined
however I was wondering if there is a way of setting this up as the default behaviour - so that all calls to _session
with a generic parameter will return null
by default?
I don’t know if it is possible in the first place, but it would probably be a nice feature.
Issue Analytics
- State:
- Created 7 years ago
- Comments:27 (26 by maintainers)
Top Results From Across the Web
Return default value for generic type
3 Answers. You can't return nil for any type. If int is used as the type argument for T for example, returning nil...
Read more >28.1.6.1. Introduction to Generic Functions
Generic functions are true functions that can be passed as arguments, returned as values, used as the first argument to funcall and apply,...
Read more >How To Use Generics in TypeScript
In this code, you are creating a function called someFunction , which returns true . You are then using the typeof operator to...
Read more >Documentation - Generics
The identity function is a function that will return back whatever is passed in. ... You can read the type of loggingIdentity as...
Read more >Generics - The Move Book
Generics are commonly used in library code, such as in vector, to declare code ... function that takes a value of any type...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yes, I’ll reopen and submit a PR with the analyzer changes.
To expand, the way I thought @thomaslevesque’s suggestion would work would be
(or swap
Where
andWithNonVoidReturnType
, as desired, like we can now withWhere
andWithReturnType<>
)