Optional Arguments for Do-Block Syntax for Functions

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Optional Arguments for Do-Block Syntax for Functions

Adrian Salceanu
I love the block syntax for functions as it provides great readability and I'm using it heavily as a design pattern to enclose code behind authorization or caching. 

For example: 

function article_edit(params::Dict{Symbol,Any}; a::Article = Article())
  with_authorization
(:edit, unauthorized_access, params) do auth_scopes
    article
= SearchLight.is_persisted(a) ? a : SearchLight.find_one!!(Article, params[:article_id])
    ejl
(:admin, :article, layout = :admin, article = article, params = params) |> respond
 
end
end

I was wondering if there's any way to make the auth_scopes param optional for the cases when the users are not interested in this passed value. Similarly to how in the ruby blocks you're not required to capture the values that are passed into, if you don't want them. It will help to greatly reduce the noise, especially when multiple params are expected. 
Reply | Threaded
Open this post in threaded view
|

Re: Optional Arguments for Do-Block Syntax for Functions

Stefan Karpinski
You can use a name like _ but otherwise, no, there's no way to do this.

On Fri, Nov 11, 2016 at 11:46 AM, Adrian Salceanu <[hidden email]> wrote:
I love the block syntax for functions as it provides great readability and I'm using it heavily as a design pattern to enclose code behind authorization or caching. 

For example: 

function article_edit(params::Dict{Symbol,Any}; a::Article = Article())
  with_authorization
(:edit, unauthorized_access, params) do auth_scopes
    article
= SearchLight.is_persisted(a) ? a : SearchLight.find_one!!(Article, params[:article_id])
    ejl
(:admin, :article, layout = :admin, article = article, params = params) |> respond
 
end
end

I was wondering if there's any way to make the auth_scopes param optional for the cases when the users are not interested in this passed value. Similarly to how in the ruby blocks you're not required to capture the values that are passed into, if you don't want them. It will help to greatly reduce the noise, especially when multiple params are expected. 

Reply | Threaded
Open this post in threaded view
|

Re: Optional Arguments for Do-Block Syntax for Functions

Adrian Salceanu
Got it, thanks

vineri, 11 noiembrie 2016, 19:11:28 UTC+1, Stefan Karpinski a scris:
You can use a name like _ but otherwise, no, there's no way to do this.

On Fri, Nov 11, 2016 at 11:46 AM, Adrian Salceanu <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="akFavQu1BQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">adrian....@...> wrote:
I love the block syntax for functions as it provides great readability and I'm using it heavily as a design pattern to enclose code behind authorization or caching. 

For example: 

function article_edit(params::Dict{Symbol,Any}; a::Article = Article())
  with_authorization
(:edit, unauthorized_access, params) do auth_scopes
    article
= SearchLight.is_persisted(a) ? a : SearchLight.find_one!!(Article, params[:article_id])
    ejl
(:admin, :article, layout = :admin, article = article, params = params) |> respond
 
end
end

I was wondering if there's any way to make the auth_scopes param optional for the cases when the users are not interested in this passed value. Similarly to how in the ruby blocks you're not required to capture the values that are passed into, if you don't want them. It will help to greatly reduce the noise, especially when multiple params are expected.