variadic-functions

Scala: Constructor taking either Seq or varargs

社会主义新天地 提交于 2019-11-30 11:05:37
I am guessing that, for compatibility reasons, the type of vararg parameters Any* is Array[Any] - please correct this if I'm wrong. However, this does not explain the following error: class Api(api_url: String, params: Seq[(String, String)]) { def this(api_url: String, params: (String, String)*) = this(api_url, params.seq) } This code does not compile, but gives the warning: double definition: constructor Api:(api_url: String, params: (String, String)*)Api and constructor Api:(api_url: String, params: Seq[(String, String)])Api at line 13 have same type after erasure: (api_url: java.lang.String

Type trait to obtain default argument promotions

隐身守侯 提交于 2019-11-30 09:37:35
[Disclaimer: I know an answer to this question. I thought it might be of some general interest.] Question: How can we have a type trait that produces the type that results from performing default argument promotions ? Motivation: I would like to be able to use variable arguments portably. For example: void foo(char const * fmt, ...); // Please pass: * unsigned short // * bool // * char32_t // * unsigned char When passing arguments to a function call without parameters, i.e. matching the ellipsis, the arguments undergo default argument promotion. So far so good, but those promotions are

How to write a variadic method which replaces chained method calls?

拟墨画扇 提交于 2019-11-30 09:00:21
问题 I am working on a recursive map class called group_by which models the SQL namesake. For example, gb is a group_by object which will store pointers to foo grouped by std::string , int , and char key types, in that order. group_by<foo,std::string,int,char> gb; group_by provides an at( I const& key ) accessor method which can be used to look inside current level map. Chaining at() calls to retrieve deeper maps works fine. auto& v = gb.at( k1 ).at( k2 ).at( k3 ).get_vec(); PROBLEM I would like

How to use varargs in conjunction with function pointers in C on Win64?

久未见 提交于 2019-11-30 08:37:35
问题 Consider the following C program: #include <stdio.h> #include <stdarg.h> typedef void (callptr)(); static void fixed(void *something, double val) { printf("%f\n", val); } static void dynamic(void *something, ...) { va_list args; va_start(args, something); double arg = va_arg(args, double); printf("%f\n", arg); } int main() { double x = 1337.1337; callptr *dynamic_func = (callptr *) &dynamic; dynamic_func(NULL, x); callptr *fixed_func = (callptr *) &fixed; fixed_func(NULL, x); printf("%f\n", x

Java: overloaded method resolution and varargs — confusing example

左心房为你撑大大i 提交于 2019-11-30 08:37:09
Just when I thought I understood JLS15.12 as it applied to varargs, here's this example: package com.example.test.reflect; public class MethodResolutionTest2 { public int compute(Object obj1, Object obj2) { return 42; } public int compute(String s, Object... objects) { return 43; } public static void main(String[] args) { MethodResolutionTest2 mrt2 = new MethodResolutionTest2(); System.out.println(mrt2.compute("hi", mrt2)); System.out.println(mrt2.compute("hi", new Object[]{mrt2})); System.out.println(mrt2.compute("hi", new Object[]{mrt2, mrt2, mrt2})); } } which prints out 42 43 43 I

Why doesn't Java's main use a variable length argument list?

倖福魔咒の 提交于 2019-11-30 07:50:22
问题 I have a question about the syntax of the Java main declaration: public static void main (String[] args) Since you can pass a variable number of Strings when invoking the main function, shouldn't this be a variable length argument list rather than an array? Why would a command-line invocation of this method with a list of string parameters even work? (Unless there is behind-the-scenes processing that builds an array with the list of strings and then passes that array to the main method...?)

How to write a variadic function in F# emulating a similar Haskell solution?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-30 07:30:29
问题 How can I (if at all) emulate variadic functions (not methods) so that I could write sum 1 2 3 sum 1 2 3 4 5 sum 1 2 3 4 5 6 7 // etc. The code above is just meant as an example - obviously if I would have to sum up a list then [ 1; 2 ; 3] |> List.sum is a much better way. However I am looking for a structurally similar solution like this Haskell solution What is also important is that the normal syntax for function calls and parameter values remains the same. So sum 1 2 3 vs sum(1, 2, 3)

Simplified Varargs Method Invocation in Java 7

梦想的初衷 提交于 2019-11-30 06:42:40
In Java 7, you have the option to put a @SafeVarargs annotation to suppress the warning you get when compiling a method with a non-reifiable varargs parameter. Project Coin's proposal stipulates that the annotation should be used when the method ensures that only elements of the same type as the varargs parameter are stored in the varargs array. What would be an example of a non-safe method? For example, foo() is not safe, it may store non-T in the array, causing problem at [2] <T extends List<?>> void foo(T... args) { List<String>[] array2 = (List<String>[])args; array2[0] = a_list_of_string;

Variadic list constructor, how to default to the correct type and get type safety

醉酒当歌 提交于 2019-11-30 05:32:32
问题 Here's what I've got: {-# LANGUAGE MultiParamTypeClasses , FlexibleInstances #-} class ListResultMult r a where lstM :: a -> [a] -> r listM :: ListResultMult r a => a -> r listM a = lstM a [] instance ListResultMult r a => ListResultMult (a -> r) a where lstM a as x = lstM x $ a:as instance ListResultMult [a] a where lstM a as = reverse $ a:as Here's how it works: > listM 'a' 'b' 'c' :: String "abc" > putStrLn $ listM 'a' 'b' 'c' abc > listM (1::Int) (2::Int) :: [Int] [1,2] Here's how it

Explain Keyword-Only Arguments (VarArgs) in Python

狂风中的少年 提交于 2019-11-30 04:52:07
Please see the code below:- #!/usr/bin/python # Filename: total.py def total(initial=5, *numbers, **keywords): count = initial for number in numbers: count += number for key in keywords: count += keywords[key] return count print(total(10, 1, 2, 3, vegetables=50, fruits=100)) Can someone please explain how is *numbers and **keywords picking up the arguments? A simple explaination is very much appreciayed Thanks in advance kgiannakakis In your code numbers is assigned the (1,2,3) tuple. keywords is assigned a dictionary, containing vegetables and fruits . One star ( * ) defines positional