首页 \ 问答 \ OCaml联合类型(OCaml union types)

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
更新时间:2023-05-30 20:05

最满意答案

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.

相关问答

更多
  • 这是一个相当广泛的问题,但我会试一试。 有几种方法可以实现这一目标: 使用筛选器查询作为变量设置模型。 楷模: 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 ...
  • 感谢gpilotino给我推动正确的方向来实现这一点。 我注意到这个问题的代码是使用datetime来计算它的实时时间。 所以我使用DateFieldFilterSpec并将其子类化。 from django.db import models from django.contrib.admin.filterspecs import FilterSpec, ChoicesFilterSpec,DateFieldFilterSpec from django.utils.encoding import smart ...
  • 现在我已经掌握了我认为你想要的东西,我假设你有一个你希望通过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.
  • 看起来您的环境配置不正确。 在运行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 ...
  • 您引用的第一个错误似乎特定于我从未使用过的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 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)