谁能解释为什么下面的代码会编译,但如果我注释掉一行,那么它不会,即使代码本质上在做同样的事情?
struct OtherStruct {
x: i32,
}
struct Inner {
blah: i32,
vector: Vec<OtherStruct>
}
struct Outer {
inner: Inner,
}
impl Inner {
pub fn set_blah(&mut self, new_val : i32) {
self.blah = new_val;
}
}
fn main() {
let mut outer = Outer {
inner: Inner {
blah: 10,
vector: vec![
OtherStruct { x: 1 },
OtherStruct { x: 2 },
OtherStruct { x: 3 },
OtherStruct { x: 4 },
OtherStruct { x: 5 },
]
} …
Run Code Online (Sandbox Code Playgroud) 我在尝试使用 FP-TS 实现事物时不断遇到的一种模式是,当我的管道涉及到 TaskEither 的分支和合并分支时。
合并似乎工作得很好,因为我可以使用sequenceT创建数组并将它们通过管道传输到函数中,然后使用所有这些值。
似乎不太有效的是更复杂的依赖关系图,其中一个函数需要较早的项目,然后需要该函数的输出以及第一个任务的原始结果。
基本上像这样的函数签名(这可能不是 100% 正确的类型,但了解其要点):
function fetchDataA(): TaskEither<Error, TypeA> {
}
function fetchBBasedOnOutputOfA(a: TypeA): TaskEither<Error, TypeB> {
}
function fetchCBasedOnOutputOfAandB(a: TypeA, b: TypeB): TaskEither<Error, TypeC> {
}
Run Code Online (Sandbox Code Playgroud)
因为在管道中,你可以很好地为前两个作曲
pipe(
fetchDataA(),
TE.map(fetchBBasedOnOutputOfA)
)
Run Code Online (Sandbox Code Playgroud)
这个管道按预期返回 TaskEither<Error, TypeB> ,并且地图处理错误对我来说很好。
而要执行最后一个操作,我现在需要输入 TypeA 作为参数,但它不可用,因为它已传递到 B 中。
一种解决方案是让函数 B 同时输出 A 和 B,但这感觉不对,因为创建 B 的函数不必知道其他某个函数也需要 A。
另一种方法是创建某种中间函数来存储 A 的值,但在我看来,这破坏了使用 TaskEither 的全部意义,即我要抽象出所有错误类型并自动处理它。
我会有某种奇怪的功能:
async function buildC(a : TypeA): TaskEither<Error, TypeC> {
const b = await fetchBBasedOnOutputOfA(a);
// NOW DO MY OWN ERROR …
Run Code Online (Sandbox Code Playgroud) 当我进行此类设置时,我收到错误:
默认_测试.rs:
mod default_mod;
use default_mod::Point;
fn main() {
let _p1 = Point::new();
let _p2: Point = Point {
z: 1,
..Default::default()
};
}
Run Code Online (Sandbox Code Playgroud)
默认_mod.rs:
pub struct Point {
x: i32,
y: i32,
pub z: i32,
}
impl Point {
pub fn new() -> Self {
Point { x: 0, y: 0, z: 0 }
}
}
impl Default for Point {
fn default() -> Self {
Point { x: 0, y: 0, z: 0 }
}
}
Run Code Online (Sandbox Code Playgroud)
这给出了编译器错误:
default_test.rs:9:7 …
Run Code Online (Sandbox Code Playgroud) 我想使用我自己的自定义转换器方法来从数据库中编组/解组数据结构。
所以我的实体看起来像这样:
class EntityName {
@Column({ type: `text`, transformer: { from: fromPostgres, to: toPostgres } })
colName: CustomClass;
}
Run Code Online (Sandbox Code Playgroud)
然后我显然有一个自定义类,沿着这些思路:
class CustomClass {
propertyA: string;
propertyB: number;
}
Run Code Online (Sandbox Code Playgroud)
我的映射函数:
fromPostgres(value: any) : any {
let c = new CustomClass();
c.propertyA = value.split(" ")[0];
c.propertyB = parseInt(value.split(" ")[1]);
return c;
}
toPostres(value: any) : any {
return `${value.propertyA} ${value.propertyB}`
}
Run Code Online (Sandbox Code Playgroud)
等等。显然这不是用例,但你明白了。
所以这一切都很好,没问题。除非您使用 FindOperators 进行查询。你知道,像这样:
let c = new CustomClass();
c.propertyA = "a";
c.propertyB = 12;
getRepository(EntityName).find({where: { colName: LessThan(c) } }); …
Run Code Online (Sandbox Code Playgroud) rust ×2
typescript ×2
default ×1
fp-ts ×1
mutable ×1
postgresql ×1
private ×1
public ×1
typeorm ×1
vector ×1