Elm中如何实现继承

How to accomplish inheritance in Elm

type alias Car = { model : Model, year: Int }


type Model = Crv | Enzo | Mustang | Viper

-- I can do this
enzo = Car Enzo -- partial Contructor
> myE<function> : Int -> Repl.Car

myEnzo = enzo 2018 
> { model = Enzo, year = 2018 } : Repl.Car

-- but i CANNOT do this
enzo2017 = Car { year: 2017 } -- INVALID

我如何制作一个简单的构造函数,它不会强制我按定义的顺序列出属性。

谈到函数式编程,我们需要"start over"一些概念。在这种情况下,混淆似乎与构造函数有关。 Elm 中的记录构造函数不同于面向对象语言中的对象构造函数。

记录构造函数不创建记录,它只创建一个可以创建记录的函数。它通过部分应用程序来做到这一点。

在部分应用中,您只能按顺序给出参数。这是在 lambda 演算中定义的,我相信它不是 Elm 特有的。 在使用记录构造函数创建记录的情况下,更改参数的顺序不会更改结果。

我们可以使用这个方便的函数 flip returns 相同的函数,但参数的顺序相反:

enzo2017 = flip Car 2017
myEnzo2017 = enzo2017 Viper

考虑一下,当你定义这个时:

type alias Car = { model : Model, year: Int, notes: String}

你真正得到的是这样一个函数Car

> Car
> <function> : Repl.Model -> Int -> String -> Repl.Car

定义另一个构造函数很容易:

> f notes model year = Car model year notes
> <function> : String -> Repl.Model -> Int -> Repl.Car