Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7538195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:03:35+00:00 2026-05-30T07:03:35+00:00

An example in the tutorial section : Defining Metamethods for a C Type looks

  • 0

An example in the tutorial section : “Defining Metamethods for a C Type” looks as follows:

local ffi = require("ffi")
ffi.cdef[[
typedef struct { double x, y; } point_t;
]]

local point
local mt = {
  __add = function(a, b) return point(a.x+b.x, a.y+b.y) end,
  __len = function(a) return math.sqrt(a.x*a.x + a.y*a.y) end,
  __index = {
    area = function(a) return a.x*a.x + a.y*a.y end,
  },
}
point = ffi.metatype("point_t", mt)

local a = point(3, 4)

I’m a bit confused about where the “constructor” is, I assume by default point(3,4) is implicity routing 3 -> x and 5 -> y. What about when I want to hang some logic onto the constructor ? Put differently.. How do I specify a non-default constructor ?

I’m wrapping a bunch of c-libraries into object oriented lua code, and I do not care about portability to canonical lua. Specifically I need to hook in the three core functions of object oriented programming for object lifetime management, create , init, destroy. I know destroy will be the __gc method of my types’ metatable. So I need to know how to do create and init, and hopefully avoid a default initialization done by luajit.

edit

ffi.new and others have a bunch of rules governing the creation of types (documented in the luajit’s ffi semantics page). it’s in the semantics section. I’d still like to know what the cleanest way would be to hang custom creators and initializers (that come as part of a c library) into ffi object creation.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T07:03:37+00:00Added an answer on May 30, 2026 at 7:03 am

    You’d need to wrap your point call to get what you desire:

    local function newpoint ( vals )
        -- Do stuff with vals here?
        return point ( vals )
    end
    newpoint {x=5;y=4}
    

    OR you could consider your point function as your create function; and just have an init method…

    mt.__index.init = function ( p , x , y )
         p.x = x;
         p.y = y;
    end
    
    local mypoint = point()
    mypoint:init ( 1 , 2 )
    

    Note; all objects of the point type already have your metatable applied, so you dont need to attach methods or anything.

    It does seem a bit pointless to me…. why do you want to seperate creation and initialization??

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Could you tell me some http-streaming tutorial or example ( used also by Gmail
Does anyone know where I can find an example or a tutorial of building
Here is an example of polymorphism from http://www.cplusplus.com/doc/tutorial/polymorphism.html (edited for readability): // abstract base
I am reading a tutorial that uses the following example (that I'll generalize somewhat):
I'm looking for an article or tutorial that gives an example of what an
I want to rewrite in scala the example from Sun's tutorial about concurrency in
When attempting to run the first example in the boost::mpi tutorial , I was
I'm searching for a simple example code or a complete tutorial how to create
I'm trying to run the Camel Example camel-example-spring-jms (also at http://activemq.apache.org/camel/tutorial-jmsremoting.html ). However, when
So I found this: http://tiles.apache.org/framework/tutorial/advanced/nesting-extending.html Here is the example: <definition name=myapp.homepage template=/layouts/classic.jsp> <put-attribute name=title

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.