Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Inheritance 在Elixir中模拟角色继承_Inheritance_Elixir_Actor - Fatal编程技术网

Inheritance 在Elixir中模拟角色继承

Inheritance 在Elixir中模拟角色继承,inheritance,elixir,actor,Inheritance,Elixir,Actor,我和我的一些同事正在设计一种新的编程语言,我们正在考虑将Elixir作为目标语言。但问题是,该语言主要基于Scala的语法和语义,这意味着它需要以某种方式允许继承 然而,与其说是类,不如说是在它们的位置使用参与者,但这引发了一个有趣的问题:如何使用Elixir函数模拟类继承?或者换句话说,是否有可能以某种方式让一个函数继承另一个函数的接收器模式 A-la这个: def io_port do receive do {:transmit, data} -> # do stuff w

我和我的一些同事正在设计一种新的编程语言,我们正在考虑将Elixir作为目标语言。但问题是,该语言主要基于Scala的语法和语义,这意味着它需要以某种方式允许继承

然而,与其说是类,不如说是在它们的位置使用参与者,但这引发了一个有趣的问题:如何使用Elixir函数模拟类继承?或者换句话说,是否有可能以某种方式让一个函数继承另一个函数的
接收器
模式

A-la这个:

def io_port do
  receive do
    {:transmit, data} -> # do stuff with data
    {:receive, data} -> # do other stuff with data
  end
end

def thunderbolt, extends: io_port do
  receive do # also accepts all the same messages io_port does
    {:display, data} -> # same story again
  end
end

Elixir不允许在匹配级别插入宏,如:

receive do
  macro1()
  macro2()
end
因此,您无法真正对消息进行动态匹配。
但您可以使用OTP组件来实现类似于继承的功能 比如:

写一些类似的东西

defmodule Actor

  defmacro __using__([]) do
    __MODULE__.receiver
  end

  defmacro __using__([extends: mod]) do
    mod.receiver
    __MODULE__.receiver
  end

end
当然,这只是一个概念,没有经过真正的测试。但它可以像GenServer那样工作,再加上使用OTP将为您节省大量重新发明轮子的时间

附言。
PM我关于你的编程语言。如果值得的话,我很乐意帮你

我建议你看看长生不老药的治疗方案。另外,请查看Elixir的
使用
宏:。这感觉很像是继承和/或混合Ruby。更不用说,你可以看看
use
宏的实现,并获得一些实现你自己的宏的想法,这些宏为你提供了你想要的语言功能。我还打算推荐
use
宏。我会在通过考试后给你写信。到目前为止,它仍然是一个学生项目
defmodule Thunderbolt do
  use Actor, extend: IOPort

  defmacro receiver do
    quote do
      def handle_call({:display, data}), do: # do stuff with data
     end
  end
end
defmodule Actor

  defmacro __using__([]) do
    __MODULE__.receiver
  end

  defmacro __using__([extends: mod]) do
    mod.receiver
    __MODULE__.receiver
  end

end