# Help making a literal function type

**URL:** <https://discourse.julialang.org/t/help-making-a-literal-function-type/8278>\
**Category:** General Usage\
**Tags:** question\
**Created:** [January 11, 2018, 12:38am UTC](https://discourse.julialang.org/t/help-making-a-literal-function-type/8278 "2018-01-11T00:38:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [January 11, 2018, 12:38am UTC](https://discourse.julialang.org/t/help-making-a-literal-function-type/8278/1 "2018-01-11T00:38:58Z")

</div>

So I’m working on a bit of a pet project which involves rolling my own computer algebra system. So far its really fun but I’m getting stuck with trying to define a special type of function called a `literal_function`. The idea with literal functions is to make a procedure that represents a function of one argument that has no known properties other than the given symbolic name.

Ie. I would like to be able to say

```julia
julia> f = literal_function(:f)
julia> f(:t)
:(f(t))

```

ie. a `literal_function` is constructed with a `Symbol` as its name and when given a symbolic input it just returns an expression that looks like  
and unevaluated function call. This is easy to do, I did it simply with

```julia
function literal_function(name)
    function (t)
        :($name($t))
    end
end

```

However, I would like to be able to dispatch differently on `literal_functions` than I would with normal functions in certain circumstances, ie. I would like to have a type `Literal_Function <: Function` such that I can do `some_other_function(f::Literal_Function) = stuff`.

so I adapted some advice I got on Slack for another question I had and tried to make a type `Literal_Function` which does what I want.

```julia
struct Literal_Function <: Function end

function literal_function(name)
    function Literal_Function(t)
        :($name($t))
    end
end

```

but unfortunately the object this returns is not of type `Literal_Function`, its of type `#Literal_Function#1{Symbol}` .  
Does anyone know how I can make my constructor `literal_function` return a function like object of type `Literal_Function`?

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [January 11, 2018, 1:52am UTC](https://discourse.julialang.org/t/help-making-a-literal-function-type/8278/2 "2018-01-11T01:52:49Z")

</div>

> [@Mason](#):
>
> struct Literal\_Function \<: Function end

Maybe something like this:

```julia
julia> struct LiteralFunction
           name::Symbol
       end

julia> x = LiteralFunction(:x)
LiteralFunction(:x)

julia> (f::LiteralFunction)(t) = :($(f.name)($t)) # **

julia> x(:t)
:(x(t))

```

Here `LiteralFunction` becomes a function-like object by defining how to call it in the line marked with \*\*.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [January 11, 2018, 2:37am UTC](https://discourse.julialang.org/t/help-making-a-literal-function-type/8278/3 "2018-01-11T02:37:54Z")

</div>

I see, that’s a nice solution! thanks a bunch.
