soullpoint

soullpoint

Hi,

I was wondering if anyone could help clarify something for me. This is more due to my lack of understanding than anything else.

Suppose I had a module for testing, with two describe blocks. I declare a function with the same name in both, one with defaults, and one without. Perhaps owing to my OO background, I naively expected that these functions are in different scopes and therefore wouldn’t affect each other:

defmodule Sample.SampleTest do
  ...

  describe "test case A" do
    alias AnotherModule
    def helper_function(attrs \\ %{}) do
      ...
    end
  end

  describe "test case B" do
    alias AnotherModule
    def helper_function() do
      ...
    end
  end
end

However, I am getting an error that helper_function/0 conflicts with defaults from helper_function/1.

I presume that the functions are therefore scoped to the containing module? But then, I also have aliases within the describe blocks which don’t appear to be shared. (If I don’t include the alias, it fails to compile if I call a function from the module). How are these different?

If anyone could help me understand this it would be much appreciated.

Showing Posts 1 to 4

dimitarvp

dimitarvp

AFAIK, neither describe nor test define a new module scope (or class as you’d call it). So what you are trying to do will not work. What’s the problem with having functions with different names at the top scope in the same file?

soullpoint

soullpoint OP

There’s no problem with it; left to my own devices, I probably would have done just that.

I encountered this sort of pattern when I ran a generator for Phoenix for two models under the same context; not having much experience with the language, I simply thought this was a common pattern and tried to maintain that with the new functions I introduced.

soullpoint

soullpoint OP

To answer my own question, the function is not “scoped” to the describe, because as dimitarvp mentioned, describe does not define a new module scope.

The alias however, is scoped to the nearest block, and the describe is one such construct, so the alias is not “shared” between the two blocks.

Just trying to understand the fundamentals. I feel like this makes sense, but please feel free to correct me if I’m wrong!

zorbash

zorbash

@soullpoint There is a way to scope them if the functions are used in setups:

describe "test case A" do
    setup :build_user
    
    @describename elem(@ex_unit_describe, 1)
    def build_user(%{describe: @describename}) do
      [user: %{user_id: 1}]
    end
  end

  describe "test case B" do
    setup :build_user

    @describename elem(@ex_unit_describe, 1)
    def build_user(%{describe: @describename}) do
      [user: %{user_id: 2}]
    end
  end
— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews