manywaypark's Blog
개발, 검색, 함수

Function has no local return

함수형 언어/Elixir 2020. 2. 27. 20:22 by manywaypark

dialyzer(discrepancy analyzer)는 Erlang의 static analyzer(정적분석도구)이다. type safety가 확보되면 아주 많은 오류를 줄일 수 있다. 그런데 이 좋은 도구인 dialyzer를 사용하다보면 언듯 보기에 멀쩡한 부분에서 "function foo/2 has no local return" 이라는 경고를 만나게 된다.

일단 참고 아티클에서 발췌:

'Function Name/Arity has no local return' is the standard Dialyzer warning emitted whenever a function provably doesn't return anything (other than perhaps raising an exception) because one of the function it calls happens to trip Dialyzer's type error detector or raises an exception itself. When such a thing happens, the set of possible types of values the function could return is empty; it doesn't actually return. This error propagates to the function that called it, giving us the 'no local return' error.

대충 요약하자면 return이 없거나 영원이 return되지 않을 것(crash 등)으로 추측되는 경우 dialyzer에서 이 경고가 나온다는 말인데...

내 경험상 해결 방법은 다음 방법들 중에 하나를 쓰면 된다. 

  1. dialyer에게 type inference hint를 준다. 즉, @spec을 제대로 써주면 해결됨 (dialyer는 좁은 범위를 제시하면 훨씬 똑똑하게 동작함)
  2. error, exception, raise 등이 발생하는 경우(1차적으로 눈에 보이지 않더라도 call stack을 따라 전파됨) return type에 명시적으로 return이 없을 수 있다고 명시한다.
    - Erlang은 no_return()
    - Elixir는 RuntimeError를 try rescue/catch 등의 방법으로 처리

 

ref: https://learnyousomeerlang.com/dialyzer#typing-about-types-of-types

 

Type Specifications and Erlang | Learn You Some Erlang for Great Good!

Hey there, it appears your Javascript is disabled. That's fine, the site works without it. However, you might prefer reading it with syntax highlighting, which requires Javascript! Type Specifications and Erlang Back in Types (or lack thereof), I introduce

learnyousomeerlang.com

happy hackin'

module을 정의할 때 attribute, directive 등의 순서는 아래와 같이 정리하면 좋다.

@moduledoc
@behaviour
use
import
alias
require
@module_attribute
defstruct
@type
@callback
@macrocallback
@optional_callbacks

 

ref: https://github.com/christopheradams/elixir_style_guide#module-attribute-ordering

 

christopheradams/elixir_style_guide

A community driven style guide for Elixir. Contribute to christopheradams/elixir_style_guide development by creating an account on GitHub.

github.com

happy hackin'

참고 링크에 있는 것을 요약한다.


  • broadcast - channel내의 모든 client들에게 전송
  • broadcast_from - 메시지를 발송한 client를 제외한 channel내의 모든 client들에게 전송
  • push - 메시지를 발송한 client에게만 전송

다르게 생각해보면 broadcast = broadcast_from + push


ref: https://www.reddit.com/r/elixir/comments/4t6k6w/phoenix_what_is_the_difference_between_broadcast/


happy hackin'

@impl true

함수형 언어/Elixir 2018. 7. 12. 12:42 by manywaypark

현재 내가 사용하는 elixir version은 1.6.5이다.

1.5.0의 changelog에 나오는 내용이므로 1.5.0 이상의 버전에서는 다 적용될 것으로 예상됨.


callback의 구현임을 명시적으로 알려줄 때 "@impl true"를 함수의 앞에 써준다.


sample:


defmodule MyApp do

  @behaviour Plug


  @impl true

  # 이 함수 `init/1`은 문서화에서 제외된다. 암묵적으로 @doc false로 설정된다.

  def init(_opts) do

    opts

  end


  @impl true

  @doc """

  여기 내용은 그대로 문서화에 적용됨

  see `Plug.call/2`

  """

  def call(conn, _opts) do

    Plug.Conn.send_resp(conn, 200, "hello world")

  end

end



- callback이 아닌 함수에 @impl 사용하면 에러.

- 어느 하나의 함수에 @impl을 적용했다면 그 모듈 내의 모든 callback에서 @impl을 써주지 않은 함수에 대해 경고.


ref: https://elixir-lang.org/blog/2017/07/25/elixir-v1-5-0-released/


happy hackin'


named argument 사용

함수형 언어/Elixir 2017. 9. 11. 12:38 by manywaypark

대충 map을 이용하면 되는데... 좀더 깔끔한 방법이 있다.

자세한 것은 참조 링크 참조.

결론은 아래처럼 NamedArgs를 mix.exs 의존성에 추가하고 필요한 모듈에서 use NamedArgs만 추가하면 끝.

  def deps do

    [

      {:named_args, "~> 0.1.0"}

    ]

  end

사용은 이렇게...

defmodule Talk do

  use NamedArgs

  def introduction(opts \\ [name: "Sarah", birthday: "1985-12-30"]) do

    IO.puts "Hi my name is #{opts[:name]} and I was born on #{opts[:birthday]}"

  end

end


refs:

https://blog.praveenperera.com/named-arugments-with-default-values-in-elixir/

https://github.com/mgwidmann/named_args


happy hackin'

Elixir에서 ABNF 사용하기

함수형 언어/Elixir 2017. 5. 23. 17:24 by manywaypark

ABNF 사용하기 - 아래 글 참조


http://marcelog.github.io/articles/abnf_grammars_in_elixir.html

https://github.com/marcelog/ex_abnf


happy hackin'


Ubuntu가 15.04 부터는 systemd만 온전히 지원하므로, 현재 이전의 upstart를 사용하는 방법은 별로 권장되지 않는다. 


아래 페이지 참고하면 systemd를 이용해서 설정할 수 있을 것이다.

http://mfeckie.github.io/Phoenix-In-Production-With-Systemd/


happy hackin'

Elixir 창시자의 변

함수형 언어/Elixir 2017. 2. 13. 10:23 by manywaypark

좀 오래된 글이지만 창시자의 꼬드김(?) 스크랩.

Concurrent and Distributed Programming with Erlang and Elixir: part1, part2


happy hackin'

함수이름 마지막에 마침표를 찍어야 anonymous function(일반적으로 변수에 저장된) 함수를 invoke할 수 있는 것은 좀 이상하다.

Joe 아저씨도 의문을 표시한 적이 있구나.

https://groups.google.com/forum/#!topic/elixir-lang-core/Zop_x5K85XE

namespace를 분리하든가 scoping을 활용하든가 하면 좀더 깔끔한 방법이 있을 듯도 한데...


happy hackin'

1 
분류 전체보기 (306)
잡담 (20)
함수형 언어 (65)
emacs (16)
java (18)
tips & tricks (154)
사랑 (1)
가사 (0)
독서 (4)
mobile (6)
비함수형 언어 (2)

공지사항

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

04-20 04:50