首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用variant选项在rescript中键入函数参数?

如何使用variant选项在rescript中键入函数参数?
EN

Stack Overflow用户
提问于 2021-04-14 16:56:49
回答 1查看 52关注 0票数 0

我想做以下几件事。但是我似乎不能使用其中一个变量选项来键入函数参数。在rescript中实现这一点的正确方法是什么?

Here is a playground

代码语言:javascript
复制
  type subject = Math | History

  type person =
    | Teacher({firstName: string, subject: subject})
    | Student({firstName: string})
  
  let hasSameNameThanTeacher = (
    ~teacher: Teacher, // syntax error here
    ~student: Person,
  ) => {
    teacher.firstName == student.firstName
  }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-14 17:58:22

TeacherStudent本身不是类型,而是构造person类型的值的构造函数。如果你想让它们有不同的类型,你必须让它显式:

代码语言:javascript
复制
module University = {
  type subject = Math | History

  type teacher = {firstName: string, subject: subject}
  type student = {firstName: string}
  type person =
    | Teacher(teacher)
    | Student(student)
  
  let hasSameNameThanTeacher = (
    ~teacher: teacher,
    ~student: student,
  ) => {
    teacher.firstName == student.firstName
  }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67088572

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档