Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Retrofit advertise as "Type Safe" library?

Tags:

I was just going through their main page and it says,

A type-safe HTTP client for Android and Java

Why Retrofit advertises itself as being Type Safe while other libraries(many other popular ones) don't?

Before you answer...

There is an answer to this same question here. It says,

Type safety is the extent to which a programming language discourages or prevents type errors. A type error is erroneous or undesirable program behavior caused by a discrepancy between differing data types for the program's constants, variables, and methods (functions), e.g., treating an integer (int) as a floating-point number (float). This is common in statically typed languages such as Java and C

Thus Retrofit prevents errors of this type

If this is truly the answer then many libraries prevent these kinds of errors but none of them advertise as Type-Safe. Is it a marketing thing then?

I consider the above answer inadequate because the definition of Type Safety has not been taken seriously.

Anyway, there is another post with the definition of Type Safety. They give out examples:

Type safety means that the compiler will validate types while compiling, and throw an error if you try to assign the wrong type to a variable.

Some simple examples:

// Fails, Trying to put an integer in a string
String one = 1;
// Also fails.
int foo = "bar";

This also applies to method arguments, since you are passing explicit types to them:

int AddTwoNumbers(int a, int b)
{
    return a + b;
}

If I tried to call that using:

int Sum = AddTwoNumbers(5, "5");

As per the above definition, it would be the language(Java) and NOT the library that is specifically TypeSafe.

So, I ask, again, why does Retrofit advertise itself as a Type-Safe library?

like image 407
Manish Kumar Sharma Avatar asked Nov 25 '17 11:11

Manish Kumar Sharma


2 Answers

I haven't been thinking too much about it, but from the moment I have started using Retrofit I understood this headline as type-safe on higher abstraction layer than what others are talking about here.

Usually we are taking programming language as a "target" for being or not type-safe. And I think this is not the case for Retrofit's headline type-safety ;) If we pretend that a whole HTTP call is a single programming language instruction (which could have some params and the value), then we can indeed say that Retrofit is type-safe... you have strictly defined what kind of result you are getting.. and you are getting this or nothing/error. Of course the error is on runtime, as you can't never really know what will be retrieved from the internet. Of course many other libraries can do that, not only Retrofit. Of course you can mislead type-safety of Retrofit by defining service with return value of ResponseBody type (that could accept anything). But generally, out of the box, you are getting library which will check, parse, validate HTTP calls' responses for you, convert into proper types and in case of any issue - will give you an error.

Simple analogy I have in my head right now (in terms of type-safe slogan):

  • programming language has: instruction + arguments' & value's types

  • retrofit has: http call + body & response's structures

Best Regards, Darek

like image 69
Dariusz Wiechecki Avatar answered Sep 27 '22 19:09

Dariusz Wiechecki


Looking at the Retrofit project's cover page, it seems that the type in type safety refers to the request body and response body objects.

We have to remember that Retrofit is built over OKHttp, which can only handle RequestBody and ResponseBody objects. Retrofit prides itself on its ability to safely serialize these types into and from other types, using Converters.

The safety in type safety refers to Retrofit handling all the boilerplate code of ensuring correct type conversions while building RequestBody and parsing ResponseBody objects. Retrofit comes with several built in Converters which wrap around popular serialization libraries, such as GSon and Jackson.

TL;DR

The afformentioned type safety is Retrofit assuming the responsibility of bulding an HTTP request and parsing the HTTP response out of your DTOs.

You, the developer, may continue using your serialization library of choice as usual and need not worry your pretty little head on the subject.

like image 31
Vaiden Avatar answered Sep 27 '22 18:09

Vaiden