如何从Julia中的函数调用参数列表

时间:2017-08-02 02:35:02

标签: julia

如何调用函数p?我不知道如何定义type Params a::TypeOfA b::TypeOfB c::TypeOfC end _unpack(p::Params) = (p.a, p.b, p.c) function dxdt(x, p::Params) a, b, c = _unpack(p) a*x^2 + b*x + c end 作为调用函数的参数。

public function up()
{
    Schema::create('friend_user', function(Blueprint $table) {
        $table->increments('id');
        $table->integer('friend_id')->unsigned()->index();
        $table->integer('user_id')->unsigned()->index();
        $table->foreign('friend_id')->references('id')->on('users')->onDelete('cascade');
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    });
}

2 个答案:

答案 0 :(得分:1)

结帐Parameters.jl。它有一个宏来做这个用更多的糖和更少的打字。我想那就是你要找的东西?

using Parameters

@with_kw type A
  a::Int = 6
  b::Float64 = -1.1
  c::UInt8
end

# Safe Way
function dxdt(x, p)
    @unpack a,b,c = p # This works on any type
    a*x^2 + b*x + c
end

# Easy Way
function dxdt(x, p)
    @unpack_A p # This only works on instances of A
    a*x^2 + b*x + c
end

答案 1 :(得分:1)

您的问题是如何定义p类型的变量Params? 对于a = 1.0,b = 2.0,c = 3.0:

p = Params(1.0,2.0,3.0)

可以通过以下方式使用:

type Params
       a::Float64
       b::Float64
       c::Float64
end

p = Params(1.0,2.0,3.0)

_unpack(p::Params) = (p.a, p.b, p.c)

function dxdt(x, p::Params)
       a, b, c = _unpack(p)
       a*x^2 + b*x + c
end

dxdt(1.0,p)
6.0

我认为这就是你要问的但我不确定。

相关问题