OCaml约束中的隐式类型变量的范围(Scope of implicit type variables in OCaml constraints)
在Ocaml中,您可以在约束中引入新类型变量,这有助于在类型检查器中强制执行类型标识:
let f g n = (g (n:'n):'n) ;; val f : ('n -> 'n) -> 'n -> 'n = <fun>
显然可以重复使用这些类型变量(否则这将是一个相当无意义的练习)。 但是,由于它们不是通过某些特殊声明引入的,我想知道它的范围是什么? 它是封闭函数,let-binding还是顶级语句?
有没有办法限制这种隐式引入的类型变量的范围?
In Ocaml you can introduce new type variables inside a constraint, which is useful to enforce type-identities in the type-checker:
let f g n = (g (n:'n):'n) ;; val f : ('n -> 'n) -> 'n -> 'n = <fun>
It is obviously possible to re-use these type variables (otherwise it would be a rather pointless exercise). However, since they are not introduced by some special statement, I wonder what there scope is? Is it the enclosing function, let-binding or top-level statement?
Is there a way to limit the scope of such an implicitly introduced type-variable?
原文:https://stackoverflow.com/questions/32074393
最满意答案
感谢Michal的意见,我重新安排了我的控制器,以便通过关联公司创建用户,而不是相反。 相应的公司控制器如下所示:
companies_controller.rb
# GET company/new def new @company = Company.new @company.users.build end # POST company/ def create @company = Company.new(params[:company]) respond_to do |format| if @company.save format.html { redirect_to root_path, notice: 'Welcome! Your account has been created successfully. A confirmation link has been sent to your email address.' } format.json { render json: root_path, status: :created, location: @company } else format.html { render action: "new" } format.json { render json: @company.errors, status: :unprocessable_entity } end end end
GET请求的表单嵌套了Devise User模型字段,如下所示:
new.html.erb
<h2>Create an account</h2> <%= simple_form_for( @company, :html => {:class => 'form-horizontal' }) do |f| %> <%= render 'shared/error_messages', :object => @company %> <%= f.input :name, :label =>'Company name', :placeholder => "Example Company Inc.", :autofocus => true %> <%= f.simple_fields_for :users do |builder| %> <%= builder.input :email, :placeholder => "example@email.com" %> <%= builder.input :password %> <%= builder.input :password_confirmation %> <% end %> <%= f.button :submit, 'Create account', :class => 'btn'%> <% end %> <%= link_to "Sign in", new_session_path(:user) %>
现在,如果验证失败,则所有实例属性值将保留在其各自的表单字段中。
Thanks to Michal's input, I rearranged my controllers so that a user will be created through an associated company and not the other way round. The respective company controller looks like this:
companies_controller.rb
# GET company/new def new @company = Company.new @company.users.build end # POST company/ def create @company = Company.new(params[:company]) respond_to do |format| if @company.save format.html { redirect_to root_path, notice: 'Welcome! Your account has been created successfully. A confirmation link has been sent to your email address.' } format.json { render json: root_path, status: :created, location: @company } else format.html { render action: "new" } format.json { render json: @company.errors, status: :unprocessable_entity } end end end
The form the GET request nests the Devise User model fields as follows:
new.html.erb
<h2>Create an account</h2> <%= simple_form_for( @company, :html => {:class => 'form-horizontal' }) do |f| %> <%= render 'shared/error_messages', :object => @company %> <%= f.input :name, :label =>'Company name', :placeholder => "Example Company Inc.", :autofocus => true %> <%= f.simple_fields_for :users do |builder| %> <%= builder.input :email, :placeholder => "example@email.com" %> <%= builder.input :password %> <%= builder.input :password_confirmation %> <% end %> <%= f.button :submit, 'Create account', :class => 'btn'%> <% end %> <%= link_to "Sign in", new_session_path(:user) %>
Now, if the validation fails, all instance attribute values remain in their respective form field.
相关问答
更多-
感谢Michal的意见,我重新安排了我的控制器,以便通过关联公司创建用户,而不是相反。 相应的公司控制器如下所示: companies_controller.rb # GET company/new def new @company = Company.new @company.users.build end # POST company/ def create @company = Company.new(params[:company]) respond_to do ...
-
“Id”字段上的验证失败(Failed validation on 'Id' field)[2023-07-14]
如果更改为Id变量的名称或将数据类型更改为字符串会发生什么? Ok, so I found out the problem. Inside the constructor of my view model class ('SomeClassViewModel'), I added a line of code that called the default constructor for the 'SomeClass' property. I figured this out by inspecting t ... -
改为 def attempt_signup @new_user = User.new(params[:user]) if @new_user.save .... ..... change it to def attempt_signup @new_user = User.new(params[:user]) if @new_user.save .... .....
-
为了解决这个问题,我在表单的任何输入之前添加了以下内容: <% if @user.profile["addresses"][i.to_s] != nil %> <% i = i.to_s %> <% else %> <% i = i %> <% end %> To solve the problem, I just added the following before any of the form's inputs: <% if @user.profile["addresses"][i.to_s ...
-
您是否在询问如何验证SharePoint表单中的字段的技术? 有几个选项,我会考虑使用JSLink 。 或者您是否在努力编写有效的JavaScript代码? 然后看看这里 。 Are you asking for the technique how to validate fields in a SharePoint form? There are several options and I would consider to use JSLink for that. Or are you struggli ...
-
最好看到你得到的具体错误,但是我会确保你的表单正确地设置了嵌套属性,并且你的user控制器在强参数中设置了嵌套属性。 在Rails指南中有文档: http : //api.rubyonrails.org/classes/ActionController/StrongParameters.html It would be best to see the specific error you're getting, but I'd make sure that your form is setting the ...
-
我担心目前没有这样的选择。 我们的想法是尽快删除错误消息,每个人似乎都喜欢这样。 I'm afraid there is no such option currently. The idea is to remove the error message as soon as possible and everyone appears to like this.
-
您可以将数组作为字段名称传递 $this->form_validation->set_rules('options[]', 'Options', 'required'); 请参阅Codeigniter验证类中使用数组的详细信息文档 。 You can pass array as a field name like this $this->form_validation->set_rules('options[]', 'Options', 'required'); refer a details docu ...
-
你的问题是在方法参数顺序。 @ModelAttribute必须后跟BindingResult参数。 查看Spring文档并查看例17.1。 BindingResult和@ModelAttribute的排序无效 。 。 如果它是其中的一部分,您还应该将MultipartFile添加到表单( Store )。 Store { /* Your other fields */ @NotNull private MultipartFile file; public Multip ...
-
来自具有嵌套属性的表单的验证失败的重复记录(Duplicated record on failed validation from a form with nested attributes)[2022-01-06]
发生这种情况的原因是你的模型和控制器的“新”动作都有User.new,我会在一个地方进行初始化。 我是在“新”行动中做到的。 The reason that this is happening is that you have User.new in both your model and the "new" action of your controller, I would do the initialization in one place. I do it in the "new" action.