Replies: 4 comments 1 reply
-
If the compiler infers that In this case you have a If you instead use this on your Note also, that if you're calling public async Task<Option<string>> CreateFolder(string folderName, string parentFolderId = "root") =>
GetSubfolders(parentFolderId)
.BindT(folders => CreateNewFolder(folderName, parentFolderId, folders)); I'm using |
Beta Was this translation helpful? Give feedback.
-
@louthy Thanks for the reply and the explanation, but unfortunately your suggestions didn't work. If I change the
If I change it to just
If I copy your
Any idea what's going wrong? To make it easier to understand, I've produced some dummy methods and a stripped-down version of the async Task<string> CreateNewFolder(string folderName, string parentFolderId, List<DriveFile> folders) {
if (folders.Any()) {
// Folder already exists with that name so return the Id
return folders.First().Id;
}
// In reality we would create a folder here and return its Id
return "new";
}
// In reality we check if a folder exists with the specified Id, and if not return `None`
// For simplicity, we'll just return `Some`
async Task<Option<List<DriveFile>>> GetSubfolders(string folderId) =>
Option<List<DriveFile>>.Some(new List<DriveFile> { new DriveFile("Jim") });
class DriveFile {
public DriveFile(string id) =>
Id = id;
public string Id { get; set; }
} Any chance you could show me how the Here's what I have so far (before implementing any of your suggestions)... public async Task<Option<string>> CreateFolder(string folderName, string parentFolderId = "root") =>
(await GetSubfolders(parentFolderId))
.Match(async folders => await CreateNewFolder(folderName, parentFolderId, folders), Option<string>.None); Thanks again. P.S. Apologies about the bold text after the third exception. Not sure why it did that. |
Beta Was this translation helpful? Give feedback.
-
@louthy Just as an after-thought, am I even doing this the right way? I have a few similar functions, which return an It occurred to me that Which do I use? Or should I be using something else entirely? Please could you show me how to create my Thanks again. |
Beta Was this translation helpful? Give feedback.
-
@StefanBertels Thanks for the reply. I re-thought out what I am trying to do, and realised that I was doing it wrong anyway. I have something that now works, please can you tell me if you think this looks sensible... async Task<Either<Exception, string>> CreateFolder(string folderName, string parentFolderId = "root") =>
await (await GetSubfolders(parentFolderId))
.Match(async folders => await CreateNewFolder(folderName, parentFolderId, folders), ex => throw ex);
async Task<Either<Exception, string>> CreateNewFolder(string folderName, string parentFolderId, List<DriveFile> folders) {
if (folders.Any()) {
// Folder already exists with that name so return the Id
return folders.First().Id;
}
// In reality we would create a folder here and return its Id. Creating a folder could throw an exception
return "new";
}
async Task<Either<Exception, List<DriveFile>>> GetSubfolders(string folderId) {
// Simulate what would happen if we passed a blank (or invalid) Id to Google Drive
if (folderId == "") {
throw new ArgumentException($"Invalid folderId: {folderId}");
}
return new List<DriveFile>(new List<DriveFile> { new DriveFile("Jim"), new DriveFile("John") });
} Can this be improved at all? Thanks |
Beta Was this translation helpful? Give feedback.
-
As usual, I'm sure this is really obvious to the enlightened, but I'm not, so please humour me!
I have a method that creates a folder in Google Drive. That works fine, but the current version doesn't check if a folder of the same name exists, and will create another with the same name. I know Google allows this, but my requirement is not to.
So, I want to change my method to check if a folder exists with the given name, and if so, return the
Id
of that folder. If a folder doesn't exist, then we'll create a new one and return theId
of the new folder.I have an existing method that gets the subfolders of a specified folder...
This returns
Some<List<DriveFile>>
if a folder exists with theId
offolderId
andNone<List<DriveFile>>
if not.I was expecting to be able to use this as follows...
However, I get compiler errors on the three
return
lines...I've done some searching on this error, but don't really understand what's going on here. I have returned values from
Match
before, and not had this issue.Anyone able to explain what I'm doing wrong? Thanks.
Update If I extract the code that does the actual folder creation to a separate method as follows...
...then I'm down to one compiler error on the
Option<string>.None
at the end...OK, so let's change the code to this...
That changes the compiler error to...
So I'm baffled.
Beta Was this translation helpful? Give feedback.
All reactions