v2 SplitPath: also return object with path parts as properties

Propose new features and changes
neogna2
Posts: 600
Joined: 15 Sep 2016, 15:44

v2 SplitPath: also return object with path parts as properties

30 Oct 2022, 06:17

Let v2 SplitPath also return an object with all path parts as properties. I think that would enable more readable code when using a single path segment in a oneliner expression for example. Code below illustrates the wished for feature. Currently SplitPath doesn't return anything so we can alternatively do something that looks a bit similar (also illustrated below). But to my eyes at least that code style is less intuitive compared to using a property. A drawback with my suggestion is that it would break that alternative use.

Code: Select all

SplitPathObj(Path, &Name?, &Dir?, &Ext?, &NameNoExt?, &Drive?) {
    SplitPath(Path, &Name, &Dir, &Ext, &NameNoExt, &Drive)
    return {Name: Name, Dir: Dir, Ext: Ext, NameNoExt: NameNoExt, Drive: Drive}
}
Path := "C:\folder\file.txt"
;suggested behaviour
MsgBox SplitPathObj(Path).Drive                     ;C:\
MsgBox SplitPathObj(Path, &MyVar).Dir "|" MyVar     ;C:\folder|file.txt
if SplitPathObj(Path).Ext = "txt"
    MsgBox "textfile"
;current alternative
MsgBox SplitPath(Path,,,,, &Drive) Drive              ;C:
MsgBox SplitPath(Path, &MyVar, &Dir) Dir "|" MyVar    ;C:\folder|file.txt
if SplitPath(Path,,, &Ext) Ext = "txt"
    MsgBox "textfile"
edit: Lexikos' similar idea in Thoughts for v2.0
SplitPath could return an object when no output parameters are passed. Property names are probably easier to remember than parameter order, and easier to read and write than counting commas. This should have no negative impact on compatibility if added later, presuming scripts do not rely on SplitPath returning "".
I prefer the always return object variant but both have benefits.
Always returning an object (my idea) lets us do SplitPathObj(Path, &MyVar).Dir "|" MyVar
Returning an object only if no output parameters (Lexikos) does not break existing code like MsgBox SplitPath(Path,,,,, &Drive) Drive
User avatar
RaptorX
Posts: 395
Joined: 06 Dec 2014, 14:27
Contact:

Re: v2 SplitPath: also return object with path parts as properties

13 Nov 2022, 11:11

I find it a bit annoying that it doesnt do it.

I was expecting it to behave as StrSplit and those guys in the sense that they return an array.
But an object/map might be a better idea.

Hopefully we get rid of passing var refs to functions to get values from them and have a standard way of getting data in the form of objects/arrays.

This would definitely be awesome:

Code: Select all

path := SplitPath(A_AhkPath)
Projects:
AHK-ToolKit

Return to “Wish List”

Who is online

Users browsing this forum: Bing [Bot] and 33 guests