I’d like to create a list that I may need to add values to, but I’m getting an error when I try to append to it. “error: use of uninitialized value” - which I think means, I’ve declared the variable, but haven’t initialized it.
Here is the code (there may be other issues with the code, I’m still learning, but one problem at a time )
@value
struct MountInfo:
var dev_type: String
var device: String
var mount_point: String
def get_nfs_cifs_mounts() -> Optional[List[MountInfo]]:
path = Path('/proc/mounts')
var device_list: List[MountInfo] # I think I need to do something here to initialize it.
try:
with open(path, 'r') as _file:
var contents_str: String = _file.read()
var contents_list: List[String] = contents_str.split('\n')
for line in contents_list:
var clean_line = String(line[].strip()).lower()
if clean_line.find('nfs') != -1 or clean_line.find('nfs4') != -1:
var device_type: String = 'NFS'
parts = clean_line.split()
if len(parts) >= 2:
_mnt = MountInfo(device_type, parts[0], parts[1])
device_list.append(_mnt) # Error is popping up on this line
elif clean_line.find('cifs') != -1:
device_type = 'CIFS'
parts = clean_line.split()
if len(parts) >= 2:
_mnt = MountInfo(device_type, parts[0], parts[1])
device_list.append(_mnt)
return device_list
except:
print('Unable to open ', path)
return None
If I change the declaration line to:
var device_list: List[MountInfo] = []
I get: cannot implicitly convert 'ListLiteral[]' value to 'List[MountInfo]'
Is there a way to initialize an empty list as if to say, I may need to add things to you later, but for now you’re empty.