Periods class implementation in java 8 -
if code this:
period wrong = period.ofyears(1).ofweeks(1);
it gives output of p7d
.
by implementation of period
class know of____()
methods static.
but if same chaining datetime
class:
localdate date = localdate.of(2020, month.january, 20); localtime time = localtime.of(5, 15); localdatetime datetime = localdatetime.of(date, time) .minusdays(1).minushours(10).minusseconds(30);
all minus___()
, plus___()
methods instance methods in localdatetime
class.
question: why method chaining not allowed period
class?
why period
class isn't supporting that?
how internal assignment going on ?
you not chaining calls in first example.
period wrong = period.ofyears(1).ofweeks(1);
is same as:
period wrong = period.ofweeks(1);
in other words: object returned ofyears()
not affect result of ofweeks()
, it's year value discarded. invoking static method ofweeks()
. doing there not fluent call chain.
and decent ide should warn doing so. reason simple: "chaining" doesn't make sense!
the ofxyz()
calls create new period object you. object done , created. should semenatics of chaining ofxyz()
call onto existing period?
in other words: can't chain ofxyz()
calls because there no clear way express semantics such chain should have!
Comments
Post a Comment