Le mardi 09 février 2016 à 20:28 -0800, Michael Landis a écrit :
> missed a paren above (for the people that are going to past the code into a
> shell and try it out) - something that I am not doing. Still, this is closer:
>
> # wishful thinking...
> using Dates;
> leapDay = isleapyear(yr) ? Date(yr,2,29) : nothing
> if ! leapDay
> doy = dayofyear( leapDay )
> ... clean and concise (thought that was the point), but we get
>
> leapDay = isleapyear (yr)? Nullable {Date} (Date (yr, 2:29)): Nullable
> {Date} ()
> if ! isnull( leapDay )
> doy = dayofyear( get(leapDay) )
First, you don't need to specify {Date} when passing a value, as the
type can be inferred:
leapDay = isleapyear(yr) ? Nullable(Date(yr, 2:29)) : Nullable{Date}()
Note that you can also rely on implicit conversion to write this as:
leapDay::Nullable{Date} = isleapyear(yr) ? Date(yr, 2:29) : nothing
My not-so-secret dream is that you would be able to write this like in
C# and Swift as:
leapDay::Date? = isleapyear(yr) ? Date(yr, 2:29) : nothing
Then, regarding the if block, writing leapDay != nothing or
!isnull(leapDay) is the same IMHO. What's a bit annoying is the call to
get(). Again, I wish we would be allowed to write leapDay? instead of
get(leapDay). Other than that, one could easily write a macro similar
to what Joshua proposes in his post. Let us know if you'd find it
useful.
Regards
> On Tue, Feb 9, 2016 at 8:22 PM, Michael Landis wrote:
> > # wishful thinking...
> > using Dates;
> > leapDay = isleapyear(yr) ? Date(yr,2,29) : nothing
> > if ! leapDay
> > dow = dayofyear( leapDay )
> > ... clean and concise (thought that was the point), but we get
> >
> > leapDay = isleapyear(yr) ? Nullable{Date}( Date(yr,2,29) : Nullable{Date}()
> > if ! isnull( leapDay )
> > dow = dayofyear( get(leapDay) )
> > ...
> >
> > If I am dumb enough to forget to check for a null date, I deserve the
> > exception - the code would be wrong. Making me type two or three times as
> > many characters, obscuring what is actually going on, ... all to eliminate
> > NullPointerExceptions? I have to write exception free code anyway, so all
> > I have 'gained' is a lot of superfluous verbosity. I'm going to side with
> > salience over verbosity every time. The type safe argument just doesn't
> > sell me, sorry.
> >