Shop OBEX P1 Docs P2 Docs Learn Events
SPIN2 namespaces — Parallax Forums

SPIN2 namespaces

Hey folks (especially spin2 toolchain developers: @cgracey @ersmith @Wuerfel_21 @macca )
How would you feel about the idea of extending the namespaces support in DAT blocks to the rest of a spin2 file? It feels like with some of the other features added to spin2 in recent years (like structures especially, but also interfaces in flexspin), that we're a hop, skip and a jump away from being able to have separate namespaces within the same spin2 file (of course I could be wrong!).

To illustrate: currently in spin2, a driver for an IMU chip (so, some combination of: accelerometer, gyroscope, magnetometer, barometer/altimeter) might be written such that methods for each of those "sub-sensors" that do the same thing (for example a data ready signal):

' parent object
obj imu: "imu-sensor-driver.spin2"

pub main() | ax, ay, az

if ( imu.accel_data_ready() )
    imu.accel_data(@ax, @ay, @az)
' etc...for gyro, or others
' child object: imu-sensor-driver.spin2
pub accel_data_ready(): r ' called like: imu.accel_data_ready()
pub gyro_data_ready(): r ' called like: imu.gyro_data_ready()
pub mag_data_ready(): r ' called like: imu.mag_data_ready()

etc. Basically any method relevant only to a specific logical block of the chip might be named with the appropriate prefix.
With namespaces, all of the methods that were relevant to those particular logical blocks within the chip might be rewritten something like this:

' parent object
obj imu: "imu-sensor-driver.spin2"

pub main() | ax, ay, az

if ( imu.accel.data_ready() )
    imu.data(@ax, @ay, @az)
' etc...for gyro, or others
' child object: imu-sensor-driver.spin2
%namespace accel ' change namespace to accel until it's changed again
pub data(x, y, z) ' this would be called from the parent object like imu.accel.data(@x, @y, @z)
pub data_ready(): r ' imu.accel.data_ready()
pub interrupt_flags(): f ' imu.accel_interrupt_flags()

%namespace gyro
pub data(x, y, z) ' imu.gyro.data(@x, @y, @z)
pub data_ready(): r ' imu.gyro.data_ready()
pub interrupt_flags(): f ' imu.gyro.interrupt_flags()

%namespace mag
pub data(x, y, z) ' imu.mag.data(@x, @y, @z)
pub data_ready(): r ' imu.mag.data_ready()
pub interrupt_flags(): f ' imu.mag.interrupt_flags()

This wouldn't be a replacement for child object files - it'd just provide the same separation they currently do, but for cases where you wouldn't really want to have to put things in separate files.

Is this something that could be completely resolved by the compiler or would it add some runtime bloat? I know child object files carry a small amount of runtime memory overhead. Hopefully this at least wouldn't be any worse.

This is mostly a "would be nice to have" thing, certainly not in my mind a necessity. Maybe people used to languages with similar features (C++? Py?) would see it as a more killer feature if they were considering spin2, not sure.

Thanks for reading!

Comments

  • Wuerfel_21Wuerfel_21 Posts: 5,744
    edited 2025-12-07 00:24

    The compiler could totally flatten a feature like this on the frontend.
    Wether it's a good idea is another question.

    The DAT namespaces themselves are a bit of an immature feature. I championend for them early this year, then kind of fell down a hole and was busy with other things, so I never got around to properly using them. Infact, flexspin's implementation still has a major problem, as I just now discovered...

  • ersmithersmith Posts: 6,231

    @avsa242 It feels like you could probably do what you want with sub-objects; the only disadvantage would be that you'd need additional files, but if the hardware really is that different maybe that's a good thing to separate them?

    @Wuerfel_21 Aargh! That bug should be fixed now, thanks for the bug report.

    And to both of you (and everyone): my e-mail has been silently broken for some weeks now, so if I haven't replied to your messages it's because I didn't see them :(.

  • @ersmith

    Yes it should definitely be possible with separate child object files - I just meant to ask about the ability to have the same kind of logical separation within the same file; just syntactic sugar (I think this is what is being accomplished in languages like C++ or Python when you define/use different namespaces in a source file, but I could be mistaken about how it works, or the underlying intent of namespaces). No big thing if this is a thumbs down - I had a feeling it'd be something that might not see a lot of use by most people. I just wondered if it would've been easy to add and if it would add little/no overhead to the code produced.

  • Wuerfel_21Wuerfel_21 Posts: 5,744
    edited 2025-12-08 23:21

    @ersmith said:

    @Wuerfel_21 Aargh! That bug should be fixed now, thanks for the bug report.

    Big thanks. Unfortunately, to quote Master Yoda, "There is another!".

    And to both of you (and everyone): my e-mail has been silently broken for some weeks now, so if I haven't replied to your messages it's because I didn't see them :(.

    My email has recently started to send all forum notifications to the spam folder. Really awesome stuff.

Sign In or Register to comment.