# Add a specifier to namelist read that requires all members to be read

**URL:** <https://fortran-lang.discourse.group/t/add-a-specifier-to-namelist-read-that-requires-all-members-to-be-read/7578>\
**Category:** Language enhancement\
**Created:** [March 8, 2024, 11:52pm UTC](https://fortran-lang.discourse.group/t/add-a-specifier-to-namelist-read-that-requires-all-members-to-be-read/7578 "2024-03-08T23:52:02Z")\
**Posts on this page:** 5\
**Page:** 2

<div class="post-metadata">

**Author:** ![urbanjost](https://avatars.discourse-cdn.com/v4/letter/u/0ea827/32.png) [@urbanjost](https://fortran-lang.discourse.group/u/urbanjost)\
**Post date:** [June 18, 2024, 11:13pm UTC](https://fortran-lang.discourse.group/t/add-a-specifier-to-namelist-read-that-requires-all-members-to-be-read/7578/22 "2024-06-18T23:13:36Z")

</div>

I have wanted to do something like

```fortran
block
namelist/printme/a,b,c,d
write(*,nml=printme)
endblock

```

on multiple occasions, particularly for debug blocks I am conditionally using or even conditionally including via pre-processing.  
So I think that would very useful.

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [June 19, 2024, 2:33pm UTC](https://fortran-lang.discourse.group/t/add-a-specifier-to-namelist-read-that-requires-all-members-to-be-read/7578/23 "2024-06-19T14:33:14Z")

</div>

The namelist feature is indeed extremely useful, since it integrates directly with the language and it is very easy to load data from a human readable file directly into variables, and let the compiler check it.

However, as mentioned above, it has some fundamental limitations to truly live to its (modern) potential and support other formats like TOML, JSON, etc., allowing the user to implement a new format, and so on. Improving either namelist, or adding a new feature for that would be great.

Here is an issue to discuss this:

> <https://github.com/j3-fortran/fortran_proposals/issues/331>
>
> Originally discussed at
> 
> \* https://fortran-lang.discourse.group/t/introspectio…n-in-fortran-for-generic-file-i-o-libraries/
> \* https://fortran-lang.discourse.group/t/what-is-the-origin-of-namelist/6742/13
> 
> The idea is to use the simple and compiler-enforced syntax of namelist (or equivalent), but the compiler would call a user library that implements other formats, such as TOML, JSON, or custom binary array formats (say \[npy/npz\](https://numpy.org/doc/stable/reference/generated/numpy.lib.format.html#module-numpy.lib.format), \[GGUF\](https://github.com/ggerganov/ggml/blob/1bf5e231a9ffe7612522baa014644c768eba1a73/docs/gguf.md), \[safetensors\](https://github.com/huggingface/safetensors), etc.).
> 
> To be figured out is the exact design how this would work.
> 
> As an example how Rust approaches this problem: the \[toml\](https://docs.rs/toml/latest/toml/) library there allows you to just create a struct and decorate it:
> \`\`\`rust
> \#\[derive(Deserialize)\]
> struct Config {
> ip: String,
> port: Option\<u16\>,
> keys: Keys,
> }
> \`\`\`
> Then call it like this:
> \`\`\`rust
> let config: Config = toml::from\_str(r#"
> ip = '127.0.0.1'
> 
> \[keys\]
> github = 'xxxxxxxxxxxxxxxxx'
> travis = 'yyyyyyyyyyyyyyyyy'
> "#).unwrap();
> \`\`\`
> and it will just work.
> 
> A similar feature in Fortran might look like:
> \`\`\`fortran
> type(toml\_file) :: toml 
> type(mytype) :: t
> call toml%load(t, 'file.toml')
> \`\`\`
> 
> Or using the Fortran's namelist like syntax:
> \`\`\`fortran
> namelist / t / A, B, C
> open(newunit=u, file="file.toml", status="old", custom\_reader=toml\_file)
> read(u, t)
> \`\`\`
> And this allows you to implement a user derived type \`toml\_file\` that implements all the necessary capability to read a custom format, and then the line \`read(u, t)\` makes the compiler call your function/type bound procedures to actually handle the read.

---

<div class="post-metadata">

**Author:** ![ivanpribec](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/ivanpribec/32/3290_2.png) [@ivanpribec](https://fortran-lang.discourse.group/u/ivanpribec)\
**Post date:** [June 25, 2024, 12:11am UTC](https://fortran-lang.discourse.group/t/add-a-specifier-to-namelist-read-that-requires-all-members-to-be-read/7578/24 "2024-06-25T00:11:10Z")

</div>

> [@certik](#):
>
> However, as mentioned above, it has some fundamental limitations to truly live to its (modern) potential and support other formats like TOML, JSON, etc., allowing the user to implement a new format, and so on. Improving either namelist, or adding a new feature for that would be great.

Is there any reason why the namelist approach can’t be copied for (a subset of) JSON? Something like this is what I have in mind:

```txt
        ! JSON representation
        ! {"x": 1.2, "y": 2.5, "R": true, "name": "John Doe" }

        real(kind(1.0d0) :: x, y
        logical :: r
        character(len=:), allocatable :: name
        json /EXAMPLE/ x, (y,key="y"), (r,key="R"), (name,key="name")
        read(json=EXAMPLE,unit=my_unit)

```

Since JSON keys are case-sensitive or could contain UTF-8 characters, a separate `key` word is necessary. If omitted it could default to the name of the variable in lower-case (the limited [reflection](https://en.wikipedia.org/wiki/Reflective_programming) available in Fortran)

Admittedly, there are a few issues when it comes to JSON arrays which are allowed to contain different types and dictionaries (map these to derived types?).

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [June 25, 2024, 4:09am UTC](https://fortran-lang.discourse.group/t/add-a-specifier-to-namelist-read-that-requires-all-members-to-be-read/7578/25 "2024-06-25T04:09:07Z")

</div>

> [@ivanpribec](#):
>
> Is there any reason why the namelist approach can’t be copied for (a subset of) JSON?

I think it can be done. That’s the idea behind [Introspection in Fortran for generic file I/O libraries (TOML, JSON, NPZ, etc.) · Issue #331 · j3-fortran/fortran\_proposals · GitHub](https://github.com/j3-fortran/fortran_proposals/issues/331).

---

<div class="post-metadata">

**Author:** ![gronki](https://avatars.discourse-cdn.com/v4/letter/g/a88e4f/32.png) [@gronki](https://fortran-lang.discourse.group/u/gronki)\
**Post date:** [July 29, 2024, 5:24pm UTC](https://fortran-lang.discourse.group/t/add-a-specifier-to-namelist-read-that-requires-all-members-to-be-read/7578/26 "2024-07-29T17:24:09Z")

</div>

I realize it is only a cosmetic detail or my OCD, but whenever `/` appears (common block, namelists etc) I personally think it should be considered obsolete code, therefore I have never touched the namelists. It would be good to find some more modern syntax that would be consistent with the rest of the language. But it is not yet time for this kind of discussion!

[Previous page](https://fortran-lang.discourse.group/t/add-a-specifier-to-namelist-read-that-requires-all-members-to-be-read/7578.md?page=1)
