From 065b7f3f8ffb98ba714f01b38737bc752fb92ee4 Mon Sep 17 00:00:00 2001 From: Eric Meadows-Jonsson Date: Wed, 5 Jun 2013 16:05:11 +0200 Subject: [PATCH] Document super and add more docs to defoverridable --- lib/elixir/lib/kernel.ex | 37 +++++++++++++++++++++----- lib/elixir/lib/kernel/special_forms.ex | 12 +++++++-- lib/elixir/lib/module.ex | 3 ++- 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/lib/elixir/lib/kernel.ex b/lib/elixir/lib/kernel.ex index dad0f0d57b..4fc6f66489 100644 --- a/lib/elixir/lib/kernel.ex +++ b/lib/elixir/lib/kernel.ex @@ -1379,13 +1379,13 @@ defmodule Kernel do file_info = FileInfo.new(atime: now()) file_info.atime #=> Returns the value of atime file_info.atime(now()) #=> Updates the value of atime - + # Update multiple attributes at once: file_info.update(atime: now(), accesses: 1) - + # Obtain the keywords representation of a record: file_info.to_keywords #=> [accesses: 1, atime: {1370,7171,911705}] - + A record is simply a tuple where the first element is the record module name. We can get the record raw representation as follow: @@ -1835,9 +1835,34 @@ defmodule Kernel do end @doc """ - Makes the given functions in the current module overridable. - An overridable function is lazily defined, allowing a - developer to customize it. + Makes the given functions in the current module overridable. An overridable + function is lazily defined, allowing a developer to customize it. + + ## Example + + defmodule DefaultMod do + defmacro __using__(_opts) do + quote do + def test(x, y) do + x + y + end + + defoverridable [test: 2] + end + end + end + + defmodule InheritMod do + use DefaultMod + + def test(x, y) do + x * y + super + end + end + + As seen as in the example `super` can be used to call the default + implementation, if no arguments are given to `super` it will be implictly + given the arguments of the current function. """ defmacro defoverridable(tuples) do quote do diff --git a/lib/elixir/lib/kernel/special_forms.ex b/lib/elixir/lib/kernel/special_forms.ex index e911cc4ef0..8198b28497 100644 --- a/lib/elixir/lib/kernel/special_forms.ex +++ b/lib/elixir/lib/kernel/special_forms.ex @@ -24,7 +24,7 @@ defmodule Kernel.SpecialForms do ## Examples - :{}.(1,2,3) + iex> { 1, 2, 3 } { 1, 2, 3 } """ @@ -35,7 +35,7 @@ defmodule Kernel.SpecialForms do ## Examples - :[].(1,2,3) + iex> [ 1, 2, 3 ] [ 1, 2, 3 ] """ @@ -810,4 +810,12 @@ defmodule Kernel.SpecialForms do """ defmacro __aliases__(args) + + @doc """ + Calls the overriden function when overriding it with `defoverridable`. If no + arguments are given to `super` the overriden function will be called with the + same arguments as the current one. See `Kernel.defoverridable` for more + information and documentation. + """ + defmacro super(args) end diff --git a/lib/elixir/lib/module.ex b/lib/elixir/lib/module.ex index c90a50a643..3f71cf6dda 100644 --- a/lib/elixir/lib/module.ex +++ b/lib/elixir/lib/module.ex @@ -362,7 +362,8 @@ defmodule Module do @doc """ Makes the given functions in the given module overridable. An overridable function is lazily defined, allowing a - developer to customize it. + developer to customize it. See `Kernel.defoverridable` for + more information and documentation. """ def make_overridable(module, tuples) do assert_not_compiled!(:make_overridable, module)