Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: spec: Referencing structs field type in another field definition #69194

Closed
1 of 4 tasks
sjiamnocna opened this issue Sep 1, 2024 · 3 comments
Closed
1 of 4 tasks
Labels
LanguageChange Suggested changes to the Go language LanguageChangeReview Discussed by language change review committee Proposal
Milestone

Comments

@sjiamnocna
Copy link

sjiamnocna commented Sep 1, 2024

Go Programming Experience

Intermediate

Other Languages Experience

Javascript, Typescript, React/NextJS, PHP, C/C++

Related Idea

  • Has this idea, or one like it, been proposed before?
  • Does this affect error handling?
  • Is this about generics?
  • Is this change backward compatible? Breaking the Go 1 compatibility guarantee is a large cost and requires a large benefit

Has this idea, or one like it, been proposed before?

No

Does this affect error handling?

No

Is this about generics?

No

Proposal

Hello!
I would like to suggest a feature to the Go language.
It would be nice to be able to reference the field type of another model directly, similar to how TypeScript allows referencing types from other interfaces or classes.
I know I can define a type and use that type for it, but this would improve the process with less redundant code.

My situation:
Currently, when defining a model in GORM, I must explicitly specify the type of fields, including foreign keys, which can lead to inconsistencies and increased maintenance overhead when changing models etc.

In TS I can do this:

interface Product {
    id: number;
    name: string;
}

interface Order {
    productId: Product['id']; // References the type of Product's id
    quantity: number;
}

In the example, Order type references the type of Product's id property directly, ensuring that any changes to the Product interface will automatically reflect in the Order interface.

Thanks.

Language Spec Changes

Adding new way of defining struct type

Informal Change

No response

Is this change backward compatible?

Yes

Orthogonality: How does this change interact or overlap with existing features?

No response

Would this change make Go easier or harder to learn, and why?

This feature will definitely simplify learning Go to (not only) Typescript users.

Cost Description

No response

Changes to Go ToolChain

I guess type check features, syntax highlighters.

Performance Costs

No runtime, comptime is affected like similiar to adding new type to the code

Prototype

To implement a similar feature in Go, I suggest introducing a syntax that allows referencing another model's field type directly. As Go uses object a dot for struct data, either use a keyword typeof Model.Column or Model['Column'].

For example:

type Product struct {
    ID   uint   `gorm:"primaryKey"`
    Name string `json:"name"`
}

type Order struct {
    ProductID1 typeof Product.ID // Reference to Product's ID type
    ProductID2 Product['ID'] // alternative
    Quantity  int       `json:"quantity"`
}

Thanks for reading and feedback.

@sjiamnocna sjiamnocna added LanguageChange Suggested changes to the Go language LanguageChangeReview Discussed by language change review committee Proposal labels Sep 1, 2024
@gopherbot gopherbot added this to the Proposal milestone Sep 1, 2024
@sjiamnocna sjiamnocna changed the title proposal: spec: proposal title proposal: spec: Referencing structs field type in another field definition Sep 1, 2024
@gabyhelp
Copy link

gabyhelp commented Sep 1, 2024

Related Issues and Documentation

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

@sjiamnocna
Copy link
Author

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

I'd like to ask if any of the thoughts in comment here will be implemented. As it's the core thought of this propsal.

@seankhliao
Copy link
Member

Duplicate of #7112

@seankhliao seankhliao marked this as a duplicate of #7112 Sep 1, 2024
@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
LanguageChange Suggested changes to the Go language LanguageChangeReview Discussed by language change review committee Proposal
Projects
None yet
Development

No branches or pull requests

4 participants