Reputation: 21
I have an issue with calling statfs() from Swift. The call uses as a parameter a struct also named statfs, and the swift compiler seems to get the two confused.
This code tells me that the struct is uninitialized so it fails to compile:
var sb : statfs
if(statfs(fd, &sb) == 0)
{
//...
}
But when I try to initialize the struct (I've tried "var sb : statfs = statfs()" and "var sb : statfs = statfs(f_bsize: 0, f_iosize: 0, ...)" , it tells me the parameters don't match and it appears to be looking for the parameters of the function instead of the struct.
So, I'm guessing there must be some syntactical sugar I am missing to get the compiler to realize it should be looking at the struct and not the function.
I have a similar naming issue that is more of an inconvenience: I wanted to have a class with method named open() that called the posix command open(), but I couldn't get the compiler to realize I wanted to call the posix command instead of my own method. I got around this by simply renaming my method, but there must be some syntax to let the compiler know which item you mean when multiple items of the same name exist.
Upvotes: 2
Views: 976
Reputation: 1637
The problem is that Swift imports both function name and struct name as statfs
. You can disambiguate them via typealias.
/// universal initializer
func blankof<T>(type:T.Type) -> T {
var ptr = UnsafeMutablePointer<T>.alloc(sizeof(T))
var val = ptr.memory
ptr.destroy()
return val
}
import Darwin
typealias StatFS = statfs // this does the trick
var fs = blankof(StatFS)
println(statfs("/", &fs))
println(fs.f_ffree)
Upvotes: 1
Reputation: 14795
And on your second question (should you make it a separate one? ;-):
I wanted to have a class with method named open() that called the posix command open(), but I couldn't get the compiler to realize I wanted to call the posix command instead of my own method.
Try this:
func open(x, y) {
Darwin.open(x, y)
}
Like in here: https://github.com/AlwaysRightInstitute/SwiftSockets/blob/master/ARISockets/Socket.swift#L99
Upvotes: 0