Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forcing specific data types as arguments to a function

Tags:

r

I was just wondering if there was a way to force a function to only accept certain data types, without having to check for it within the function; or, is this not possible because R's type-checking is done at runtime (as opposed to those programming languages, such as Java, where type-checking is done during compilation)?

For example, in Java, you have to specify a data type:

class t2 {
    public int addone (int n) {
        return n+1;
    }
}

In R, a similar function might be

addone <- function(n)
{
    return(n+1)
}

but if a vector is supplied, a vector will (obviously) be returned. If you only want a single integer to be accepted, then is the only way to do to have a condition within the function, along the lines of

addone <- function(n)
{
  if(is.vector(n) && length(n)==1)
  {
    return(n+1)
  } else
  {
    return ("You must enter a single integer")
  }
}

Thanks,
Chris

like image 313
ChrisW Avatar asked Aug 21 '11 17:08

ChrisW


People also ask

How do you specify data types in a Python function argument?

Python is a strongly-typed dynamic language in which we don't have to specify the data type of the function return value and function argument. It relates type with values instead of names. The only way to specify data of specific types is by providing explicit datatypes while calling the functions.

Which data types can be passed as arguments to a function?

An argument refers to values that are passed within a function when the calling of a function takes place. Furthermore, specifying argument data types is important for C++ programming. Moreover, C++ supports three types of argument data types – pass by value, pass by reference, and pass by pointer.

Do function parameters require data types?

The parameter is only accessible within the scope of the function where it's supplied. As you create a list of parameters, you must assign a data type to each parameter upon its declaration. This is no different than declaring a variable anywhere else in your code.

Can you specify data types in Python?

Python is an object-orientated language, and as such it uses classes to define data types, including its primitive types.


1 Answers

This is entirely possible using S3 classes. Your example is somewhat contrived in the context or R, since I can't think of a practical reason why one would want to create a class of a single value. Nonetheless, this is possible. As an added bonus, I demonstrate how the function addone can be used to add the value of one to numeric vectors (trivial) and character vectors (so A turns to B, etc.):

Start by creating a generic S3 method for addone, utlising the S3 despatch mechanism UseMethod:

addone <- function(x){
  UseMethod("addone", x)
}

Next, create the contrived class single, defined as the first element of whatever is passed to it:

as.single <- function(x){
  ret <- unlist(x)[1]
  class(ret) <- "single"
  ret
}

Now create methods to handle the various classes. The default method will be called unless a specific class is defined:

addone.default <- function(x) x + 1
addone.character <- function(x)rawToChar(as.raw(as.numeric(charToRaw(x))+1))
addone.single <- function(x)x + 1

Finally, test it with some sample data:

addone(1:5)
[1] 2 3 4 5 6

addone(as.single(1:5))
[1] 2
attr(,"class")
[1] "single"

addone("abc")
[1] "bcd"

Some additional information:

  1. Hadley's devtools wiki is a valuable source of information on all things, including the S3 object system.

  2. The S3 method doesn't provide strict typing. It can quite easily be abused. For stricter object orientation, have a look at S4 classes, reference based classesor the proto package for Prototype object-based programming.

like image 127
Andrie Avatar answered Nov 16 '22 02:11

Andrie