# Bug during redefinition of \`Base.getproperty\` for abstract type

**URL:** <https://discourse.julialang.org/t/bug-during-redefinition-of-base-getproperty-for-abstract-type/99203>\
**Category:** General Usage\
**Created:** [May 22, 2023, 8:31am UTC](https://discourse.julialang.org/t/bug-during-redefinition-of-base-getproperty-for-abstract-type/99203 "2023-05-22T08:31:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sirion34](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sirion34/32/49526_2.png) [@sirion34](https://discourse.julialang.org/u/sirion34)\
**Post date:** [May 22, 2023, 8:31am UTC](https://discourse.julialang.org/t/bug-during-redefinition-of-base-getproperty-for-abstract-type/99203/1 "2023-05-22T08:31:06Z")

</div>

why does this bug occur?

if you run this code, the console will give a lot of “name” and close with an error, but I didn’t even run the function

```julia
abstract type AbstractNewType end

function Base.getproperty(x::Type{A}, key::Symbol) where {A<:AbstractNewType}
    println(key)
    println(x)
end

```

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [May 22, 2023, 9:17am UTC](https://discourse.julialang.org/t/bug-during-redefinition-of-base-getproperty-for-abstract-type/99203/2 "2023-05-22T09:17:46Z")

</div>

I can run this without problem in Julia 1.9 on Linux. What does `versioninfo()` say?

---

<div class="post-metadata">

**Author:** ![sirion34](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sirion34/32/49526_2.png) [@sirion34](https://discourse.julialang.org/u/sirion34)\
**Post date:** [May 22, 2023, 9:21am UTC](https://discourse.julialang.org/t/bug-during-redefinition-of-base-getproperty-for-abstract-type/99203/3 "2023-05-22T09:21:25Z")

</div>

```julia
Julia Version 1.8.5
Commit 17cfb8e65ea (2023-01-08 06:45 UTC)
Platform Info:
  OS: macOS (arm64-apple-darwin21.5.0)
  CPU: 8 × Apple M1 Pro
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-13.0.1 (ORCJIT, apple-m1)
  Threads: 1 on 6 virtual cores
Environment:
  JULIA_EDITOR = code
  JULIA_NUM_THREADS = 

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [May 22, 2023, 9:30am UTC](https://discourse.julialang.org/t/bug-during-redefinition-of-base-getproperty-for-abstract-type/99203/4 "2023-05-22T09:30:57Z")

</div>

Also no problem on Julia 1.8.5, Windows 10.

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [May 22, 2023, 11:53am UTC](https://discourse.julialang.org/t/bug-during-redefinition-of-base-getproperty-for-abstract-type/99203/5 "2023-05-22T11:53:32Z")

</div>

I can reproduce with

```julia
abstract type MyType end
function Base.getproperty(x::Type{A}, key::Symbol) where {A<:MyType}
    println(key)
    println(x)
end

[foo() for i in 1:2 for j in 1:2]

```

I think this code is comitting type piracy, because the user owns `MyType` but not `Type{<:MyType}`.

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [May 22, 2023, 12:10pm UTC](https://discourse.julialang.org/t/bug-during-redefinition-of-base-getproperty-for-abstract-type/99203/6 "2023-05-22T12:10:46Z")

</div>

It’s technically piracy, but just becaues `A <: MyType` can be `A = Union{}`. But even without straight piracy, redefining any aspects for `Type{MyType}` may easily mess with Julia internals expectation. Better not to do that (: At best, maybe you can _add_ properties, but surely should keep existing ones.

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [May 22, 2023, 2:26pm UTC](https://discourse.julialang.org/t/bug-during-redefinition-of-base-getproperty-for-abstract-type/99203/7 "2023-05-22T14:26:07Z")

</div>

Are you sure you need to define this for `::Type{<:AbstractNewType}` (for the _subtypes_) and not just `::AbstractNewType` (an _instance_ of the abstract type)?

I’ve never felt the need to define properties for types themselves (and, for that matter, I seldom define a non-default `getproperty` for any object). If you find this necessary, an alternative is to instead define some accessor functions and use those rather than `getproperty`. E.g., `flavor(::Type{<:AbstractNewType}) = :chocolate`.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [May 22, 2023, 2:27pm UTC](https://discourse.julialang.org/t/bug-during-redefinition-of-base-getproperty-for-abstract-type/99203/8 "2023-05-22T14:27:28Z")

</div>

This is type piracy. There are many points in the compiler where `SomeType.field` is used and it expects that the field of the type is returned.
