我有一些构造函数和函数,我希望始终使用命名参数调用.有没有办法要求这个?
我希望能够为具有许多参数的构造函数和函数执行此操作,并且对于那些在使用命名参数时更清楚地读取的函数,等等.
Hibernate保持检测
org.hibernate.QueryParameterException: could not locate named parameter [name]
Run Code Online (Sandbox Code Playgroud)
即使它存在.这是我的hql
Query query = sess().createQuery("from UserProfile where firstName LIKE '%:name%'").setParameter("name", name);
Run Code Online (Sandbox Code Playgroud)
为什么hibernate不断抛出异常?即使参数存在?
例:
class Planet(Enum):
MERCURY = (mass: 3.303e+23, radius: 2.4397e6)
def __init__(self, mass, radius):
self.mass = mass # in kilograms
self.radius = radius # in meters
Run Code Online (Sandbox Code Playgroud)
参考:https://docs.python.org/3/library/enum.html#planet
我为什么要这样做?如果构造函数列表中有一些原始类型(int,bool),那么使用命名参数会很好.
我有一个类,我用3个参数定义了构造函数,这些参数都是可选的.我希望能够传入命名参数,所以我不需要传入undefined.
constructor(year?: number,
month?: number,
date?: number)
Run Code Online (Sandbox Code Playgroud)
我希望像这样创建一个类的intance
const recurrenceRule = new MyNewClass(month: 6)
Run Code Online (Sandbox Code Playgroud)
但它没有用,我试过了
const recurrenceRule = new MyNewClass(month = 6)
Run Code Online (Sandbox Code Playgroud)
那不起作用.
我得到它的唯一方法是
const recurrenceRule = new MyNewClass(undefined, 4)
Run Code Online (Sandbox Code Playgroud)
要么
const recurrenceRule = new MyNewClass(, 4)
Run Code Online (Sandbox Code Playgroud)
但它似乎很混乱,我希望传递命名参数,因为它们都是可选的我应该能够传入1 - 对吗?
以前,我使用过python.在Python中,我使用命名参数(关键字参数)进行函数调用.关于命名参数的维基百科页面告诉我C++不支持它.为什么C++不支持命名参数?它是否支持未来版本的C++标准?
有没有人知道Scala中是否有类似的东西:
case class Thing(property:String)
def f(thing:Thing, prop:String = thing.property) = println(prop)
Run Code Online (Sandbox Code Playgroud)
上面的代码没有编译; 给错误error: not found: value thing的thing.property
以下显示了预期的行为:
f(Thing("abc"), "123") // prints "123"
f(Thing("abc")) // prints "abc"
Run Code Online (Sandbox Code Playgroud)
我意识到我可以使prop参数成为一个Option[String]并在函数定义中进行检查,但我想知道是否有一种方法可以使用2.8.0中的新命名/默认参数支持.
这似乎是一个简单的问题,但由于某种原因,我无法在任何地方找到答案.基本上,我希望能够实现一个带NamedParameters的构造函数.
通过命名参数,我不是指具有默认值(可选参数)的参数,例如:
public SomeMethod(){
string newBar = Foo(bar2 : "customBar2");
}
public string Foo(string bar1 = "bar1", bar2 = "bar2" ){
//...
}
Run Code Online (Sandbox Code Playgroud)
什么我想实现一个很好的例子是AuthorizeAttribute从System.Web.Mvc装配.您可以使用以下方式:
[Authorize(Roles = "Administrators", Users = "ThatCoolGuy")]
public ActionResult Admin(){
}
Run Code Online (Sandbox Code Playgroud)
intellisense中构造函数的签名类似于以下示例,我相信(请确认)那些NamedParameters正在映射到类属性.
AuthorizeAttribute.AuthorizeAttribute(NamedParameters ...)Initiliaze System.Web.Mvc.AuthorizeAttribute类的新实例
命名参数:
- 订单int
- 用户字符串
- 角色字符串
Ruby 2.0.0支持关键字参数(KA),我想知道在纯Ruby的上下文中这个特性的好处/用例是什么,特别是考虑到由于关键字匹配导致的性能损失,需要每个时间调用带有关键字参数的方法.
require 'benchmark'
def foo(a:1,b:2,c:3)
[a,b,c]
end
def bar(a,b,c)
[a,b,c]
end
number = 1000000
Benchmark.bm(4) do |bm|
bm.report("foo") { number.times { foo(a:7,b:8,c:9) } }
bm.report("bar") { number.times { bar(7,8,9) } }
end
# user system total real
# foo 2.797000 0.032000 2.829000 ( 2.906362)
# bar 0.234000 0.000000 0.234000 ( 0.250010)
Run Code Online (Sandbox Code Playgroud) 我正在使用Rails基于一组复杂的嵌套属性自动神奇地创建子对象.因此,我需要以非常特殊的方式嵌套参数.显然我意识到我可以用JS构建它们,但是我希望表单的顺序自动帮助构造.对于上下文,我有2列,由2 <td>秒表示.每列可以创建新记录或编辑现有记录.当然,当要修改现有记录时,必须传递记录的id.
呈现的HTML如下:
<td width="50%" style="padding-right:3%" class="logistic-details" data-type="logistics" data-typelogistics="delivery" data-instructions="test instructions" data-id="1" data-amount="20">
<span class="area-to-inject-amount-inputs" data-object="type_logistics" data-type="logistics" data-typelogistics="delivery">
<input class="labeler-response" name="type_logistics_attributes[][id]" type="hidden" value="1">
<input class="labeler-response" name="type_logistics_attributes[][instructions]" type="text" value="test instructions">
</span>
</td>
<td width="50%" style="padding-right:3%" class="logistic-details" data-type="logistics" data-typelogistics="pickup" data-instructions="" data-id="" data-amount="0">
<span class="area-to-inject-amount-inputs" data-object="type_logistics" data-type="logistics" data-typelogistics="pickup" data-actioned="charged">
<input type="hidden" name="type_logistics_attributes[][type_of_logistics]" value="pickup">
<input class="injected-amount-input" type="number" min="0" max="" placeholder="Amount" name="type_logistics_attributes[][charged_amounts_attributes][][amount]" value="20">
<span class="area-to-inject-type-of-amount">
<input type="hidden" name="type_logistics_attributes[][charged_amounts_attributes][][type_of_amount]" value="logistics">
</span>
<input class="labeler-response" name="type_logistics_attributes[][instructions]" type="text" placeholder="Enter address and instructions">
</span>
</td>
Run Code Online (Sandbox Code Playgroud)
在这种情况下,第一个<td>是修改id为1的现有记录,而第二个<td> …
html ruby-on-rails named-parameters nested-forms nested-attributes
看一下我的库的一些scala-docs,在我看来,值类中有一些不需要的噪音.例如:
implicit class RichInt(val i: Int) extends AnyVal {
def squared = i * i
}
Run Code Online (Sandbox Code Playgroud)
这引入了不必要的符号i:
4.i // arghh....
Run Code Online (Sandbox Code Playgroud)
那些东西出现在scala文档和IDE自动完成中,这实际上并不好.
那么......关于如何缓解这个问题的任何想法?我的意思是你可以使用,RichInt(val self: Int)但这不会让它变得更好(4.self,是吗?)
编辑:
在以下示例中,编译器是否擦除了中间对象?
import language.implicitConversions
object Definition {
trait IntOps extends Any { def squared: Int }
implicit private class IntOpsImpl(val i: Int) extends AnyVal with IntOps {
def squared = i * i
}
implicit def IntOps(i: Int): IntOps = new IntOpsImpl(i) // optimised or not?
}
object Application …Run Code Online (Sandbox Code Playgroud) named-parameters ×10
scala ×2
.net ×1
c# ×1
c++ ×1
enums ×1
hibernate ×1
hql ×1
html ×1
java ×1
kotlin ×1
nested-forms ×1
python ×1
python-3.x ×1
ruby ×1
scala-2.8 ×1
typescript ×1
value-class ×1
visibility ×1