Swift’s Nil Coalescing Operator AKA the “?? Operator” or “Double Question Mark Operator”

This last week I was introduced to quite a funky little operator in Swift called the Nil Coalescing operator. It is basically a shorthand for the ternary conditional operator when used to test for nil.

For example traditionally in many programming languages you could do something like this to check for nil/null…

stringVar != nil ? stringVar! : "a default string"

Here if the condition is not nil the value of stringVar will be returned otherwise the value “a default string” is returned.

This can be shortened in Swift by using the Nil Coalescing operator.

stringVar ?? "a default string"

This does exactly the same thing, if stringVar is not nil then it is returned, however if it is nil the value “a default string” is returned.

Love it.

Technorati Tags: , , , , , , , , ,

How To Pass A Null Value To A Custom Tag Library

Today I found something very interesting.  I wanted to pass a java.math.BigDecimal to a custom JSP Tag Library and format it as a percentage, however if the BigDecimal was null I didn’t want to show it.

Easy, I thought. So I created a class similar to the following…

package name.kayley.blog.taglib;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;
import java.math.BigDecimal;

public class BigDecimalTagSupport extends TagSupport {

    private BigDecimal value;

    public void setValue(BigDecimal value) {
        this.value = value;
    }

    public int doEndTag() throws JspException {
        try {
            if (value != null) {
                pageContext
                        .getOut()
                        .print(formatAsPercentage(value));
            }
        } catch (IOException e) {
            throw new JspException(e);
        }
        return EVAL_PAGE;
    }
// ...
}

However, when I called this taglib with a null value I still got a value of 0.

<mytag:bigDecimal value="${aBigDecimal}" />

The output:
0

After a bit of trial and error I discovered that the JSP tab library framework does a little bit of magic on known classes, such as Strings and Numeric objects. Classes of Numeric types that are null get converted to 0 and Strings get converted into the empty string ""

I discovered that if you change your internal value to an java.lang.Object instead of a java.math.BigDecimal the magic doesn’t know what to do and just passes null to your class.

So the new class looks like the following…

package name.kayley.blog.taglib;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;

public class BigDecimalTagSupport extends TagSupport {

    private Object value;

    public void setValue(Object value) {
        this.value = value;
    }

    public int doEndTag() throws JspException {
        try {
            if (value != null) {
                // Cast value to a BigDecimal 
                // if you need to.
                pageContext
                        .getOut()
                        .print(formatAsPercentage(value));
            }
        } catch (IOException e) {
            throw new JspException(e);
        }
        return EVAL_PAGE;
    }
//...
}

So now when I use the tag and pass a null BigDecimal to it I get the required result of no output.

WIN

I have an example web app demonstrating this, if anyone wants a copy just let me know.

Technorati Tags: , , , , ,