标签: default-parameters

如何在 dart 函数中将函数作为可选参数传递并带有默认值?

这就是我想要实现的:

void fun({
  bool Function(int i) predicate = (i) => false,
}) {
  // do something with 'predicate(something)'
}
Run Code Online (Sandbox Code Playgroud)

但我收到错误:

可选参数的默认值必须是constant.dart(non_constant_default_value)。

我能够通过以下方法解决此错误:

bool falsePredicate(int i) => false;

void fun({
  bool Function(int i) predicate = falsePredicate,
}) {
  // do something with 'predicate(something)'
}
Run Code Online (Sandbox Code Playgroud)

但现在问题来了,为什么我不能像第一组代码那样直接创建一个默认函数值呢?第一种情况和第二种情况似乎没有什么区别。第一种方法中给出的函数为什么不是常数?

default-parameters dart

6
推荐指数
1
解决办法
1063
查看次数

根据另一个参数的值设置一个参数的默认值

所以我想创建一个函数,生成从“开始”到“结束”与“大小”一样多的连续数字。对于迭代,它将在函数内部计算。但我在设置参数“end”的默认值时遇到问题。在我进一步解释之前,先看一下代码:

# Look at this -------------------------------
#                                           ||
#                                           \/
def consecutive_generator(size=20, start=0, end=(size+start)):
    i = start
    iteration = (end-start)/size

    arr = []
    temp_size = 0

    while temp_size < size:
        arr.append(i)

        i += iteration
        temp_size += 1

    return arr

# with default end, so the 'end' parameter will be 11
c1= consecutive_generator(10, start=1)
print(c1)

# with end set
c2= consecutive_generator(10, end=20)
print(c2)


Run Code Online (Sandbox Code Playgroud)

从上面可以看出(关于'end'参数的默认值),我想要实现的是'end'参数,其默认值为'start'+'size'参数(那么迭代将是1)

输出肯定会出错。那么我该怎么做呢?(这是我第一次在 stackoverflow 上提问,如果我犯了错误,抱歉)

(关闭)

python parameters function default-parameters

6
推荐指数
1
解决办法
2114
查看次数

使用参数默认值键入擦除

以下内容无法编译:

package play

object Stats2 {
  def variance(data: Seq[Double], dof: Int = 0): Double = {
    println("variance Double direct"); 1.0
  }
  def variance[T](data:Seq[T], dof: Int = 0)(implicit ex: T => Double): Double = {
    println("variance Double extracted"); 1.0
  }
} 
Run Code Online (Sandbox Code Playgroud)

编译器说:

