gpt4 book ai didi

function - Elixir 函数有最大的 Airty 吗?

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

函数可以接受的参数数量是否有上限?这不计算模式匹配。例如。像 fn ([arg1, arg2, arg3], arg4) 这样定义的函数仅算作 2。

如果没有上限,Elixir 是否也支持可变参数函数?

最佳答案

Elixir 运行在 Erlang 之上,其最大数量似乎为 255请参阅此页面:http://erlang.org/doc/reference_manual/typespec.html并向下搜索包含该行的表: arity() 0..255

确认更新:尝试编译具有 255 个参数的函数是可行的。

尝试编译具有 256 个参数的函数会出现以下编译错误:

== Compilation error in file arity.ex ==
** (CompileError) arity.ex:3: too many arguments (256) - maximum allowed is 255
(stdlib) lists.erl:1338: :lists.foreach/2
(stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
** (CompileError) compile error
(iex) lib/iex/helpers.ex:183: IEx.Helpers.c/2

使用 c "arity.ex" 在 Windows10 x64 上的 Elixir Shell 中完成测试,其中 arity.ex 的内容(加上或减去参数)为:

defmodule Test do
def arity(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17, p18, p19, p20, p21, p22, p23, p24, p25, p26, p27, p28, p29, p30, p31, p32, p33, p34, p35, p36, p37, p38, p39, p40, p41, p42, p43, p44, p45, p46, p47, p48, p49, p50, p51, p52, p53, p54, p55, p56, p57, p58, p59, p60, p61, p62, p63, p64, p65, p66, p67, p68, p69, p70, p71, p72, p73, p74, p75, p76, p77, p78, p79, p80, p81, p82, p83, p84, p85, p86, p87, p88, p89, p90, p91, p92, p93, p94, p95, p96, p97, p98, p99, p100, p101, p102, p103, p104, p105, p106, p107, p108, p109, p110, p111, p112, p113, p114, p115, p116, p117, p118, p119, p120, p121, p122, p123, p124, p125, p126, p127, p128, p129, p130, p131, p132, p133, p134, p135, p136, p137, p138, p139, p140, p141, p142, p143, p144, p145, p146, p147, p148, p149, p150, p151, p152, p153, p154, p155, p156, p157, p158, p159, p160, p161, p162, p163, p164, p165, p166, p167, p168, p169, p170, p171, p172, p173, p174, p175, p176, p177, p178, p179, p180, p181, p182, p183, p184, p185, p186, p187, p188, p189, p190, p191, p192, p193, p194, p195, p196, p197, p198, p199, p200, p201, p202, p203, p204, p205, p206, p207, p208, p209, p210, p211, p212, p213, p214, p215, p216, p217, p218, p219, p220, p221, p222, p223, p224, p225, p226, p227, p228, p229, p230, p231, p232, p233, p234, p235, p236, p237, p238, p239, p240, p241, p242, p243, p244, p245, p246, p247, p248, p249, p250, p251, p252, p253, p254, p255, p256 ) do
IO.puts( "hello world" )
end
end

关于function - Elixir 函数有最大的 Airty 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56046200/

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