merge_list_DTs <- function(list_DTs, by=NULL, all=FALSE, all.x=all, all.y=all, suffixes=sprintf(".%02i", seq_along(list_DTs)), allow.cartesian=getOption("datatable.allow.cartesian"), ...) {
  ## TODO: Suffixes needs to rotate somehow...
  ##        by argument.  What if some have keys and some do not? 

  ## Sanity check on list_DTs input
  ## ---------------------------------------- ##
  if (is.data.table(list_DTs))
    return(list_DTs)
  if (!is.list(list_DTs))
    stop ("list_DTs is not a list")
  
  stopifnot(sapply(list_DTs, is.data.table))
  ## ---------------------------------------- ##

  if (length(list_DTs) == 1) {
    warning ("only a single data.table was in list_DTs")
      return(list_DTs[[1]])
  }

  ## ----------------------------------------------- ##
  ## EXPAND THE ARGUMENTS 
  ## ----------------------------------------------- ##
    expected_length <- length(list_DTs) - 1

    if (length(suffixes) == expected_length + 1) {
      suffixes.list <- list()
      for (i in seq.int(expected_length))
        suffixes.list %<>% c(list(suffixes[c(i, i+1)]))
    } else {
      suffixes.list <- as.list(suffixes) 
    }

    if (!is.null(by) && !is.list(by))
      by <- list(by)

    repeat_if_1 <- . %>% {if (is.null(.)) list(.) else .} %>% {L <- length(.); if (L == expected_length) . else if (L == 1) rep(., each=expected_length) else if (L != expected_length) {warning ("expected length is ", expected_length, " - acutal length is ", L); .} }
    ## ^^^^ using each, for suffxes for now

    by %<>% repeat_if_1
    all.x %<>% repeat_if_1
    all.y %<>% repeat_if_1
    suffixes.list %<>% repeat_if_1
    allow.cartesian %<>% repeat_if_1
  ## ----------------------------------------------- ##

  i <- 1
  DT.ret <- list_DTs[[1]]
  browser(text="before merging in merge_list_DTs", expr=inDebugMode("merger", "merge_list_DTs"))

  for (i in seq.int(expected_length))
    DT.ret <- data.table:::merge.data.table(DT.ret, list_DTs[[i+1]], by=by[[i]], all.x=all.x[[i]], all.y=all.y[[i]], suffixes=suffixes.list[[i]], allow.cartesian=allow.cartesian[[i]], ...)

  return(DT.ret)
}


## FOR DEBUGGING
if (FALSE) {
  DT.country[labelid == 894]
  DT.ret <- DT.ret[labelid == 894]

  data.table:::merge.data.table(DT.ret, list_DTs[[i+1]], by=by[[i]], all.x=all.x[[i]], all.y=all.y[[i]], suffixes=suffixes.list[[i]], allow.cartesian=allow.cartesian[[i]], ...)
  data.table:::merge.data.table(DT.ret, list_DTs[[i+1]][labelid == 894], by=by[[i]], all.x=all.x[[i]], all.y=all.y[[i]], suffixes=suffixes.list[[i]], allow.cartesian=allow.cartesian[[i]], ...)
}


## ---------------------------------------------------------------------------------------------- ##

mergeDTlist <- function(DTlist, suffixes=NULL, checkKeys=TRUE
                       , all=TRUE, all.x=all, all.y=all) {

## This is a very old function;;  I wrote merge_list_DTs() at least a year or two after, having forgotten about mergeDTlist()
  

warning ("use merge_list_DTs() instead of the older mergeDTlist() ")

  ## TODO:  Allow for DTlist to be the names of the table, by using `get()` further down in the code

  # DTlist should be an actual list of DT's.  If instead it is names, use `lapply(.., get)`
  if(all(sapply(DTlist, is.character)) && all(sapply(DTlist, length)==1)) {
    en <- parent.frame()
    DTlist <- lapply(DTsToMerge, get, envir=en)
  }

  ## check the keys 
  if (checkKeys) {
    intersection <- Reduce(intersect, lapply(DTlist, key))
    if (identical(character(0), intersection))
      stop("Cannot automatically merge this list of DTs. No shared key amongst the DTs")
    # else
    checkKeys <- FALSE 
  }

  ## set suffixes
  if (is.null(suffixes)) {
    suffixes <- paste0(".", fw0(length(DTlist)))
  }


  if (length(DTlist)==1)
    return(DTlist)
  if (length(DTlist)==2)
    return(merge(DTlist[[1]], DTlist[[2]], suffixes=suffixes, all=all, all.x=all.x, all.y=all.y))

  # if there are more than 2 elements, merge the 2nd into the 1st, and iterate
  DTlist[[1]] <- merge(DTlist[[1]], DTlist[[2]], suffixes=suffixes, all=all, all.x=all.x, all.y=all.y)
  DTlist[[2]] <- NULL
  suffixes <- suffixes[-2]

  return(mergeDTlist(DTlist, suffixes=suffixes, checkKeys=checkKeys, all=all, all.x=all.x, all.y=all.y))
}
