logo
down
shadow

F# - Writing a function that accepts both non-generic and generic argument


F# - Writing a function that accepts both non-generic and generic argument

By : Dan35
Date : November 22 2020, 04:01 AM
it should still fix some issue Your original definition for await is fine.
The problem is with unitTask which is not generic. Make it generic the same way you did with stringTask:
code :
let unitTask = new Task<unit>(fun () -> printf "something")
let await     (task : Task<'a>) = task |> Async.AwaitTask |> Async.RunSynchronously
let awaitUnit (task : Task    ) = task |> Async.AwaitTask |> Async.RunSynchronously
type Task with 
    static member Await (task : Task    ) = task |> Async.AwaitTask |> Async.RunSynchronously
    static member Await (task : Task<'a>) = task |> Async.AwaitTask |> Async.RunSynchronously


Share : facebook icon twitter icon
How to declare function which accepts Generic AsyncTask as an argument

How to declare function which accepts Generic AsyncTask as an argument


By : Rory Cawley
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further I have many Activities and AsyncTasks in my application. I want to define an Interface with a method which accept a Generic AsyncTask and a boolean. I will implement that interface in all Activities and want to call the Interface's method from AsyncTask. , You can declare it this way:
code :
public void onUpdate(AsyncTask<?, ?, ?> task, boolean show) {

}
private class SomeTask extends AsyncTask<String, Integer, Long> {

    @Override
    protected Long doInBackground(String... params) {
        // TODO Auto-generated method stub
        return null;
    }

}
onUpdate(new SomeTask(), true);
Why can I not cast to a generic interface from a type with a generic argument which is a subtype of the generic argument

Why can I not cast to a generic interface from a type with a generic argument which is a subtype of the generic argument


By : user3342750
Date : March 29 2020, 07:55 AM
will be helpful for those in need The fact that you're not allowed to do this is perfectly logical from a type safety perspective. If an IController accepts BlergRequests in various methods (in your example, HandleRequest is one such method), it stands to reason that those methods might invoke members that are only available on a BlergRequest, and not on a Request. Thus you can't assign (or cast) an IController to an IController.
If on the other hand your interface was only returning BlergRequests, and never consumed them, then it could return a BlergRequest to any consuming code that required a Request. In such a case, your interface would be covariant in T, and you could mark the type parameter T with the covariant out modifier. You could then assign IController wherever an IController is needed.
method that accepts a generic class as argument in c#

method that accepts a generic class as argument in c#


By : عقلية نقية
Date : March 29 2020, 07:55 AM
To fix this issue You can make GetDataAsync() a generic method, GetDataAsync() but all parameters will be limited to whatever T happens to be when you call the method:
code :
GetDataAsync(..., new SqlCommandParameter<int>(), new SqlCommandParameter<int>());
GetDataAsync(..., new SqlCommandParameter<int>(), new SqlCommandParameter<string>());
public abstract class SqlCommandParameter
{
    public string ParameterName { get; private set; }
    public SqlDbType SqlDbType { get; private set; }

    protected SqlCommandParameters(string parameterName, SqlDbType sqlDbType)
    {
        ParameterName = parameterName;
        SqlDbType = sqlDbType;
    }
}

public class SqlCommandParameter<T>: SqlCommandParameter
{
    public T SqlDbValue { get; private set; }

    public SqlCommandParameter(string parameterName, SqlDbType sqlDbType, T sqlDbValue): base(parameterName, sqlDbType)
    {
        SqlDbValue = sqlDbValue;
    }
}
GetDataAsync(..., new SqlCommandParameter<int>(), new SqlCommandParameter<string>());
public Task<DataTable> GetDataAsync(
    int? id,
    string commandTextQuery,
    CommandType commandType,
    params SqlCommandParameter[] parameters )  { ... }
Writing a generic method with two possible sets of constraints on the generic argument

Writing a generic method with two possible sets of constraints on the generic argument


By : user2220838
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further If you need a method to handle reference types and a method to handle value types, that's a perfectly valid reason to have two methods.
What may help is to view this from the perspective of code that will call the methods in this class. From their perspective, do they benefit if they can call just one method regardless of the type instead of having to call one method for value types and another for value types? Probably not.
Problem writing a generic function that accepts any value that is RawRepresentable by a CustomStringConvertible

Problem writing a generic function that accepts any value that is RawRepresentable by a CustomStringConvertible


By : user3257616
Date : March 29 2020, 07:55 AM
Any of those help I'm trying to write a function that accepts any value that is RawRepresentable by a CustomStringConvertible. I tried writing this: , You should say that it conforms the protocol
code :
where R.RawValue: CustomStringConvertible 
enum MyEnum2: Int {
    case one = 1
}

myFunction(val: MyEnum2.one)
shadow
Privacy Policy - Terms - Contact Us © bighow.org