Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Type Witnesses -- just know they exist and their syntax and then forget about them?

 
Ranch Foreman
Posts: 175
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the life of me I can't figure out why I can't find the thread where I once asked about Type Witnesses and was instructed that they used to be marginally useful, but for reasons I don't remember have become just a Trivia Answer in Java 8 and beyond and we can pretty much forget about them as long as we don't freak out when we see them on exams or in older code.

They are mentioned in the 816/819 Sybex book, in that context...one answer choice to a mock question is:

Helper.<NullPointerException>printException( new NullPointerException ("D") );

Option D uses an odd syntax by explicitly listing the type, but you should be able to recognize it as acceptable.



Is that what I should be remembering?
"When you stick a <Classname> between the . after the name of a class (for calling a static method) or the reference and the name of the method you are calling from it, that's this old thing called a Type Witness.  It used to be marginally useful, but for reasons you don't need to know if you don't care for trivia, you basically don't need them anymore in Java 8.  Don't freak out if you see them in older code or on an exam"?
 
Marshal
Posts: 71682
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No idea where your old post went.
 
Jesse Silverman
Ranch Foreman
Posts: 175
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The default scope of the Search was more restricted in terms of Forum than I had realized when I hit Search.

My new default is to look and pay attention to which forums I am searching before I hit <Search>
 
Saloon Keeper
Posts: 12608
273
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If your goal is to get through life without getting lost in all the ugly details, then yes, you can mostly forget about type witnesses in Java. They are only very rarely useful, and even when they are, you can usually get rid of them by introducing intermediate variables to help Java's automatic type inference:

(Yes, I'm aware that Comparator also has the static reverseOrder() method that can be used instead of naturalOrder().reversed(), but for sake of argument let's pretend it doesn't exist.)

It's still nice to know about type witnesses in general though. Languages that have better runtime support for generics (such as C#) make hefty use of type witnesses (although I'm not sure they're called that way in those languages). Here's an example:
 
Jesse Silverman
Ranch Foreman
Posts: 175
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Stephan!

I tend to go for intermediate variables to make code clearer.
Not just to read, but to debug.

I have spent years of my life (well-paid, but otherwise lost) debugging

result = fun1( fun2( x + y, z) / fun3( w % average( c[0]  - fun4() ) ) );

when that code someone else wrote yielded a crash/stack trace "at line 791".

Not an edifying use of time.
 
Aaaaaand ... we're on the march. Stylin. Get with it tiny ad.
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic