# Building function (like OOP) inside a struct

**URL:** https://discourse.julialang.org/t/building-function-like-oop-inside-a-struct/126180
**Category:** General Usage
**Tags:** question
**Created:** [February 22, 2025, 10:57am UTC](https://discourse.julialang.org/t/building-function-like-oop-inside-a-struct/126180 "2025-02-22T10:57:45Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![xinzhicaishu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xinzhicaishu/32/23913_2.png) [@xinzhicaishu](https://discourse.julialang.org/u/xinzhicaishu)
#### Post date: [February 22, 2025, 10:57am UTC](https://discourse.julialang.org/t/building-function-like-oop-inside-a-struct/126180/1 "2025-02-22T10:57:46Z")

</div>

This is my demo, my goal is run `set_grade!` successfully. How can achieve this?

```julia

# try demo
mutable struct Group

    members::Dict{String, Int64} # project => grade
    const list::Vector{String} # project name list
    const set_grade!::Function
    # 
    function set_grade!(group::Group, project::String, grade::Int64)
        if project in group.list
            if !(haskey(group.list, project))
                push!(group.members, project => grade)
            end
        else
            error("wrong project name: ", project)
        end
    end
    function Base.getproperty(group::Group, prop::Symbol)
        if prop == :set_grade!
            return (project, grade,)->set_grade!(group, project, grade)
        else
            throw(UndefVarError(prop))
        end
    end

    # 
    function Group()
        group = new(
            Dict{String,Any}(), # members
            ["project1", "project2", "project3"], # list
            (project, grade,)->set_grade!(group, project, grade) # function
        )
        return group
    end
end

member1 = Group();
member1.set_grade!("project1", 93) # wrong

```

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [February 22, 2025, 11:15am UTC](https://discourse.julialang.org/t/building-function-like-oop-inside-a-struct/126180/2 "2025-02-22T11:15:49Z")

</div>

Short answer: don’t do this. Julia style is to do `method(object, args…)` and not `object.method(args…)`, and to define methods _outside_ of `struct` definitions (with the exception of [inner constructors](https://docs.julialang.org/en/v1/manual/constructors/#man-inner-constructor-methods)). Trying to force OOP syntax on Julia will lead to non-idiomatic code, [re-introduces the expression problem](https://www.youtube.com/watch?v=kc9HwsxE1OY), and [will be slow](https://docs.julialang.org/en/v1/manual/performance-tips/#Avoid-fields-with-abstract-type) if you’re not clever.

Long answer: see many previous discussions of this topic, e.g.:

- [Allowing the object.method(args...) syntax as an alias for method(object, args ...)](https://discourse.julialang.org/t/allowing-the-object-method-args-syntax-as-an-alias-for-method-object-args/62051)
- [Do julia structs have member functions (and this-\>)](https://discourse.julialang.org/t/do-julia-structs-have-member-functions-and-this/8839)
