by edgyswingset · 11y ago · 0 comments
It pays off in spades in a lot of situations, but the biggest I've noticed is Android development. Hitting a network from a button press in Xamarin is as simple as:
button.OnClick += async
{
var data = HitTheNetworkForSomeData();
// Note that since this is still async,
// you may want to set a global or have a
// different approach to handling data than
// "standard" Android development.
DoSomethingWithTheData(data);
}
In native Android, it usually looked like this: button.SetOnClickListener(new OnClickListener() {
new HitTheNetworkTask(this, this).execute()
});
@Override
public void onNetworkTaskDataRecieved(String data) {
doSomething(data);
}
public class HitTheNetworkTask extends AsyncTask<Void, Void, String> {
Context mContext;
NetworkDataTaskCallbacks mCallbacks;
public HitTheNetworkTask(Context context, NetworkDataTaskCallbacks callbacks) {
mContext = context;
mCallBacks = callbacks;
}
@Override
public String doInBackGround(Void... voids){ // I'm not pulling your leg here
return doTheNetworkTask();
}
@Override
public void onPostExecute(String data) {
callbacks.onNetworkTaskDataRecieved(data);
}
}
It's downright frustrating having to use Java once you're used to C# and .NET. public SearchViewModel(ISearchService searchService = null) : ReactiveObject, IRoutableViewHost
{
SearchService = searchService ?? Locator.Current.GetService<ISearchService>();
// Here we're describing here, in a *declarative way*, the conditions in
// which the Search command is enabled. Now our Command IsEnabled is
// perfectly efficient, because we're only updating the UI in the scenario
// when it should change.
var canSearch = this.WhenAny(x => x.SearchQuery, x => !String.IsNullOrWhiteSpace(x.Value));
// ReactiveCommand has built-in support for background operations and
// guarantees that this block will only run exactly once at a time, and
// that the CanExecute will auto-disable and that property IsExecuting will
// be set according whilst it is running.
Search = ReactiveCommand.CreateAsyncTask(canSearch, async _ => {
return await searchService.Search(this.SearchQuery);
});
// ReactiveCommands are themselves IObservables, whose value are the results
// from the async method, guaranteed to arrive on the UI thread. We're going
// to take the list of search results that the background operation loaded,
// and them into our SearchResults.
Search.Subscribe(results => {
SearchResults.Clear();
SearchResults.AddRange(results);
});
// ThrownExceptions is any exception thrown from the CreateAsyncTask piped
// to this Observable. Subscribing to this allows you to handle errors on
// the UI thread.
Search.ThrownExceptions
.Subscribe(ex => {
UserError.Throw("Potential Network Connectivity Error", ex);
});
// Whenever the Search query changes, we're going to wait for one second
// of "dead airtime", then automatically invoke the subscribe command.
this.WhenAnyValue(x => x.SearchQuery)
.Throttle(TimeSpan.FromSeconds(1), RxApp.MainThreadScheduler)
.InvokeCommand(this, x => x.Search);
}If you implement SynchronizationContext and set it, you can actually have the TPL call back into whatever special threading stuff you want.
It doesn't work for WPF bindings, but for the quick'n'dirty procedural UI code it works great.
[1]: https://msdn.microsoft.com/en-us/library/system.threading.sy...
Then check out http://arteksoftware.com/resilient-network-services-with-xam... for the pattern/inspiration how to glue them together to obtain a x-platform resilient, performant and data caching network stack. Write once, enjoy on every platform.
Having said that, more is possible with Java than many people realise. You can read statically available generic type params at runtime (field declarations, supertype tokens etc) . There are also workarounds for most of the erasure problems like methods that vary by generic type parameter only.
Java has some nice things itself too. Structural typing of single method interfaces/lambdas makes numerous things much easier. Default methods on interfaces let you do trait-like things nicer than with extension methods. Static imports reduce verbosity. More powerful enums etc.
c# approach to type inference seems to be to infer the left hand side of expressions with "var". Java infers types on the right hand side with the diamond operator, method type parameter inference, lambda to interface type inference. The Java approach makes code using fluent chained invocations less verbose, while the c# approach reduces the verbosity of assigning to lots of intermediate variables.
If ever, given their attitude on last year's Google IO.
One PITA for me is that whenever talks about various technologies happen on HN, I tend to comment on disadvantages, because it's the disadvantages that dictate the best use cases for that particular technology.
But whenever I do that for .NET / C#, there's like a circle of jerks on HN that down-vote everything that sounds bad about .NET.
That's fine, I had some hopes for Microsoft's .NET, now that it is finally being open-sourced and made multi platform, but when choosing a language, you're also choosing the ecosystem around it and this instance, amongst other interactions I've had, confirms that .NET is not for me and probably never will be.
Cheers,
This just isn't a JVM vs .NET competition. If Prolog were the best answer, you could port it to JVM or .NET to your heart's content.
For me Scala is more accessible and elegant than both C# and F# and in my opinion has less baggage.