Forums

Extension Method Error: "static types...parameters"

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

Extension Method Error: "static types...parameters"

Postby oahmad04 » Sun Jan 19, 2014 5:55 pm

I get the following error:
Code: Select all
fixPath.cobra(12): error: static types cannot be used as parameters
Compilation failed - 1 error, 0 warnings
Not running due to errors above.


With the following code:
"""Extends Path to fix the slashes of a path"""

@platform clr
@args colorlib -color

namespace FixPath
"""Extends Path to fix the slashes of a path"""

extend Path
"""Extend with method to fix the slashes of a path"""

def fix(path as String) as String is shared
"""
Fix the slashes of the given path so it works on the current platform
"""

ensure result.length == path.length

test

assert Path.fix("") == ""
assert Path.fix(" ") == " "
assert Path.fix("File Name") == "File Name"
assert Path.fix("Path/Name") == Path.combine("Path", "Name")
assert Path.fix("Path\Name") == Path.combine("Path", "Name")
assert Path.fix("A\Path/File") == Path.combine("A", "Path", "File")

body

return .combine(path.split("/", "\"))


I have no idea what I did wrong. Any help would be appreciated.

The code I posted above isn't displayed properly for some reason. I entered the text correctly, but when I click preview I see "colorlib" instead of "-target:lib" and "\" instead of "\\". Huh?
oahmad04
 
Posts: 19

Re: Extension Method Error: "static types...parameters"

Postby Charles » Sun Jan 19, 2014 9:13 pm

Note that Cobra's extension methods, like C#'s, can only add instance methods and not static/shared. I have considered the idea of allowing shared extension methods as I think it's doable, but it hasn't been a high priority.

Regarding the poor error message, if you look at the Path class:
http://msdn.microsoft.com/en-us/library ... h(v=vs.110).aspx

You'll see that it's declared to be "static". All of its methods are static aka "shared" in Cobra. I think Cobra just tries to create your .fix extension method anyway and C# chokes on "Path" being a parameter. Basically getting an error is correct for the current feature set, but the error message is not useful.

Your current options include:

-- Create a "class PathEx" with a shared "fix" method and invoke with "PathEx.fix(p)"

-- Create "extend String" and add a "def fixPath as String" method; invoked with "p.fixPath".

-- Get hard core on the OO and create a "FilePath" class to represent file paths, having all the appropriate methods for them and making other types more readable, e.g., "List<of FilePath>" is more self documenting than "List<of String>".
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Extension Method Error: "static types...parameters"

Postby oahmad04 » Sun Jan 19, 2014 11:43 pm

Thanks for the explanation, and thanks even more for the suggestions. It's a little dissapointing that I can't extend Path with my static method, but at least there are good alternatives.
oahmad04
 
Posts: 19


Return to Discussion

Who is online

Users browsing this forum: No registered users and 31 guests