|
| 1 | +# New COMPONENT keyword 'INHERIT' |
| 2 | + |
| 3 | +## Status |
| 4 | + |
| 5 | +*Proposed* and prototype *implemented* |
| 6 | + |
| 7 | +## Context |
| 8 | + |
| 9 | +As discussed in https://github.com/mccode-dev/McCode/discussions/2041, |
| 10 | +a recent addition of a 'multi-COPY for flexible mix and match of |
| 11 | +COMPONENT sections lead to different syntax / meaning of the COPY |
| 12 | +keyword in: |
| 13 | +* instruments: `COMPONENT COPY(instance) = COPY(instance)(par1=val1, par2=val2)` |
| 14 | +* component sections: `SHARE COPY PowderN COPY Fluorescence` |
| 15 | + |
| 16 | +## Decision |
| 17 | + |
| 18 | +In a zoom meeting on June 11th 2025 it was agreed between @farhi, |
| 19 | +@g5t, @mads-bertelsen, @willend to |
| 20 | +* Keep the `COPY` syntax in instrument files as is |
| 21 | +* Stop supporting `COPY` in components, and define a new `INHERIT` keyword for component section copying (mix |
| 22 | + and match as used in e.g. McXtrace`FluoPowder`) |
| 23 | +* Prototype implemented and ready for review in PR https://github.com/mccode-dev/McCode/pull/2062 |
| 24 | + |
| 25 | + |
| 26 | +## Consequences |
| 27 | + |
| 28 | +* Syntax becomes clearer - either `COPY(reference)` with parenthesis in |
| 29 | + instruments or `INHERIT reference` in components. |
| 30 | +* Existing components with use of `COPY` must be migrated (already |
| 31 | + done for repo comps in PR. |
| 32 | +* User home-grown McStas or McXtrace components will break and need |
| 33 | + migration to `INHERIT`. It is believed that very few - if any - such |
| 34 | + components exist in the wild. |
| 35 | +* Important: `INHERIT` does not ensure that component setting |
| 36 | + parameters used in copied section(s) are copied too. (This to allow |
| 37 | + complete modification of the parameter interface for a "derived" component.) |
| 38 | +* CHANGELOG should be crystal clear on this change / potential incompatibility |
| 39 | + |
| 40 | +## Behaviour |
| 41 | + |
| 42 | +For trivial components `n_part.comp`: |
| 43 | + |
| 44 | +``` |
| 45 | +DEFINE COMPONENT n_part |
| 46 | +SETTING PARAMETERS (int n) |
| 47 | +TRACE |
| 48 | +%{ |
| 49 | + for (int i= 0; i < n; i++) { |
| 50 | + printf("n=%d\\n", i); |
| 51 | + } |
| 52 | +%} |
| 53 | +END |
| 54 | +``` |
| 55 | +and `m_part.comp`: |
| 56 | + |
| 57 | +``` |
| 58 | +DEFINE COMPONENT m_part |
| 59 | +SETTING PARAMETERS (int m) |
| 60 | +TRACE |
| 61 | +%{ |
| 62 | + for (int i = 0; i < m; i++) { |
| 63 | + printf("m=%d\\n", i); |
| 64 | + } |
| 65 | +%} |
| 66 | +END |
| 67 | +``` |
| 68 | +a third component can now be defined pulling together sections from each, `both_parts.comp`: |
| 69 | + |
| 70 | +``` |
| 71 | +DEFINE COMPONENT both_parts |
| 72 | +SETTING PARAMETERS (int n, int m) |
| 73 | +TRACE INHERIT n_part INHERIT m_part |
| 74 | +END |
| 75 | +``` |
| 76 | +Using the third component in a trivial instrument, e.g., `test_both_parts.instr`: |
| 77 | + |
| 78 | +``` |
| 79 | +define instrument test_both_parts(dummy=0.) |
| 80 | +trace |
| 81 | +component origin = both_parts(n=3, m=2) at (0, 0, 0) absolute |
| 82 | +end |
| 83 | +``` |
| 84 | +will produce runtime output |
| 85 | + |
| 86 | +``` |
| 87 | +$ mcrun test_both_parts -n 1 dummy=0 |
| 88 | +n=0 |
| 89 | +n=1 |
| 90 | +n=2 |
| 91 | +m=0 |
| 92 | +m=1 |
| 93 | +``` |
0 commit comments