generic-programming

Get type of a “singleton type”

橙三吉。 提交于 2020-08-22 05:21:30
问题 We can create a literal types via shapeless: import shapeless.syntax.singleton._ var x = 42.narrow // x: Int(42) = 42 But how can I operate with Int(42) as a type if it's even impossible to create type alias type Answ = Int(42) // won't compile // or def doSmth(value: Int(42)) = ... // won't compile 回答1: 1) In Typelevel Scala you can write just val x: 42 = 42 type Answ = 42 def doSmth(value: 42) = ??? 2) In Dotty Scala you can write the same. 3) In Lightbend Scala (i.e. standard Scala) +

How to do if-else for some specific generic type?

我只是一个虾纸丫 提交于 2020-07-23 06:18:09
问题 I have MyClass and I want to call both mySubMethod(key) and mySubMethod2(key) for integer, but only call mySubMethod2(key) for non-integer. I tried below codes but doesn't work. I also tried to change K == int to something else, e.g. K == Integer , K.equals(int) and K.equals(Integer) etc., but all of them don't work. How Can I fix it? Thanks. public class MyClass<K,V> { public boolean myMethod(K key) { if (K == int) { mySubMethod(key); }else { // do nothing } mySubMethod2(key); return false;

How to do if-else for some specific generic type?

余生长醉 提交于 2020-07-23 06:17:41
问题 I have MyClass and I want to call both mySubMethod(key) and mySubMethod2(key) for integer, but only call mySubMethod2(key) for non-integer. I tried below codes but doesn't work. I also tried to change K == int to something else, e.g. K == Integer , K.equals(int) and K.equals(Integer) etc., but all of them don't work. How Can I fix it? Thanks. public class MyClass<K,V> { public boolean myMethod(K key) { if (K == int) { mySubMethod(key); }else { // do nothing } mySubMethod2(key); return false;

How to do if-else for some specific generic type?

孤者浪人 提交于 2020-07-23 06:16:25
问题 I have MyClass and I want to call both mySubMethod(key) and mySubMethod2(key) for integer, but only call mySubMethod2(key) for non-integer. I tried below codes but doesn't work. I also tried to change K == int to something else, e.g. K == Integer , K.equals(int) and K.equals(Integer) etc., but all of them don't work. How Can I fix it? Thanks. public class MyClass<K,V> { public boolean myMethod(K key) { if (K == int) { mySubMethod(key); }else { // do nothing } mySubMethod2(key); return false;

How to make Parameters of VB.NET function as Generic type?

≯℡__Kan透↙ 提交于 2020-07-06 11:53:17
问题 I have a VB.NET function as below, the parameter 'x' that is passed to the function is of Type 'Single'. However, I want to write the function so that it can accept any numeric type such as 'Single', 'Double' and 'Integer'. I know one way of doing that is to write 3 functions with the same names, but it would be so tedious. Can anyone suggest any idea? Thank you. Public Function Square(x As Single) As Single Return x * x End Function 回答1: try following method Public Function Square(Of T)

Fortran generic functions based on the return kind

房东的猫 提交于 2020-06-09 04:33:32
问题 I am trying to create a generic function in Fortran based on the value to be returned, that is, depending on if the output of the function is to be assigned to a single precision real or to a double precision real. The code is: MODULE kk_M USE ISO_FORTRAN_ENV IMPLICIT NONE INTEGER, PARAMETER :: sp = REAL32 INTEGER, PARAMETER :: dp = REAL64 INTERFACE use_func MODULE PROCEDURE use_sp_func MODULE PROCEDURE use_dp_func END INTERFACE use_func INTERFACE use_sub MODULE PROCEDURE use_sp_sub MODULE

Fortran generic functions based on the return kind

馋奶兔 提交于 2020-06-09 04:33:03
问题 I am trying to create a generic function in Fortran based on the value to be returned, that is, depending on if the output of the function is to be assigned to a single precision real or to a double precision real. The code is: MODULE kk_M USE ISO_FORTRAN_ENV IMPLICIT NONE INTEGER, PARAMETER :: sp = REAL32 INTEGER, PARAMETER :: dp = REAL64 INTERFACE use_func MODULE PROCEDURE use_sp_func MODULE PROCEDURE use_dp_func END INTERFACE use_func INTERFACE use_sub MODULE PROCEDURE use_sp_sub MODULE

Is it possible to define _Generic's association-list dynamically?

烂漫一生 提交于 2020-05-13 18:48:25
问题 I have a template like this: template.h ---------- // Declare a function "func_type()" void JOIN(func_, T)(T t) { return; } #undef T which I use like this in order to generate the same function for different types: example.c --------- #define T int #include "template.h" #define T float #include "template.h" I would like to have a single func that I can use instead of funct_int , func_float , etc. My problem with _Generic is that it doesn't seem possible to define the association-list

Is it possible to define _Generic's association-list dynamically?

被刻印的时光 ゝ 提交于 2020-05-13 18:48:23
问题 I have a template like this: template.h ---------- // Declare a function "func_type()" void JOIN(func_, T)(T t) { return; } #undef T which I use like this in order to generate the same function for different types: example.c --------- #define T int #include "template.h" #define T float #include "template.h" I would like to have a single func that I can use instead of funct_int , func_float , etc. My problem with _Generic is that it doesn't seem possible to define the association-list