Questions tagged [generic-function]
81 questions
2
votes
2 answers
Casting List<[KnownType]> to List to match function's return type?
I am trying to make a function that generically retrieves data from my MongoDB collections. To do this I have constructed a generic method that returns a List.
My issue is that I have to create this List to return, but I do so based on the…

KDecker
- 6,928
- 8
- 40
- 81
2
votes
1 answer
R: Create custom output from list object
I have a list that stores different data types and objects:
header <- "This is a header."
a <- 10
b <- 20
c <- 30
w <- 1:10
x <- 21:30
y <- 51:60
z <- 0:9
mylist <- list(header = header,
const = list(a = a, b = b, c = c),
…

ttlngr
- 43
- 7
1
vote
1 answer
Negative effects of using a generic function like this?
My question here is, what are the negative effects of using a generic function such as this? Calling this function does work, and in a test console module, it compiles perfectly fine. I do know this is not a strongly typed function, and is %100 bad…

wakurth
- 1,644
- 1
- 23
- 39
1
vote
1 answer
TypeScript how associate enam with union types and get returned object property type?
I have an enum and its associated union type:
type User = { name: string, age: number }
export enum StorageTypeNames {
User = "user",
Users = "referenceInfo",
IsVisibleSearchPanel = "searchPanel",
PropertyPanelInfo =…

AshBadCoder
- 47
- 5
1
vote
1 answer
Kotlin class generic type infered as nullable
A generic class for holding network request result
sealed class Result {
data class Success(val data: T) : Result()
data class Error(val message: String, val exception: Exception? = null) :…

yaugenka
- 2,602
- 2
- 22
- 41
1
vote
4 answers
Kotlin generic function return type does not conform to declared nullability
fun function(input: T): T = input
fun function2() {
val n = function(1)!!
}
Since T is declared as nullable, I expected the output to be nullable as well, but Lint produces Unnecessary non-null assertion (!!) on a non-null receiver of…

yaugenka
- 2,602
- 2
- 22
- 41
1
vote
1 answer
Is there any compile-time (or sth similar) function in C that will give me format specifier for any primitive type?
I seek format_spec function. Context described below.
Is there any compile-time function like that ?
#include
#define typeof(x) __typeof(x)
int main(){
int x; /* Plain old int variable. */
typeof(x) y=8; /* Same type as x.…

Sir
- 337
- 1
- 7
1
vote
4 answers
How to pass a non-nullable type to a function with a nullable parameter?
A nullable type can be checked and cast to a non-nullable type, but the other way around is quite difficult.
For example:
str1: String? = ""
// if str1 not null
str2: String = str1
str3: String? = str2 // This works!
In my case I'm talking about a…

sd_master92
- 13
- 4
1
vote
1 answer
Generic function parameter restricted to a property of T
I have a uniqBy function defined thus:
export function uniqBy(a: T[], key: any): T[] {
const seen = {};
return a.filter(function (item) {
if (item) {
const k = key(item);
return seen.hasOwnProperty(k) ?…

BlackICE
- 8,816
- 3
- 53
- 91
1
vote
0 answers
Abstract Generic Function Implementation
I am having trouble with an abstract generic method.
Abstract Method Definition
protected abstract T InitEntity(ref NpgsqlDataReader reader) where T : BaseEntity, new();
Implementation
protected override EUser InitEntity(ref…

Vintle
- 21
- 3
1
vote
1 answer
Create generic extension to make a deep copy of a List with Data Class
Hi I'm trying to create a generic function to make a deep copy of list using the copy() method that a Data class provide us.
I'm using the method that I found on other post which returns a list of objects being deep copied:
listDataClass.map{…

Barrufet
- 495
- 1
- 11
- 33
1
vote
2 answers
Typed lambda functions in Common Lisp
I don't know of any practical uses for this, it just came to my mind whether there is any thing comparable to defmethod to defun for lambda? Something like this
(defmacro tlambda (args &body body)
(let* ((gf-name (subseq (write-to-string (gensym))…

Student
- 708
- 4
- 11
1
vote
3 answers
Question about C# Pattern in ASP.NET Core When Adding Services in Startup.cs
With regards to this code to configure services:
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton();
services.AddControllers();
}
and specifically the line:
services.AddSingleton();
would…

triple.vee
- 126
- 1
- 7
1
vote
1 answer
Kotlin generic function for Float and Double
The following function compiles, but can only used with Doubles:
fun triang(x: Double): Double {
var m = x - truncate(x)
if (m < 0) m += 1.0
return when {
m < 0.25 -> 4.0 * m
m < 0.50 -> 2.0 - 4.0 * m
m < 0.75 ->…

digory doo
- 1,978
- 2
- 23
- 37
1
vote
2 answers
Execute code after `UseMethod()` in generic function in R?
I would like to have the following generic function, which
checks for thew allowedFormats (This works),
than executes the generic function base on the type of argument x (works)
evaluates the statements after the call of UseMethod() (does not…

Rainer
- 8,347
- 1
- 23
- 28