Questions tagged [generic-function]
81 questions
7
votes
3 answers
Typescript - How do I narrow type possibilities of a generic type in a switch statement?
I'm trying to write a function that will perform a particular calculation based on the passed key and parameters. I also want to enforce a relationship between the passed key and parameters, so I have used a generic function with a…

azwier
- 163
- 6
7
votes
1 answer
How to predict random and fixed effect models?
I just recently made a change from STATA to R and have some troubles implementing the R equivalent of the STATA commands xtlogit,fe or reand predict. May I ask for some assistance to adjust the following scenario:
data <-…

Googme
- 914
- 7
- 27
6
votes
1 answer
Is there any generic Hashable typeclass in Haskell? (a.k.a. "deriving (Hashable)")
Has anyone written a generic function so that hash functions can be generated automatically for custom data types (using the deriving mechanism)? A few times, I've written the following kind of boilerplate,
data LeafExpr = Var Name | Star deriving…

gatoatigrado
- 16,580
- 18
- 81
- 143
6
votes
1 answer
How to use a "taken" word as a CLOS generic
Generics seem to offer a nice facility for pulling out a common word and letting it act on things according to the types you pass it, with extensibility after-the-fact.
But what about common words that are already taken and aren't defined as…

HostileFork says dont trust SE
- 32,904
- 11
- 98
- 167
6
votes
5 answers
Trying to learn: Object Reorientation, and generic functions in LISP!
im reading Practical common Lisp as a result of another question.
I just read chapter 16 and 17 where you can find how LISP manages objects. But after a couple of years of thinking how Java manages objects, i cant really seem to understand how you…

DFectuoso
- 4,877
- 13
- 39
- 55
6
votes
1 answer
Specialising on Vectors and Matrices
I am using common-lisp for my real-time graphics experiments and so far it has being great. My requirements for speed and easy compatibility with cffi mean I am using 'typed' arrays. The one area of the code which really feels ugly is the generic…

Baggers
- 3,183
- 20
- 31
6
votes
1 answer
Is 'show' a normal S4 generic function?
I'm trying to create a method for my class, which inherits from data.frame. I was originally hoping just to inherit the 'show' method from data.frame as well, but I'm also fine with writing my own. I defined my class and 'show' method as…

Eli Sander
- 1,228
- 1
- 13
- 29
5
votes
1 answer
How to implement specialized versions of a generic function?
I'd like to have multiple versions of a function optimized for type of its arguments, and Rust call appropriate one depending on context.
In my case all arguments have the same type, and all are equivalent, so it'd rather avoid having a self…

Kornel
- 97,764
- 37
- 219
- 309
4
votes
2 answers
What is the parameter type for a generic function parameter when called from a base class with 'this'?
Confusing question, I know. Given the following:
class Test
{
public static void GenericFunc(T SomeType)
{
System.Console.WriteLine("typeof(T): " + typeof(T).Name);
…

Bill Brooks
- 751
- 1
- 10
- 30
4
votes
1 answer
Common Lisp: extract methods from generic function
Is there a way to extract a list of methods from a generic function in Common Lisp?
For example:
(defmethod say ((self string)) ; method-0
(format t "Got string: ~a~%" self))
(defmethod say ((self integer)) ; method-1
(format t "Got integer:…

AlexDarkVoid
- 485
- 3
- 12
4
votes
2 answers
Replacing an ordinary function with a generic function
I'd like to use names such as elt, nth and mapcar with a new data structure that I am prototyping, but these names designate ordinary functions and so, I think, would need to be redefined as generic functions.
Presumably it's bad form to redefine…

Rob Blackwell
- 55
- 5
3
votes
1 answer
R - problems dispatching to new method print.list()
In my summarytools package, I have successfully defined the print.by method. However, even though I've followed the exact same steps for print.list, the dispatch fails.
The method seems to have registered, along with the two other print methods…

Dominic Comtois
- 10,230
- 1
- 39
- 61
3
votes
0 answers
R - Redefining utils::View as generic without conflicting with RStudio
I have successfully redefined utils::View as a generic function so that I can use it my package. However, it so happens that RStudio also defines some kind of hook on this function.
Before loading my package, I see:
> View
function (...)…

Dominic Comtois
- 10,230
- 1
- 39
- 61
3
votes
2 answers
How can I dispatch Python 2 functions based on the data type passed to the function?
I would like to dispatch Python functions dependent (e.g. using a dict approach) on the data type of the argument passed to the "dispatching" function (e.g. using isinstance()). Are there implementation alternatives? What's the easiest approach?

thinwybk
- 4,193
- 2
- 40
- 76
3
votes
1 answer
How to call swift generic function from Objective-C
Please read the question.
I know that we can't call Swift generic methods from Objective-C as per this link
But in my case I have a project written in Objective-C and Swift. There is network library written in Swift having generic GET, POST, DELETE…

Gagan_iOS
- 3,638
- 3
- 32
- 51