$ scalac erasure2.scala 
erasure2.scala:7: error: double definition:
method variance$default$2:[T]=> Int and
method variance$default$2:=> Int at line 4
have same type after erasure: ()Int
  def variance[T](data:Seq[T], dof: Int = 0)(implicit ex: T => Double): Double = {
                               ^
one error …
Run Code Online (Sandbox Code Playgroud)

scala type-erasure default-parameters

5
推荐指数
1
解决办法
146
查看次数

如何为T [],T [] []编写重载的泛型扩展方法而没有歧义?

我想编写用于将向量和矩阵转换为字符串的扩展方法.我是用以下方式做到的.

对于矢量

public static string GetString<T>(this T[] SourceMatrix, string ColumnDelimiter = " ")
{
    try
    {
        string result = "";
        for (int i = 0; i < SourceMatrix.GetLength(0); i++)
            result += SourceMatrix[i] + ColumnDelimiter;
        return result;
    }
    catch (Exception ee) { return null; }
}
Run Code Online (Sandbox Code Playgroud)

对于Matrix

public static string GetString<T>(this T[][] SourceMatrix, string ColumnDelimiter = " ", string RowDelimiter = "\n")
{
    try
    {
        string result = "";
        for (int i = 0; i < SourceMatrix.GetLength(0); i++)
        {
            for (int …
Run Code Online (Sandbox Code Playgroud)

c# generics extension-methods default-parameters

5
推荐指数
1
解决办法
129
查看次数

使用JavaScript在默认情况下动态创建函数

ECMAScript 6引入的功能之一是能够在JavaScript中指示未指定参数的默认值,例如

function foo(a = 2, b = 3) {
    return a * b;
}

console.log(foo());   // 6
console.log(foo(5));  // 15
Run Code Online (Sandbox Code Playgroud)

现在我想知道是否可以对Function构造函数动态创建的函数使用默认参数,如下所示:

new Function('a = 2', 'b = 3', 'return a * b;');
Run Code Online (Sandbox Code Playgroud)

Firefox 39似乎已经支持默认参数(参见此处),但上面的行被拒绝作为语法错误.

javascript default-parameters ecmascript-6

5
推荐指数
1
解决办法
189
查看次数

将私有常量用作公共方法的默认参数背后的想法是什么?

令我惊讶的是,这个编译并运行:

class Program
{
    static void Main()
    {
        DoSomething();
    }

    private const int DefaultValue = 2; // <-- Here, private.

    public static void DoSomething(int value = DefaultValue)
    {
        Console.WriteLine(value);
    }
}
Run Code Online (Sandbox Code Playgroud)

该方法是公共的,而默认值"重定向"到常量私有变量.

我的问题:

这个"概念"背后的想法是什么?

我的理解(直到今天)是,只有所有其他"引用"元素也是公开的,才能使用公共事物.

更新:

我只是ILSpy反编译我的课找到:

static class Program
{
    private static void Main()
    {
        Program.DoSomething(2);
    }

    private const int DefaultValue = 2;

    public static void DoSomething(int value = 2)
    {
        Console.WriteLine(value);
    }
}
Run Code Online (Sandbox Code Playgroud)

因此,如果私有常量作为默认参数在例如库中完成,则库的用户仍然可以看到默认参数.

.net c# default-parameters

5
推荐指数
4
解决办法
207
查看次数

使用默认参数为Kotlin函数键入签名

比方说我有:

fun addInvoker(adder: () -> Int = ::add): Int{
    return adder()
}

fun add(num1:Int = 1, num2:Int = 1): Int{
    return num1 + num2
}
Run Code Online (Sandbox Code Playgroud)

我得到一个错误,因为:: add有两个参数,但addInvoker的签名要求它有零参数.但是,如果我将其更改为:

fun addInvoker(adder: (Int, Int) -> Int = ::add): Int{
    return adder()
}

fun add(num1:Int = 1, num2:Int = 1): Int{
    return num1 + num2
}
Run Code Online (Sandbox Code Playgroud)

然后我不能调用adder(),即使用默认参数调用add.

那么,有什么办法可以制作::将默认参数添加到invokeAdder但仍然调用add with adder(),从而使用默认的args调用它?

default-parameters kotlin

5
推荐指数
1
解决办法
609
查看次数

Python中默认参数的范围

我正在学习Python并遇到了一个我不太了解的例子.在官方教程中,给出了以下代码:

i = 5

def f(arg=i):
    print(arg)

i = 6
f()
Run Code Online (Sandbox Code Playgroud)

来自c ++,直觉对我来说这将打印5.但我也想理解技术解释:"默认值在定义范围内的函数定义点进行评估." "定义范围"在这里意味着什么?

python parameters scope parameter-passing default-parameters

5
推荐指数
1
解决办法
174
查看次数

对于具有默认模板参数的多态类,模板推导失败

考虑这个例子:

#include <memory>

template<typename T>
class A {};

template<typename T1, typename T2>
class B: public A<T1> {};

template<typename T = int>
void foo(std::shared_ptr< A<T> > test)
{

}

int main()
{
    auto p = std::make_shared<B<int, int>>();
    foo<int>(p);    // Works
    foo<>(p);       // Does not work
    foo(p);         // Does not work
}
Run Code Online (Sandbox Code Playgroud)

我试图在没有为foo明确指定类型T的情况下编译它,但它不起作用.我不确定为什么好像我明确表示类型T,它工作正常,但如果我,它不编译,即使我已经告诉编译器,如果我不这样做类型T应该是什么明确指定它.

我明白为什么编译器不能推断类型T,但为什么我不指定它时它不能使用我的默认类型T?我如何解决这个问题以及"正确"的做法是什么?

c++ polymorphism default-parameters template-argument-deduction

5
推荐指数
1
解决办法
109
查看次数

合法使用非跟踪功能模板参数包?

在调试某人讨厌的宏生成的代码时,我看到仅MSVC ++对类似于以下函数模板声明的内容不满意:

template <typename ...Vs, typename>
void foo();
Run Code Online (Sandbox Code Playgroud)

很公平。我对为什么GCC和Clang会编译它感到困惑。我foo在上面的声明中添加了的定义,现在GCC也产生了编译错误(Clang仍然存在)。此代码如下:

template <typename ...Vs, typename>
void foo();

template <typename ...Vs, typename = int>
void foo() {  }

int main(int argc, char *argv[])
{
  foo<char,float>();
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

Clang是对还是错?我注意到,如果删除了声明,则GCC和MSVC ++可以编译。

c++ function-templates default-parameters variadic-templates c++11

5
推荐指数
1
解决办法
84
查看次数