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

  • Home
  • SEARCH
  • 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 9060335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:12:10+00:00 2026-06-16T15:12:10+00:00

I have the following small example(vala 0.18.1): namespace Learning { public interface IExample<T> {

  • 0

I have the following small example(vala 0.18.1):

namespace Learning
{
   public interface IExample<T>
   {
      public abstract void set_to(T val);
      public abstract T get_to();
   }

   public class Example : Object, IExample<double>
   {
      private double to;

      public void set_to(double val)
      {
         to = val;
      }

      public double get_to()
      {
         return to;
      }

      public string to_string()
      {
         return "Example: %.5f".printf(to);
      }
   }

   public class Test
   {
      public static void main(string[] args)
      {
         stdout.printf("Start test\n");

         Example ex = new Example();

         stdout.printf("%s\n", ex.to_string());
         ex.set_to(5.0);
         stdout.printf("%s\n", ex.to_string());

         stdout.printf("End test\n");
      }
   }
}

This throws the error:

/src/Test.vala.c: In function ‘learning_test_main’:
/src/Test.vala.c:253:2: error: incompatible type for argument 2 of ‘learning_iexample_set_to’
/src/Test.vala.c:117:6: note: expected ‘gconstpointer’ but argument is of type ‘double’
error: cc exited with status 256
Compilation failed: 1 error(s), 0 warning(s)

Now from what little documentation I have been able to find on generics interfaces in Vala, http://www.vala-project.org/doc/vala-draft/generics.html#genericsexamples, this should work. When I check the resulting C code, it shows the Example’s set_to function as taking a double and the IExample’s set_to function as taking a gconstpointer.

So why is the main function then using the gconstpointer version instead of the double version? Can some one explain to me why it does not work and a way to get around it?

Thank you for your help.

P.S. Yes I know the documentation found is a draft document.

ANSWER CODE:
According to the selected answer below this is what I changed the code to.

namespace Learning
{
   public interface IExample<T>
   {
      public abstract void set_to(T val);
      public abstract T get_to();
   }

   public class Example : Object, IExample<double?>
   {
      private double? to;

      public void set_to(double? val)
      {
         to = val;
      }

      public double? get_to()
      {
         return to;
      }

      public string to_string()
      {
         return (to == null) ? "NULL" : "Example: %.5f".printf(to);
      }
   }

   public class Test
   {
      public static void main(string[] args)
      {
         stdout.printf("Start test\n");

         Example ex = new Example();

         stdout.printf("%s\n", ex.to_string());
         ex.set_to(5.0);
         stdout.printf("%s\n", ex.to_string());

         stdout.printf("End test\n");
      }
   }
}
  • 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-06-16T15:12:11+00:00Added an answer on June 16, 2026 at 3:12 pm

    Rather than using IExample<double>, use IExample<double?> to box the double so that it can be passed as a pointer. This necessary, generally, for any struct type in Vala. Classes and compact classes do not need this treatment, as they are already pointers. Also, struct types that are smaller than 32-bits (even on 64-bit platforms) such as uint8 or char can be used directly without boxing. When in doubt, box.

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

Sidebar

Related Questions

I have big dataset (but the following is small one for example). I can
I have following problem. I have a team page like this example: http://www.social-markets.de/social-commerce-team.html small
Say I have the following example: #include <cstdlib> class A { public: static const
I have following small script to preview some text before submitting it to store
I have the following parser grammar (this is a small sample): expr: ident assignop
In the root directory of a small site i'm developing i have the following
Work on this small test application to learn threading/locking. I have the following code,
I'm doing a small app in Excel, and I have the following code when
I have a small I have just written which uses the following uri template.
A small - little question. I have seen many application having buttons like following.

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.