dogweather

dogweather

Following the Adding Pages tutorial, I addded a new route, without creating the controller:

get("/hello", HelloController, :index)

And this compiles w/ no warnings! Even after doing mix clean ; mix compile. However, I can get a compile error if I use an unknown lowercased keyword in router.ex:

snackTime

But the following inside a router.ex scope compiles fine:

SnackTime

Is there some kind of lack of type safety for module names?

Showing Posts 1 to 10

OvermindDL1

OvermindDL1

Well first there is no static typing in elixir. Second, module names are just atom, no different from any other atom:

iex(4)> Blah == :"Elixir.Blah"
true

So the only way to check it would be to actually call something on it (module_info/0 is a common thing to test). What the get is basically doing is just taking the atom you pass in for the module and the atom you pass in for the function (function names are just atoms too) and basically calling it like apply(moduleAtom, functionAtom, [conn, params]). :slight_smile:

dogweather

dogweather OP

Thanks!

I’m seeing the difference outside of the get(), however. E.g., after the defmodule’s end:

NonexistentThing  # No error
nonexistentThing  # error

Are you saying, that NonexistentThing is simply not evaluated? Optimized out before it’s linked?

(Indeed, I see that I get this behavior in a simple mix new project.)

Ankhers

Ankhers

Simply calling NonexistentThing isn’t doing anything. It is just an atom. There is nothing to evaluate. It does not know that you are trying to reference a module. It is just like any other literal. You would not get an error if you replaced it with 4. If instead you tried using NonexistentThing.foo, you should get an error stating that it does not exist.

On the other hand, nonexistentThing is trying to reference a variable that does not exist.

dogweather

dogweather OP

So it could be seen as a language syntax issue. I think it’d be hugely beneficial to get warnings for mis-spelled symbols. I hadn’t realized that an uppercase “bare word” is accepted as a valid expression.

dogweather

dogweather OP

I imagine that this alternate API would provide the kind of compiler checks I’m thinking about:

get("/hello", &HelloController.index/2)

…using capture to pass in an actual function instead of stringly typed values. TBH, I like it - it’s more obvious what get() is doing.

michalmuskala

michalmuskala

The problem with this syntax is that it would introduce a compile-time dependency on the HelloController module. You don’t want all of your controllers recompiled when you change the router.

peerreynders

peerreynders

The MFA pattern (module,function,arguments) is quite pervasive in BEAM languages. For example compare:
Kernel.spawn/3
Kernel.spawn/1

and as already mentioned
Kernel.apply/3

So while

get("/hello", HelloController, :index)

may look strange coming from other languages, it’s not unusual in OTP to assemble function invocations (or process initializations) as MFA triplets.

Not to be confused with the mfa() type which is actually {module(), atom(), arity()}.

NobbZ

NobbZ

Also, will this not create a lot of functions in the router module?

As far as I remember anonymous functions in the BEAM are just compiled into regular functions with a mangled name in the owning module?

dogweather

dogweather OP

Acutally, it’s all too familiar, coming from Ruby and Rails. I was hoping to get a little more help from the compiler when possible.

OvermindDL1

OvermindDL1

Hear hear! If only Elixir had static typing. ^.^;

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
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
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews