SBCL 64位,1.1.7
如果我想创建一个包并使用包中的一些小符号:CL,我将创建一个像这样的包:
(defpackage :foo
(:import-from :cl
:defun :defmacro :in-package
:null :car :cdr :cons :if
:eq))
Run Code Online (Sandbox Code Playgroud)
但是,在这个包中,如果我定义一个带有可选参数的函数并在不提供可选参数的情况下调用它,我总会得到一个错误:
(defun test (&optional a))
(test)
invalid number of arguments: 0
[Condition of type SB-INT:SIMPLE-PROGRAM-ERROR]
Restarts:
0: [RETRY] Retry SLIME interactive evaluation request.
1: [*ABORT] Return to SLIME's top level.
2: [REMOVE-FD-HANDLER] Remove #<SB-IMPL::HANDLER INPUT on descriptor 10: #<CLOSURE (COMMON-LISP:LABELS SWANK-BACKEND::RUN :IN SWANK-BACKEND:ADD-FD-HANDLER) {100490B95B}>>
3: [ABORT] Exit debugger, returning to top level.
Run Code Online (Sandbox Code Playgroud)
定义宏得到相同的错误,但有更多信息:
(defmacro test (&rest body))
(test)
error while parsing arguments to DEFMACRO …Run Code Online (Sandbox Code Playgroud) 我对PRESENT使用Fortran 95 的语句有疑问.目前我正在使用Silverfrost的Plato和他们的FTN95编译器(在"Release Win32"模式下).我想要做的是创建一个子程序SUB(a,b),其中b是一个可选变量.到目前为止一切都那么好,但是当我试图给出一个新的价值时,问题就出现b了if (.NOT. present(b)) b=0.这是代码:
module MOD
contains
subroutine SUB(a,b)
implicit none
integer :: a
integer,optional :: b
if (.NOT. present(b)) b=0
print*, a,b
end subroutine SUB
end module MOD
program TEST
use MOD
implicit none
integer :: i=2, j=1
call SUB(i,j)
call SUB(i)
call SUB(j)
end program TEST
Run Code Online (Sandbox Code Playgroud)
有没有一种优雅的方式摆脱这种情况,或者我真的需要创建另一个变量,b_aux例如,然后使用以下代码?:
if (present(b)) then
b_aux=b
else
b_aux=0
endif
Run Code Online (Sandbox Code Playgroud) 我正在使用Python.NET加载一个C#程序集来从Python调用C#代码.这非常干净,但我遇到一个问题,调用一个如下所示的方法:
Our.Namespace.Proj.MyRepo中的方法:
OutputObject GetData(string user, int anID, int? anOptionalID= null)
Run Code Online (Sandbox Code Playgroud)
我可以为存在可选的第三个参数的情况调用该方法,但是无法确定要为第三个参数传递什么以匹配null情况.
import clr
clr.AddReference("Our.Namespace.Proj")
import System
from Our.Namespace.Proj import MyRepo
_repo = MyRepo()
_repo.GetData('me', System.Int32(1), System.Int32(2)) # works!
_repo.GetData('me', System.Int32(1)) # fails! TypeError: No method matches given arguments
_repo.GetData('me', System.Int32(1), None) # fails! TypeError: No method matches given arguments
Run Code Online (Sandbox Code Playgroud)
iPython Notebook表明最后一个参数应该是类型:
System.Nullable`1[System.Int32]
Run Code Online (Sandbox Code Playgroud)
只是不确定如何创建一个与Null案例匹配的对象.
有关如何创建C#认可的Null对象的任何建议?我假设传递原生Python没有用,但事实并非如此.
我有两个功能:
def f(a,b,c=g(b)):
blabla
def g(n):
blabla
Run Code Online (Sandbox Code Playgroud)
c是函数中的可选参数f.如果用户没有指定其值,程序应该计算g(b),这将是值c.但是代码没有编译 - 它说名称'b'没有定义.如何解决?
有人建议:
def g(b):
blabla
def f(a,b,c=None):
if c is None:
c = g(b)
blabla
Run Code Online (Sandbox Code Playgroud)
但这不起作用.也许用户打算c成为None,然后c会有另一个值.
我不知道我是怎么或在哪里得到这个想法,但出于某种原因我认为这是可能的.显然,经过测试它不起作用,但有没有办法让它工作?我想设置$ value2而不必为$ value1输入任何东西.
function test($value1 = 1, $value2 = 2) {
echo 'Value 1: '.$value1.'<br />';
echo 'Value 2: '.$value2.'<br />';
}
test($value2 = 3);
// output
Value 1: 3
Value 2: 2
Run Code Online (Sandbox Code Playgroud) 我有一个带有2个可选参数的方法.
public IList<Computer> GetComputers(Brand? brand = null, int? ramSizeInGB = null)
{
return new IList<Computer>();
}
Run Code Online (Sandbox Code Playgroud)
我现在正尝试在其他地方使用此方法,我不想指定Brand参数,只是int但我使用此代码时遇到错误:
_order.GetComputers(ram);
Run Code Online (Sandbox Code Playgroud)
我收到的错误:
Error 1 The best overloaded method match for 'ComputerWarehouse.Order.GetComputers(ComputerWarehouse.Brand?, int?)' has some invalid arguments C:\Users\avmin!\Documents\InnerWorkings Content\630dd6cf-c1a2-430a-ae2d-2bfd995881e7\content\T0062A2-CS\task\ComputerWarehouse\ComputerStore.cs 108 59 ComputerWarehouse
Error 2 Argument 1: cannot convert from 'int?' to 'ComputerWarehouse.Brand?' C:\Users\avmin!\Documents\InnerWorkings Content\630dd6cf-c1a2-430a-ae2d-2bfd995881e7\content\T0062A2-CS\task\ComputerWarehouse\ComputerStore.cs 108 79 ComputerWarehouse
Run Code Online (Sandbox Code Playgroud) 当我在函数定义中使用可选参数时,我的省略号有问题.为了澄清,我定义了以下功能:
func1 <- function (x) (x-2)^2
func3 <- function (fun, arg.curve.user){
arg.curve.user$expr <- substitute(func1)
arg.curve.default <- list(col = "blue", n = 1000, main = "This is a test")
arg.curve <- modifyList (arg.curve.default, arg.curve.user)
do.call("curve", arg.curve)
}
# optimizes func1 and call func2 to plot func1
func2 <- function (lb, ub, n.restarts = 5, n.sim = 10, ...){
arg.curve.user <- as.list(substitute(list(...)))
output <- gosolnp(fun = func1, LB = lb, UB = ub, n.restarts = n.restarts,
n.sim = n.sim)$par
func3(fun = func1, …Run Code Online (Sandbox Code Playgroud) 我想创建一个具有默认值的可选参数的函数
def my_function(a = nil, b=nil, c=500)
end
Run Code Online (Sandbox Code Playgroud)
并使用我想要指定的参数调用该函数
my_function(b=100)
Run Code Online (Sandbox Code Playgroud)
我如何在Ruby 1.9.2中实现这一目标?
我正在使用elses class,而且这个人定义了一个带有五个参数的函数.
在Sentry.php中:
function checkLogin($user = '',$pass = '',$group = 10,$goodRedirect = '',$badRedirect = '')
Run Code Online (Sandbox Code Playgroud)
如果填写了所有五个字段,则会导致登录过程.
现在在他解释如何使用它的页面上有一个片段,根据php.net,没有意义.
在加载哨兵的页面中:
require_once('../system/Sentry.php');
$theSentry = new Sentry();
if(!$theSentry->checkLogin(2)){ header("Location: login.php"); die(); }
Run Code Online (Sandbox Code Playgroud)
默认情况下,它应该表现为检查$ group参数是否<= 10(默认值).在这种情况下应该是两个.如果用户选中的组变量<= 2,则应该允许此人查看页面.
但是,这不起作用,并且有一个非常明显的原因:php手册指出:
请注意,使用默认参数时,任何默认值都应位于任何非默认参数的右侧; 否则,事情将无法按预期工作.
所以代码,根据phpbuilder.com应该没有可选的($variable = default_something)字段来填充函数的调用,它绝对不应该被定义为五个参数中的第三个.
我该如何使用这样的功能?:
checkLogin(2)
Run Code Online (Sandbox Code Playgroud) 任何想法如何(如果可能)用JSF页面中的可选参数调用java方法?我使用Java 7,JSF 2.1,EL 2.2(Glassfish 3.1.2).提前致谢...
我得到了这个例外
javax.el.ELException: /example.xhtml: wrong number of arguments
Caused by: java.lang.IllegalArgumentException: wrong number of arguments
Run Code Online (Sandbox Code Playgroud)
页面示例
<h:outputText value="#{bean.methodWithParameters('key.en.currentDate', '2012-01-01', '00:00')}"/>
<h:outputText value="#{bean.methodWithParameters('key.en.currentTime', '12:00')}"/>
Run Code Online (Sandbox Code Playgroud)
豆的例子
public String methodWithParameters(String key, Object ... params) {
String langValue = LanguageBean.translate(key);
return String.format(langValue, params);
}
Run Code Online (Sandbox Code Playgroud)
属性示例
key.en.currentDate=Today is %s and current time is %s.
key.en.currentTime=Current time is %s.
key.en.currentDate=Today is %1$s and current time is %2$s.
key.en.currentTime=Current time is %2$s.
Run Code Online (Sandbox Code Playgroud)