gpt4 book ai didi

PureScript - 访问 NewType 的属性

转载 作者:行者123 更新时间:2023-12-05 01:23:23 25 4
gpt4 key购买 nike

考虑以下代码示例,它创建了一个新类型来表示客户模型:

module Main where

import Effect (Effect)
import Effect.Console ( logShow )
import Prelude (Unit,(+),(/),(*),(-), (<>),discard)

newtype Customer
= Customer
{ firstname :: String
}

sample :: Customer
sample = Customer
{ firstname : "Average"
}

first :: Customer -> String
first a = _.firstname a

main = do
logShow ( first sample )

预期的输出将是值 Average,它等于 sample.name,但会产生错误:

 Could not match type

{ firstname :: t0
| t1
}

with type

Customer


while checking that type Customer
is at least as general as type { firstname :: t0
| t1
}
while checking that expression a
has type { firstname :: t0
| t1
}
in value declaration first

where t0 is an unknown type
t1 is an unknown type

这是一个很好的错误,但没有解释如何实际访问这个值。

如何访问作为 newType 创建的对象的值?

最佳答案

你必须做

first :: Customer -> String
first (Customer a) = _.firstname a

因为 newtype 实际上是一种新类型。

关于PureScript - 访问 NewType 的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72433059/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com