Skip to content

custom error message for missing default constructor #29

@stephanos

Description

@stephanos

I recently forgot to define a default constructor (when using reflection) and had a hard time discovering what I did wrong. The error message was "next on empty iterator" coming from:

def newInstance[T](clazz: Class[T])(op: T => Unit): T = {
    val constructor =
      clazz.getDeclaredConstructors.filter(_.getParameterTypes.length == 0).head

Maybe you could turn it into something like:

def newInstance[T](clazz: Class[T])(op: T => Unit): T = {
    val constructor =
            clazz.getDeclaredConstructors.filter(_.getParameterTypes.length == 0)
                    .headOption.getOrElse(sys.error("no default constructor found on " + clazz))

This way it would be much easier to find the reason for the exception!

Cheers!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions