Login or Sign Up to become a member!
LessThanDot Site Logo

LessThanDot

A Technical Community for IT Professionals

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Highly Rated Users

Forum
No Posts Rated

Top 50
Given
Received

Forum Statistics

Users
Members:
1879
Members Online:
1
Guests Online:
24

Total Post History
Posts:
81448
Topics:
18714

7-Day Post History
New Posts:
0
New Topics:
0
Active Topics:
0

Our newest member
mwojcik

Other

FAQ
All times are UTC [ DST ]

Google Ads

"Lazy" Property Access Without Conditionals

Mind Boggling Puzzles, to keep that grey matter in shape...
Forum rules
Always post answers in a "Hidecode" tag, so that others have a chance to answer the question too.
Please wait...

"Lazy" Property Access Without Conditionals

Postby AlexCuse on Fri Apr 02, 2010 1:26 pm

I saw this on a company's job page the other day, figured I'd post it here (names changed to protect the innocent) since Thirster is always complaining about the lack of puzzles ;)

  1. abstract class SomeClass<T> {
  2.   private boolean obtained;
  3.   private T value;
  4.   public T get() {
  5.     if (!obtained) {
  6.       value = goGet();
  7.       obtained = true;
  8.     }
  9.     return value;
  10.   }
  11.   abstract protected T goGet();
  12. }


The rules are pretty simple, implement a class that will behave the same way without any boolean logic. The result should be something you could reasonably expect to work for all types that T could represent.

I guess it is a pretty silly puzzle, but no sillier than the others that get posted here :)
Say what you like about the tenets of National Socialism Dude, at least it's an ethos
User avatar
AlexCuse
LTD Admin
LTD Admin
LTD Gold - Rating: 1031LTD Gold - Rating: 1031LTD Gold - Rating: 1031LTD Gold - Rating: 1031LTD Gold - Rating: 1031
LTD Gold - Rating: 1031LTD Gold - Rating: 1031LTD Gold - Rating: 1031LTD Gold - Rating: 1031LTD Gold - Rating: 1031
LTD Gold - Rating: 1031
 
Posts: 5523
Joined: Tue Oct 09, 2007 5:26 pm
Location: Pennsylvania, US
Unrated