OCaml联合类型(OCaml union types)
我正在尝试创建一个使用已定义类型的子类型但OCaml不会派生正确值的函数:请考虑以下类型定义:
type fraction = {numerator : int; denominator : int};; type number = | Int of int | Fraction of fraction;;
如果我尝试输入interpeter(如果重要的话我使用utop):
utop # ((fun a-> a>0) (Int 1));; Error: This expression has type number but an expression was expected of type int
类型号也是一个int,但我不能理解这个功能,我怎么解决这个问题呢?
I'm trying to make a function that uses a subtype of a defined type but OCaml wont derive the correct value : Consider this type definition:
type fraction = {numerator : int; denominator : int};; type number = | Int of int | Fraction of fraction;;
if i try to type into the interpeter (i'm using utop if it matters) :
utop # ((fun a-> a>0) (Int 1));; Error: This expression has type number but an expression was expected of type int
type number is also an int but I cant get the function to understand that, how can I solve this?
原文:https://stackoverflow.com/questions/33845651
最满意答案
v1.4中引入了新的django.contrib.admin.SimpleListFilter满足您的需求,并且官方文档提供了示例代码并且易于阅读。 在本节中搜索SimpleListFilter。
There is new django.contrib.admin.SimpleListFilter introduced in v1.4 meet your need, and official document provide sample code and easy to read. search SimpleListFilter in this section.
相关问答
更多-
自定义Django管理员建议(Customize Django Admin Advice)[2023-06-13]
这是一个相当广泛的问题,但我会试一试。 有几种方法可以实现这一目标: 使用筛选器查询作为变量设置模型。 楷模: class Filter(models.Model): Filter_Query = models.CharField(max_length=30) 观点: from app_name.models import Filter, Some_Model def filter(request, pk): template = loader.get_template("app_name/ ... -
您可以使用自定义QuerySet执行此操作。 我建议创建自定义过滤器方法,而不是覆盖现有的过滤器方法。 基于https://stackoverflow.com/a/33916628/3627387的 更新您可以使用extra功能实现它 class CustomQuerySet(models.QuerySet): def date_filter(self): # using return self.extra(where=["some_date + hours * IN ...
-
尝试编写自己的Widget,然后像这样覆盖你的: class CustomerAdmin(admin.ModelAdmin): formfield_overrides = { models.DateField: { 'widget': AdminDateWidget }, } Try to write your own Widget and then override yours like this: class CustomerAdmin(admin.ModelAdmin ...
-
Django 1.3或更低版本中的Django Admin中的自定义过滤器(Custom Filter in Django Admin on Django 1.3 or below)[2022-12-10]
感谢gpilotino给我推动正确的方向来实现这一点。 我注意到这个问题的代码是使用datetime来计算它的实时时间。 所以我使用DateFieldFilterSpec并将其子类化。 from django.db import models from django.contrib.admin.filterspecs import FilterSpec, ChoicesFilterSpec,DateFieldFilterSpec from django.utils.encoding import smart ... -
Django Admin中的日期字段自定义过滤器,Django 1.2(Custom Filter for Date Field in Django Admin, Django 1.2)[2022-05-16]
现在我已经掌握了我认为你想要的东西,我假设你有一个你希望通过DateField过滤的模型,如: class Position(models.Model): expiration_date = models.DateField() ... 您现在应该修改为 class Position(models.Model): expiration_date = models.DateField() expiration_date.is_expired_filter = True ... -
我最终使用了这个: date_hierarchy = 'created' 这对我来说是一个很好的功能。 I ended up using this instead: date_hierarchy = 'created' it's a good enough feature for me to sort with.
-
Django 1.4定义用户(Django 1.4 defining user)[2023-06-11]
看起来您的环境配置不正确。 在运行python manage.py syncdb之前尝试导出这些环境变量: export LANG=es_ES.UTF-8 export LC_ALL=es_ES.UTF-8 python manage.py syncdb 如果您从Debian衍生物(如* buntu)运行该命令,请尝试以下操作: sudo dpkg-reconfigure locales 编辑 :等等。 现在我看到(pythonanywhere2)bash-3.2$ 。 您是否在tutorial.py ... -
使用UserProfile字段的Django 1.4 User Admin list_filter(Django 1.4 User Admin list_filter using UserProfile field)[2021-12-01]
您引用的第一个错误似乎特定于我从未使用过的FieldListFilter 。 我想重点是自动化与特定领域相关的东西。 当你提供所有选项时,我认为你的例子没用。 我现在只使用SimpleListFilter ...... 无论如何你的错误是usertypes是一组c.user_type ,它是一个字符串。 它没有id属性。 您需要返回[(c, c) for c in usertypes] ,其中第一个值是传递给QS的值,第二个值是显示值。 其余的不需要改变。 The first error you refer ... -
我的错误最终是相当愚蠢的。 我的一个自定义上下文处理器重新定义了“cl”上下文var,因此使以下标记失败: {% url 'admin:app_list' app_label=cl.opts.app_label %} 我只需要对自己的上下文处理器和模板进行一些更改(并使用更好的上下文处理器var名称)。 注意:我不知道为什么以前没有打扰过我。 理论上,即使在1.1中也应该是一个问题。 My error was rather dumb in the end. One of my custom context ...
-
v1.4中引入了新的django.contrib.admin.SimpleListFilter满足您的需求,并且官方文档提供了示例代码并且易于阅读。 在本节中搜索SimpleListFilter。 There is new django.contrib.admin.SimpleListFilter introduced in v1.4 meet your need, and official document provide sample code and easy to read. search Simp ...