Is there any way to make an Apex method parameter lazy?Get Total Permutations of child objects in a list of...
What is the draw frequency for 3 consecutive games (same players; amateur level)?
How bad is a Computer Science course that doesn't teach Design Patterns?
How to extract specific values/fields from the text file?
How can find the 2D Voronoi cell area distribution?
Single-row INSERT...SELECT much slower than separate SELECT
Context html export bibliography
Is it possible to detect 100% of SQLi with a simple regex?
Renting a 2CV in France
Critique vs nitpicking
Are all power cords made equal?
Boss asked me to sign a resignation paper without a date on it along with my new contract
How can I prevent an oracle who can see into the past from knowing everything that has happened?
How do you get out of your own psychology to write characters?
Case protection with emphasis in biblatex
Buying a "Used" Router
How do I add a strong "onion flavor" to the biryani (in restaurant style)?
Is `Object` a function in javascript?
Co-worker sabotaging/undoing my work (software development)
How much light is too much?
Democratic Socialism vs Social Democracy
Is there any danger of my neighbor having my wife's signature?
What species should be used for storage of human minds?
Taking an academic pseudonym?
The No-Straight Maze
Is there any way to make an Apex method parameter lazy?
Get Total Permutations of child objects in a list of listsProcessInstanceWorkitems not created when running an Approval Process unit testWill SFDC manufacture batches from concurrent operations?Order of execution of controller method calls during rerenderCalling a method with List<string> Ids parameterIs there an easy way to sync the Default Account Team of an User to all accounts he owns by APEX?Is there any way to LIKE a chatter post in the context of another user?Need a Help to Convert Workflow Formula field into ApexIs there a Salesforce method to validate Remote Objects Query Criteria and turn it into dynamic SOQL?Is applying a LIMIT to a Count() query an optimization?
Let's say I want to call a method like:
doSomething(a.b.c.d.e.f());
I know that this might be an expensive operation and a, a.b, a.b.c, a.b.c.d, and a.b.c.d.e might all be null, however I would rather deal with all these possibilities within doSomething()
is there any way I can defer that operation until I want/need it?
(I'm 99% certain the answer is 'no', but I hope to learn that I'm wrong.)
apex null-pointer method lazy-loading
add a comment |
Let's say I want to call a method like:
doSomething(a.b.c.d.e.f());
I know that this might be an expensive operation and a, a.b, a.b.c, a.b.c.d, and a.b.c.d.e might all be null, however I would rather deal with all these possibilities within doSomething()
is there any way I can defer that operation until I want/need it?
(I'm 99% certain the answer is 'no', but I hope to learn that I'm wrong.)
apex null-pointer method lazy-loading
This Idea would help, though not as nice as having a shortcut like?
null checks available in .Net.
– sfdcfox
14 mins ago
add a comment |
Let's say I want to call a method like:
doSomething(a.b.c.d.e.f());
I know that this might be an expensive operation and a, a.b, a.b.c, a.b.c.d, and a.b.c.d.e might all be null, however I would rather deal with all these possibilities within doSomething()
is there any way I can defer that operation until I want/need it?
(I'm 99% certain the answer is 'no', but I hope to learn that I'm wrong.)
apex null-pointer method lazy-loading
Let's say I want to call a method like:
doSomething(a.b.c.d.e.f());
I know that this might be an expensive operation and a, a.b, a.b.c, a.b.c.d, and a.b.c.d.e might all be null, however I would rather deal with all these possibilities within doSomething()
is there any way I can defer that operation until I want/need it?
(I'm 99% certain the answer is 'no', but I hope to learn that I'm wrong.)
apex null-pointer method lazy-loading
apex null-pointer method lazy-loading
asked 3 hours ago
Brian KesslerBrian Kessler
1,6431131
1,6431131
This Idea would help, though not as nice as having a shortcut like?
null checks available in .Net.
– sfdcfox
14 mins ago
add a comment |
This Idea would help, though not as nice as having a shortcut like?
null checks available in .Net.
– sfdcfox
14 mins ago
This Idea would help, though not as nice as having a shortcut like
?
null checks available in .Net.– sfdcfox
14 mins ago
This Idea would help, though not as nice as having a shortcut like
?
null checks available in .Net.– sfdcfox
14 mins ago
add a comment |
1 Answer
1
active
oldest
votes
Not exactly, but you can do something like lazy evaluation by making use of the Callable
interface (or rolling your own). Here's an example using Callable
...
To start with, a test:
@IsTest private class LazyTest {
@TestSetup static void setup() {
List<Account> accounts = new List<Account> {
new Account(Name = 'A'),
new Account(Name = 'B')};
insert accounts;
List<Contact> contacts = new List<Contact> {
new Contact(LastName = '1', AccountId = accounts[0].Id),
new Contact(LastName = '2', AccountId = accounts[1].Id)};
insert contacts;
}
@IsTest static void testBehavior() {
Callable lazyContacts = new LazyContacts(new LazyAccounts('A'));
// No calls made yet, so I can pass this around at no great cost
System.assertEquals(0, Limits.getQueries());
// OK, I've decided I need those contacts now...
List<Contact> results = (List<Contact>)lazyContacts.call(null, null);
System.assertEquals(2, Limits.getQueries());
System.assertEquals(1, results.size());
System.assertEquals('1', results[0].LastName);
}
}
In the test, lazyContacts
is not yet the Contacts I'm looking for. It's like a Callable
that will provide the Contacts when I actually need them. It's parameterised by another Callable
, so that inner one is also not invoked until you decide you actually need it.
The actual implementations:
public with sharing class LazyContacts implements Callable {
private Callable lazyAccounts;
public LazyContacts(Callable lazyAccounts) {
this.lazyAccounts = lazyAccounts;
}
public Object call(String param1, Map<String, Object> param2) {
return [
SELECT LastName
FROM Contact
WHERE AccountId IN :(List<SObject>)lazyAccounts.call(null, null)
];
}
}
public with sharing class LazyAccounts implements Callable {
private String accountName;
public LazyAccounts(String accountName) {
this.accountName = accountName;
}
public Object call(String param1, Map<String, Object> param2) {
return [
SELECT Id
FROM Account
WHERE Name = :accountName
];
}
}
You could certainly put together something like that in Apex. And you can do similar with iterators - only iterating when the data is actually requested.
The only downside is that Apex is missing a few language niceties that exist in Java and would make the code more succinct (and type-safe if we had templates).
Thanks for the response. Interesting approach. I'm not really sure what "Callable" brings to this conversation since, a you point out, we could roll our own interface and do so in a way that doesn't have unused parameters and could have greater type safety... Also doesn't solve the problem of a, a.b, etc possibly being null (but I guess that probably isn't solvable in Apex)
– Brian Kessler
1 hour ago
The advantage of callable is that it exists already, so no extra dependencies if you're using unlocked packaging. And it does keep everything very loosely coupled. It seems like you're asking two questions, really: 1. How can I defer evaluation? A: Use callable or something similar 2. How can I avoid lots of null-checks. 1 seems like the bigger problem to me, so that's what I answered. 2 can be done by replacing null with an appropriate actual object e.g. an empty list, or string, or a Null Object. Then make sure you don't do expensive things if the incoming data doesn't need them
– Aidan
54 mins ago
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "459"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f251558%2fis-there-any-way-to-make-an-apex-method-parameter-lazy%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Not exactly, but you can do something like lazy evaluation by making use of the Callable
interface (or rolling your own). Here's an example using Callable
...
To start with, a test:
@IsTest private class LazyTest {
@TestSetup static void setup() {
List<Account> accounts = new List<Account> {
new Account(Name = 'A'),
new Account(Name = 'B')};
insert accounts;
List<Contact> contacts = new List<Contact> {
new Contact(LastName = '1', AccountId = accounts[0].Id),
new Contact(LastName = '2', AccountId = accounts[1].Id)};
insert contacts;
}
@IsTest static void testBehavior() {
Callable lazyContacts = new LazyContacts(new LazyAccounts('A'));
// No calls made yet, so I can pass this around at no great cost
System.assertEquals(0, Limits.getQueries());
// OK, I've decided I need those contacts now...
List<Contact> results = (List<Contact>)lazyContacts.call(null, null);
System.assertEquals(2, Limits.getQueries());
System.assertEquals(1, results.size());
System.assertEquals('1', results[0].LastName);
}
}
In the test, lazyContacts
is not yet the Contacts I'm looking for. It's like a Callable
that will provide the Contacts when I actually need them. It's parameterised by another Callable
, so that inner one is also not invoked until you decide you actually need it.
The actual implementations:
public with sharing class LazyContacts implements Callable {
private Callable lazyAccounts;
public LazyContacts(Callable lazyAccounts) {
this.lazyAccounts = lazyAccounts;
}
public Object call(String param1, Map<String, Object> param2) {
return [
SELECT LastName
FROM Contact
WHERE AccountId IN :(List<SObject>)lazyAccounts.call(null, null)
];
}
}
public with sharing class LazyAccounts implements Callable {
private String accountName;
public LazyAccounts(String accountName) {
this.accountName = accountName;
}
public Object call(String param1, Map<String, Object> param2) {
return [
SELECT Id
FROM Account
WHERE Name = :accountName
];
}
}
You could certainly put together something like that in Apex. And you can do similar with iterators - only iterating when the data is actually requested.
The only downside is that Apex is missing a few language niceties that exist in Java and would make the code more succinct (and type-safe if we had templates).
Thanks for the response. Interesting approach. I'm not really sure what "Callable" brings to this conversation since, a you point out, we could roll our own interface and do so in a way that doesn't have unused parameters and could have greater type safety... Also doesn't solve the problem of a, a.b, etc possibly being null (but I guess that probably isn't solvable in Apex)
– Brian Kessler
1 hour ago
The advantage of callable is that it exists already, so no extra dependencies if you're using unlocked packaging. And it does keep everything very loosely coupled. It seems like you're asking two questions, really: 1. How can I defer evaluation? A: Use callable or something similar 2. How can I avoid lots of null-checks. 1 seems like the bigger problem to me, so that's what I answered. 2 can be done by replacing null with an appropriate actual object e.g. an empty list, or string, or a Null Object. Then make sure you don't do expensive things if the incoming data doesn't need them
– Aidan
54 mins ago
add a comment |
Not exactly, but you can do something like lazy evaluation by making use of the Callable
interface (or rolling your own). Here's an example using Callable
...
To start with, a test:
@IsTest private class LazyTest {
@TestSetup static void setup() {
List<Account> accounts = new List<Account> {
new Account(Name = 'A'),
new Account(Name = 'B')};
insert accounts;
List<Contact> contacts = new List<Contact> {
new Contact(LastName = '1', AccountId = accounts[0].Id),
new Contact(LastName = '2', AccountId = accounts[1].Id)};
insert contacts;
}
@IsTest static void testBehavior() {
Callable lazyContacts = new LazyContacts(new LazyAccounts('A'));
// No calls made yet, so I can pass this around at no great cost
System.assertEquals(0, Limits.getQueries());
// OK, I've decided I need those contacts now...
List<Contact> results = (List<Contact>)lazyContacts.call(null, null);
System.assertEquals(2, Limits.getQueries());
System.assertEquals(1, results.size());
System.assertEquals('1', results[0].LastName);
}
}
In the test, lazyContacts
is not yet the Contacts I'm looking for. It's like a Callable
that will provide the Contacts when I actually need them. It's parameterised by another Callable
, so that inner one is also not invoked until you decide you actually need it.
The actual implementations:
public with sharing class LazyContacts implements Callable {
private Callable lazyAccounts;
public LazyContacts(Callable lazyAccounts) {
this.lazyAccounts = lazyAccounts;
}
public Object call(String param1, Map<String, Object> param2) {
return [
SELECT LastName
FROM Contact
WHERE AccountId IN :(List<SObject>)lazyAccounts.call(null, null)
];
}
}
public with sharing class LazyAccounts implements Callable {
private String accountName;
public LazyAccounts(String accountName) {
this.accountName = accountName;
}
public Object call(String param1, Map<String, Object> param2) {
return [
SELECT Id
FROM Account
WHERE Name = :accountName
];
}
}
You could certainly put together something like that in Apex. And you can do similar with iterators - only iterating when the data is actually requested.
The only downside is that Apex is missing a few language niceties that exist in Java and would make the code more succinct (and type-safe if we had templates).
Thanks for the response. Interesting approach. I'm not really sure what "Callable" brings to this conversation since, a you point out, we could roll our own interface and do so in a way that doesn't have unused parameters and could have greater type safety... Also doesn't solve the problem of a, a.b, etc possibly being null (but I guess that probably isn't solvable in Apex)
– Brian Kessler
1 hour ago
The advantage of callable is that it exists already, so no extra dependencies if you're using unlocked packaging. And it does keep everything very loosely coupled. It seems like you're asking two questions, really: 1. How can I defer evaluation? A: Use callable or something similar 2. How can I avoid lots of null-checks. 1 seems like the bigger problem to me, so that's what I answered. 2 can be done by replacing null with an appropriate actual object e.g. an empty list, or string, or a Null Object. Then make sure you don't do expensive things if the incoming data doesn't need them
– Aidan
54 mins ago
add a comment |
Not exactly, but you can do something like lazy evaluation by making use of the Callable
interface (or rolling your own). Here's an example using Callable
...
To start with, a test:
@IsTest private class LazyTest {
@TestSetup static void setup() {
List<Account> accounts = new List<Account> {
new Account(Name = 'A'),
new Account(Name = 'B')};
insert accounts;
List<Contact> contacts = new List<Contact> {
new Contact(LastName = '1', AccountId = accounts[0].Id),
new Contact(LastName = '2', AccountId = accounts[1].Id)};
insert contacts;
}
@IsTest static void testBehavior() {
Callable lazyContacts = new LazyContacts(new LazyAccounts('A'));
// No calls made yet, so I can pass this around at no great cost
System.assertEquals(0, Limits.getQueries());
// OK, I've decided I need those contacts now...
List<Contact> results = (List<Contact>)lazyContacts.call(null, null);
System.assertEquals(2, Limits.getQueries());
System.assertEquals(1, results.size());
System.assertEquals('1', results[0].LastName);
}
}
In the test, lazyContacts
is not yet the Contacts I'm looking for. It's like a Callable
that will provide the Contacts when I actually need them. It's parameterised by another Callable
, so that inner one is also not invoked until you decide you actually need it.
The actual implementations:
public with sharing class LazyContacts implements Callable {
private Callable lazyAccounts;
public LazyContacts(Callable lazyAccounts) {
this.lazyAccounts = lazyAccounts;
}
public Object call(String param1, Map<String, Object> param2) {
return [
SELECT LastName
FROM Contact
WHERE AccountId IN :(List<SObject>)lazyAccounts.call(null, null)
];
}
}
public with sharing class LazyAccounts implements Callable {
private String accountName;
public LazyAccounts(String accountName) {
this.accountName = accountName;
}
public Object call(String param1, Map<String, Object> param2) {
return [
SELECT Id
FROM Account
WHERE Name = :accountName
];
}
}
You could certainly put together something like that in Apex. And you can do similar with iterators - only iterating when the data is actually requested.
The only downside is that Apex is missing a few language niceties that exist in Java and would make the code more succinct (and type-safe if we had templates).
Not exactly, but you can do something like lazy evaluation by making use of the Callable
interface (or rolling your own). Here's an example using Callable
...
To start with, a test:
@IsTest private class LazyTest {
@TestSetup static void setup() {
List<Account> accounts = new List<Account> {
new Account(Name = 'A'),
new Account(Name = 'B')};
insert accounts;
List<Contact> contacts = new List<Contact> {
new Contact(LastName = '1', AccountId = accounts[0].Id),
new Contact(LastName = '2', AccountId = accounts[1].Id)};
insert contacts;
}
@IsTest static void testBehavior() {
Callable lazyContacts = new LazyContacts(new LazyAccounts('A'));
// No calls made yet, so I can pass this around at no great cost
System.assertEquals(0, Limits.getQueries());
// OK, I've decided I need those contacts now...
List<Contact> results = (List<Contact>)lazyContacts.call(null, null);
System.assertEquals(2, Limits.getQueries());
System.assertEquals(1, results.size());
System.assertEquals('1', results[0].LastName);
}
}
In the test, lazyContacts
is not yet the Contacts I'm looking for. It's like a Callable
that will provide the Contacts when I actually need them. It's parameterised by another Callable
, so that inner one is also not invoked until you decide you actually need it.
The actual implementations:
public with sharing class LazyContacts implements Callable {
private Callable lazyAccounts;
public LazyContacts(Callable lazyAccounts) {
this.lazyAccounts = lazyAccounts;
}
public Object call(String param1, Map<String, Object> param2) {
return [
SELECT LastName
FROM Contact
WHERE AccountId IN :(List<SObject>)lazyAccounts.call(null, null)
];
}
}
public with sharing class LazyAccounts implements Callable {
private String accountName;
public LazyAccounts(String accountName) {
this.accountName = accountName;
}
public Object call(String param1, Map<String, Object> param2) {
return [
SELECT Id
FROM Account
WHERE Name = :accountName
];
}
}
You could certainly put together something like that in Apex. And you can do similar with iterators - only iterating when the data is actually requested.
The only downside is that Apex is missing a few language niceties that exist in Java and would make the code more succinct (and type-safe if we had templates).
answered 1 hour ago
AidanAidan
7,1701144
7,1701144
Thanks for the response. Interesting approach. I'm not really sure what "Callable" brings to this conversation since, a you point out, we could roll our own interface and do so in a way that doesn't have unused parameters and could have greater type safety... Also doesn't solve the problem of a, a.b, etc possibly being null (but I guess that probably isn't solvable in Apex)
– Brian Kessler
1 hour ago
The advantage of callable is that it exists already, so no extra dependencies if you're using unlocked packaging. And it does keep everything very loosely coupled. It seems like you're asking two questions, really: 1. How can I defer evaluation? A: Use callable or something similar 2. How can I avoid lots of null-checks. 1 seems like the bigger problem to me, so that's what I answered. 2 can be done by replacing null with an appropriate actual object e.g. an empty list, or string, or a Null Object. Then make sure you don't do expensive things if the incoming data doesn't need them
– Aidan
54 mins ago
add a comment |
Thanks for the response. Interesting approach. I'm not really sure what "Callable" brings to this conversation since, a you point out, we could roll our own interface and do so in a way that doesn't have unused parameters and could have greater type safety... Also doesn't solve the problem of a, a.b, etc possibly being null (but I guess that probably isn't solvable in Apex)
– Brian Kessler
1 hour ago
The advantage of callable is that it exists already, so no extra dependencies if you're using unlocked packaging. And it does keep everything very loosely coupled. It seems like you're asking two questions, really: 1. How can I defer evaluation? A: Use callable or something similar 2. How can I avoid lots of null-checks. 1 seems like the bigger problem to me, so that's what I answered. 2 can be done by replacing null with an appropriate actual object e.g. an empty list, or string, or a Null Object. Then make sure you don't do expensive things if the incoming data doesn't need them
– Aidan
54 mins ago
Thanks for the response. Interesting approach. I'm not really sure what "Callable" brings to this conversation since, a you point out, we could roll our own interface and do so in a way that doesn't have unused parameters and could have greater type safety... Also doesn't solve the problem of a, a.b, etc possibly being null (but I guess that probably isn't solvable in Apex)
– Brian Kessler
1 hour ago
Thanks for the response. Interesting approach. I'm not really sure what "Callable" brings to this conversation since, a you point out, we could roll our own interface and do so in a way that doesn't have unused parameters and could have greater type safety... Also doesn't solve the problem of a, a.b, etc possibly being null (but I guess that probably isn't solvable in Apex)
– Brian Kessler
1 hour ago
The advantage of callable is that it exists already, so no extra dependencies if you're using unlocked packaging. And it does keep everything very loosely coupled. It seems like you're asking two questions, really: 1. How can I defer evaluation? A: Use callable or something similar 2. How can I avoid lots of null-checks. 1 seems like the bigger problem to me, so that's what I answered. 2 can be done by replacing null with an appropriate actual object e.g. an empty list, or string, or a Null Object. Then make sure you don't do expensive things if the incoming data doesn't need them
– Aidan
54 mins ago
The advantage of callable is that it exists already, so no extra dependencies if you're using unlocked packaging. And it does keep everything very loosely coupled. It seems like you're asking two questions, really: 1. How can I defer evaluation? A: Use callable or something similar 2. How can I avoid lots of null-checks. 1 seems like the bigger problem to me, so that's what I answered. 2 can be done by replacing null with an appropriate actual object e.g. an empty list, or string, or a Null Object. Then make sure you don't do expensive things if the incoming data doesn't need them
– Aidan
54 mins ago
add a comment |
Thanks for contributing an answer to Salesforce Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f251558%2fis-there-any-way-to-make-an-apex-method-parameter-lazy%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
This Idea would help, though not as nice as having a shortcut like
?
null checks available in .Net.– sfdcfox
14 mins ago