Menu

#877 Add "/" operator for path concatenation to the File class

5.3.0
unread
nobody
None
none
1
2026-08-06
2026-08-03
No

Adding a / method to the File class would allow to write the following:

f = .File~new("/home/user/storage")
r = f / "examples" / "hello.rex" -- returns a new File object

This notation, similar to the one provided by Python's pathlib, is more readable than manual string concatenation and would automatically use the correct path separator on each platform.

Overloading of / should not cause any ambiguity, since neither a File object nor its string representation should ever be interpreted as a numeric string.

Discussion

  • Anonymous

    Anonymous - 2026-08-04

    From Jochem Peelen:
    Did you consider that in Windows the path separator is the \ (backslash)? The backslash is also the Rexx symbol for negating.

     
    • Salvador Parra Camacho

      Thank you for the response. I think there might be a slight misunderstanding of the proposal.

      I am not suggesting to use the backslash (\) as an operator in Rexx code (which, as you pointed out, is the unary operator for negation).

      Instead, the proposed binary / is strictly a path composition operator in code syntax across all operating systems. The resulting File object handles the OS-specific path separators internally when constructing the path string.

      As far as I know, this is the way Python's pathlib works on Windows.

      f = .File~new("dir")
      r = f / "subdir" / "hello.rex" -- returns a new File object
      

      the File object r would have the following path:

      • "dir/subdir/hello.rex" in Linux, Mac, BSD...
      • "dir\subdir\hello.rex" in Windows

      Here is a quick proof-of-concept class that implements this behavior:

      ::class Path
      
      ::method init
        expose file
        use arg name
        file = .File~new(name)
      
      ::method '/'
        expose file
        use arg str
        return .Path~new(file~path || .File~separator || str)
      
       

      Last edit: Salvador Parra Camacho 2026-08-04
  • Gil Barmwater

    Gil Barmwater - 2026-08-06

    Just a few thoughts on this proposal. I think that what threw me off initially was the choice for the operator - '/'. I'm a Windows guy so it struck me as a bit confusing at first. I think that perhaps a better choice might be '+' as it suggests that one is "adding" an element - folder or file name - to the path defining the .File object.

    With that in mind I have put together the following bit of code that you can use now pending the (possible) implementation of this feature request. Some things to note: the code can be inserted into a program that needs this behavior or placed into its own file which can then be included via ::requires. The PUBLIC keyword on the CLASS statement is NOT needed if the code is in the using program (but it doesn't hurt) but is needed if the code is in a file included via ::requires. Second, I named the class File so that you don't have to remember another classname when you wish to use this feature. As a result, it "appears" that the File class has gained a new '+' method but in reality, .File now refers to a subclass of the Rexx-supplied File class. Of course, you can choose to name the class whatever you wish. The same goes for the choice of the operator; any binary operator would work just as well as '+'.

    ::class file subclass rexx:file public
    
    ::method '+'
        p = self~absolutePath
        p_new = p || self~class~separator || arg(1)
        return self~class~new(p_new)
    
     

Anonymous
Anonymous

Add attachments
Cancel