Skip to main content
4 of 6
added 98 characters in body
Kuba
  • 137.7k
  • 13
  • 289
  • 751

Self contained APIFunction. Collecting definitions

Cross posted on community.wolfram.com

Background

After failed attempt (1) to create a complex API, by nicely linking my APIFunction to a cloud based package, I decided to go with the flow and trust that:

CloudDeploy[expr, ...] automatically deploys all definitions needed to evaluate expr, much like CloudSave.

Problem

but either it can't be trusted or I'm missing the point. It seems that definitions are collected by scanning right-hand-sides of related definitions but not left-hand-sides.

Example

ClearAll[foo, check];
check = IntegerQ;
foo[x_?check] := $Failed;
foo[x_] := x^2

api = CloudDeploy[
    APIFunction[{"x" -> "Number"}, foo[#x] &]
  , Permissions -> "Public"
]
foo[2]     (* -> $Failed*)
foo[1.2]   (* -> 1.44 *)
URLExecute[api, {"x" -> 2}]    (* -> 4*) !!!!!!!!!!!
URLExecute[api, {"x" -> 1.2}]  (* -> 1.44*)

The first URLExecute is wrong, it is caused by missing definition of check.

One can confirm that by

Import[api, "Text"]
Language`ExtendedFullDefinition[] = Language`DefinitionList[ 
  HoldForm[foo] -> {..., DownValues -> {
    HoldPattern[foo[(x_)?check]] :> $Failed, 
    HoldPattern[foo[x_]] :> x^2
    }, ...
 }];
APIFunction[{"x" -> "Number"}, foo[#x] & ]

Questions

  • Is this expected? Are there any new guidelines how to write code so it could be collected well? Or is it a flaw in Language` ?

  • Is there anything I could read to learn working with the Cloud features in such a way that I won't have to come back here with a question/problem about a basic issue like deployment of dependencies?

Links

  1. Clean package update for API/FormFunctions on Wolfram Cloud

  2. Why my cloud application failed to display manipulate element correctly?

  3. CloudDeploy a Manipulate with dependent functions

Kuba
  • 137.7k
  • 13
  • 289
  • 751